- Feature Name:
cfg_attr_multi
- Start Date: 2018-09-10
- RFC PR: rust-lang/rfcs#2539
- Rust Issue: rust-lang/rust#54881
Summary
Change cfg_attr
to allow
Motivation
Simply put, ergonomics and intent. When you have multiple
Guide-level explanation
The cfg_attr
attribute takes
For an example of multiplesparkles
and crackles
), but only when feature = "magic"
is enabled. We can write this as:
When the feature flag is enabled, it expands to:
#[sparkles]
#[crackles]
fn bewitche() {}
The list
Reference-level explanation
The next sectioncfg_attr
attribute. It explains both current and new behavior,
cfg_attr
Attribute
The cfg_attr
attribute conditionally includes attributes based
It is written as cfg_attr
followed(
, a comma separated)
The metaitem sequence
cfg_attr(predicate, attr)
cfg_attr(predicate, attr_1, attr_2)
cfg_attr(predicate, attr,)
cfg_attr(predicate, attr_1, attr_2,)
cfg_attr(predicate,)
Note:
cfg_attr(predicate)
is not allowed.許可する、可能にするThat comma is semantically意味論的にdistinct区別された/独立したfrom the commas following下記の、次に続く、追従するattributes, so we require it.
When the configuration predicate is true, this attribute expands out to be an attribute for each attribute metaitem. For example, the followinglinux.rs
or windows.rs
based
#[cfg_attr(linux, path = "linux.rs")]
#[cfg_attr(windows, path = "windows.rs")]
mod os;
For an example of multiplefeature = "magic"
is enabled. We can write this as:
When the feature flag is enabled, the attribute expands to:
#[sparkles]
#[crackles]
fn bewitche() {}
Note: The cfg_attr
can expand to another cfg_attr
. For example, #[cfg_attr(linux, cfg_attr(feature = "multithreaded", some_other_attribute))
is valid.#[cfg_attr(all(linux, feaure ="multithreaded"), some_other_attribute)]
.
Warning When Zero Attributes
This RFC allows#[cfg_attr(predicate,)]
. This is so that macros can generate it. Having it in the sourceunused_attributes
warning.
Attribute Syntax文法 Opportunity Cost
This would be the first place attributes would be allowed
Today, an attribute can look like:
name
,name(`TokenStream`)
name = `TokenTree`
where TokenStream
is a sequenceTokenTree
is a singleTokenStream
.
With this RFC accepted,
name, option
name = some, options
Arguably, we could allow(name, option)
, but we shouldn't.
This restriction#[]
container, which has been suggested, but this RFC will not tackle.
Drawbacks
It's another thing that has to be learned. Though even there, it's just learning that the attribute takes
It restricts
Rationale and alternatives代わりのもの、選択肢
We could require that multiple#[cfg_attr(predicate, [attr, attr])]
. While this could increase clarity, it mostly seems like it would just add noise. In the multiline case, it already reads pretty clear with the predicate on the first line and each attribute indented.
The default alternative
We could change attribute container syntaxcfg_attr
takes#[]
part. While this could be a good final state, it's a more ambitious change that has more drawbacks. There are legitimate reasons we'd want cfg_attr
to take
The original draft of this RFC only allowed
Prior art
I cannot think of any prior art specifically,
Unresolved questions
None.