Skip to content

Commit 5a1be04

Browse files
author
Sergio Benitez
committed
Fixes and clarifications: '!' in grammar, "literal" definition, more examples.
1 parent 8768d9f commit 5a1be04

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

text/0000-attributes-with-literals.md

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
This RFC proposes accepting literals in attributes by defining the grammar of attributes as:
1010

1111
```ebnf
12-
attr : '#' '[' meta_item ']' ;
12+
attr : '#' '!'? '[' meta_item ']' ;
1313
1414
meta_item : IDENT ( '=' LIT | '(' meta_item_inner? ')' )? ;
1515
@@ -21,10 +21,12 @@ attributes, among others, would be accepted by this grammar:
2121

2222
```rust
2323
#[attr]
24-
#[attr()]
24+
#[attr(true)]
2525
#[attr(ident)]
26-
#[attr(ident, ident = 100, ident = "hello", ident(100))]
26+
#[attr(ident, 100, true, "true", ident = 100, ident = "hello", ident(100))]
2727
#[attr(100)]
28+
#[attr(enabled = true)]
29+
#[enabled(true)]
2830
#[attr("hello")]
2931
#[repr(C, align = 4)]
3032
#[repr(C, align(4))]
@@ -40,10 +42,11 @@ only _string_ literals are accepted. This means that literals can only appear in
4042
This forces non-string literal values to be awkwardly stringified. For example, while it is clear
4143
that something like alignment should be an integer value, the following are disallowed:
4244
`#[align(4)]`, `#[align = 4]`. Instead, we must use something akin to `#[align = "4"]`. Even
43-
`#[align("4")]` and `#[name("name")]` are disallowed, forcing identifiers or key-values to be used
44-
instead: `#[align(size = "4")]` or `#[name(name)]`.
45+
`#[align("4")]` and `#[name("name")]` are disallowed, forcing key-value pairs or identifiers to be
46+
used instead: `#[align(size = "4")]` or `#[name(name)]`.
4547

46-
In short, the current design forces users to use values of the wrong type in attributes.
48+
In short, the current design forces users to use values of a single type, and thus occasionally the
49+
_wrong_ type, in attributes.
4750

4851
### Cleaner Attributes
4952

@@ -67,14 +70,31 @@ positions becomes more important.
6770
# Detailed design
6871
[design]: #detailed-design
6972

73+
To clarify, _literals_ are:
74+
75+
* **Strings:** `"foo"`, `r##"foo"##`
76+
* **Byte Strings:** `b"foo"`
77+
* **Byte Characters:** `b'f'`
78+
* **Characters:** `'a'`
79+
* **Integers:** `1`, `1{i,u}{8,16,32,64,size}`
80+
* **Floats:** `1.0`, `1.0f{32,64}`
81+
* **Booleans:** `true`, `false`
82+
83+
They are defined in the [manual] and by implementation in the [AST].
84+
85+
[manual]: https://doc.rust-lang.org/reference.html#literals
86+
[AST]: http://manishearth.github.io/rust-internals-docs/syntax/ast/enum.LitKind.html
87+
88+
Implementation of this RFC requires the following changes:
89+
7090
1. The `MetaItemKind` structure would need to allow literals as top-level entities:
7191

7292
```rust
7393
pub enum MetaItemKind {
7494
Word(InternedString),
7595
List(InternedString, Vec<P<MetaItem>>),
7696
NameValue(InternedString, Lit),
77-
Lit,
97+
Literal(Lit),
7898
}
7999
```
80100

@@ -92,12 +112,12 @@ the wild.
92112
# Alternatives
93113
[alternatives]: #alternatives
94114

95-
### Token Trees
115+
### Token trees
96116

97117
An alternative is to allow any tokens inside of an attribute. That is, the grammar could be:
98118

99119
```ebnf
100-
attr : '#' '[' TOKEN+ ']' ;
120+
attr : '#' '!'? '[' TOKEN+ ']' ;
101121
```
102122

103123
where `TOKEN` is any valid Rust token. The drawback to this approach is that attributes lose any
@@ -108,21 +128,36 @@ attribute parsing code to change.
108128
The advantage, of course, is that it allows any syntax and is rather future proof. It is also more
109129
inline with `macro!`s.
110130

111-
### Only Allow Literals as Values in K/V Pairs
131+
### Allow only unsuffixed literals
132+
133+
This RFC proposes allowing _any_ valid Rust literals in attributes. Instead, the use of literals
134+
could be restricted to only those that are unsuffixed. That is, only the following literals could be
135+
allowed:
136+
137+
* **Strings:** `"foo"`
138+
* **Characters:** `'a'`
139+
* **Integers:** `1`
140+
* **Floats:** `1.0`
141+
* **Booleans:** `true`, `false`
142+
143+
This cleans up the appearance of attributes will still increasing flexibility.
144+
145+
### Allow literals only as values in k/v pairs
112146

113147
Instead of allowing literals in top-level positions, i.e. `#[attr(4)]`, only allow them as values in
114148
key value pairs: `#[attr = 4]` or `#[attr(ident = 4)]`. This has the nice advantage that it was the
115149
initial idea for attributes, and so the AST types already reflect this. As such, no changes would
116150
have to be made to existing code. The drawback, of course, is the lack of flexibility. `#[repr(C,
117151
align(4))]` would no longer be valid.
118152

119-
### Do Nothing
153+
### Do nothing
120154

121155
Of course, the current design could be kept. Although it seems that the initial intention was for a
122156
form of literals to be allowed. Unfortunately, this idea was [scrapped due to release pressure] and
123-
never revisited. Even the manual alludes to allowing all literals.
157+
never revisited. Even [the reference] alludes to allowing all literals as values in k/v pairs.
124158

125159
[scrapped due to release pressure]: https://github.com/rust-lang/rust/issues/623
160+
[the manual]: https://doc.rust-lang.org/reference.html#attributes
126161

127162
# Unresolved questions
128163
[unresolved]: #unresolved-questions

0 commit comments

Comments
 (0)