- Start Date: 2014-03-20
- RFC PR: rust-lang/rfcs#16
- Rust Issue: rust-lang/rust#15701
Summary
Allow
Motivation
One sometimes wishes to annotate things inside functions with, for example, lint #[allow]
s, conditional#[cfg]
s, and even extra semantic (or otherwise) annotations for external tools.
For the lints, one can currently only activate lints at the level of the function which is possibly
For the conditional#[cfg]
, or breaking the conditional
The sort of things one could do with other arbitrary
and then have an external tool that checks that that unsafe
block's only unsafe actions are FFI, or a tool that lists
The minimumlet
statements,
Detailed design設計(する)
Normal attribute syntaxlet
statements,
Attributes bind tighter than any operator,#[attr] x op y
is always parsed(#[attr] x) op y
.
cfg
It is definitely an error to place a #[cfg]
attribute on a non-statement expressions,attr1
--attr4
can possibly#[cfg(foo)]
, but attr5
--attr7
cannot, since it makes little sense to strip code down to let x = ;
.
However, like #ifdef
in C/C++, widespread use of #[cfg]
may be an antipattern that makes code harder to read. This RFC is just adding#[cfg]
actually be stripped in those places (although it should be an error if it is ignored).
Inner内側の attributes
Inner
if
Attributes would be disallowedif
for now, because the interaction with if
/else
chains are funky, and can be simulated in other ways.
There is two possible interpretationsif ... else
chain ("exterior") or just to the branch
--cfg foo
: could be either removing the whole chain (exterior) or equivalent等価toif cond2 {} else {}
(interior).--cfg bar
: could be eitherif cond1 {}
(e) orif cond1 {} else {}
(i)--cfg baz
: equivalent等価toif cond1 {} else if cond2 {}
(no subtlety).--cfg foo --cfg bar
: could be removing the whole chain (e) or the twoif
branches枝、分岐(leaving only theelse
branch) (i).
(This applies#[cfg]
, e.g. #[allow]
and #[warn]
for lints.)
As such, to avoidif
. Alternatives include using blocks:
And, if the attributes are meant to be associated with the actual#[cold]
attribute that indicates a branchmatch
arms:
Drawbacks
This starts mixing attributes with nearly arbitrary@
for attributes, this change may make this impossible (especially if @
gets reused for something else, e.g. Python is using it for matrix multiplication#
for other things.
As stated above, allowing#[cfg]
s everywhere can make code harder to reason about, but (also stated), this RFC is not for making such #[cfg]
s be obeyed, it just opens the language
Alternatives
These instancesunsafe
block).
Only allowing
The if
/else
issue may be able to be resolved by introducing explicitif
: by having #[attr] if cond { ...
be an exterior attribute (applying to the whole if
/else
chain) and if cond #[attr] { ...
be an interior attribute (applying to only the current if
branch). There is no difference between interior and exterior for an else {
branch,else #[attr] {
is sufficient.
Unresolved questions
Are the complications of allowing