@@ -23,6 +23,7 @@ Attributes and inner attributes would be written in one of the following forms
23
23
24
24
```
25
25
ATTR = '@' [!] META
26
+ | '@' '[' [!] META ']'
26
27
META = ID
27
28
| ID '(' META_SEQ ')'
28
29
META_SEQ = META_ITEM {',' META_ITEM}
@@ -34,13 +35,33 @@ Here are some examples of legal syntax:
34
35
35
36
* ` @inline `
36
37
* ` @!inline `
38
+ * ` @[inline] `
37
39
* ` @deprecated(reason = "foo") `
38
40
* ` @deriving(Eq) `
39
41
40
42
Note that some attributes which are legal today have no equivalent:
41
43
42
44
* ` #[deprecated = "foo"] ` becomes ` @deprecated(reason = "foo") `
43
45
46
+ ` [] ` delimiters are allowed but not required. They are necessary to avoid
47
+ mis-parses in certain situations:
48
+ ``` rust
49
+ match (foo , bar ) {
50
+ @ thing
51
+ (a , b ) => { ... }
52
+ ...
53
+ }
54
+ ```
55
+ This will parse as the attribute ` @thing(a, b) ` , which will in turn result in
56
+ a syntax error. ` [] ` delimiters will resolve the ambiguity:
57
+ ``` rust
58
+ match (foo , bar ) {
59
+ @ [thing ]
60
+ (a , b ) => { ... }
61
+ ...
62
+ }
63
+ ```
64
+
44
65
## Implementation
45
66
46
67
The parser will be adjusted to accept ` @ ` -attributes in addition to current
@@ -55,12 +76,7 @@ It's a large change that will cause a ton of churn very close to 1.0. Since the
55
76
only compiler changes required will be to the parser and pretty printer, it's
56
77
relatively low risk (compared to resolve or typeck changes for example).
57
78
58
- The lack of delimiters around the whole attribute does pose a small ambiguity
59
- problem once attributes are allowed to be attached to expressions. Is `@foo
60
- (1 + 1)` the attribute ` @foo` attached to the expression ` (1 + 1)` or is it
61
- the (syntactically invalid) attribute ` @foo(1+1) ` ? The parser will act greedily
62
- and take the second interpretation. The parenthesis can be replaced by ` {} ` or
63
- the attribute could be made an inner attribute: ` (@!foo 1+1) ` .
79
+ The need to continue to allow ` [] ` delimiters is a bit unfortunate.
64
80
65
81
# Alternatives
66
82
@@ -70,6 +86,8 @@ all that hard to deal with.
70
86
71
87
We can leave the syntax as is, which is also not that bad.
72
88
89
+ We could use ` () ` as the optional delimiters instead of ` [] ` .
90
+
73
91
Support for ` #[deprecated = "reason"] ` style attributes is removed because
74
92
` @deprecated = "reason" ` is a bit visually confusing since there are no
75
93
delimiters wrapping the attribute. There are a couple of alternatives here.
0 commit comments