Skip to content

Commit cf136d0

Browse files
committed
Allow [] delimiters
1 parent 2e19731 commit cf136d0

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

text/0000-attribute-syntax.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Attributes and inner attributes would be written in one of the following forms
2323

2424
```
2525
ATTR = '@' [!] META
26+
| '@' '[' [!] META ']'
2627
META = ID
2728
| ID '(' META_SEQ ')'
2829
META_SEQ = META_ITEM {',' META_ITEM}
@@ -34,13 +35,33 @@ Here are some examples of legal syntax:
3435

3536
* `@inline`
3637
* `@!inline`
38+
* `@[inline]`
3739
* `@deprecated(reason = "foo")`
3840
* `@deriving(Eq)`
3941

4042
Note that some attributes which are legal today have no equivalent:
4143

4244
* `#[deprecated = "foo"]` becomes `@deprecated(reason = "foo")`
4345

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+
4465
## Implementation
4566

4667
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
5576
only compiler changes required will be to the parser and pretty printer, it's
5677
relatively low risk (compared to resolve or typeck changes for example).
5778

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.
6480

6581
# Alternatives
6682

@@ -70,6 +86,8 @@ all that hard to deal with.
7086

7187
We can leave the syntax as is, which is also not that bad.
7288

89+
We could use `()` as the optional delimiters instead of `[]`.
90+
7391
Support for `#[deprecated = "reason"]` style attributes is removed because
7492
`@deprecated = "reason"` is a bit visually confusing since there are no
7593
delimiters wrapping the attribute. There are a couple of alternatives here.

0 commit comments

Comments
 (0)