Skip to content

Commit 8e1b5d8

Browse files
committed
Restrict value in key-value attributes to literals
1 parent b57fe74 commit 8e1b5d8

11 files changed

+87
-18
lines changed

src/libsyntax/parse/attr.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,21 @@ impl<'a> Parser<'a> {
158158
self.parse_token_tree().into()
159159
} else if self.eat(&token::Eq) {
160160
let eq = TokenTree::Token(self.prev_span, token::Eq);
161-
let tree = match self.token {
162-
token::CloseDelim(_) | token::Eof => self.unexpected()?,
163-
_ => self.parse_token_tree(),
161+
let mut is_interpolated_expr = false;
162+
if let token::Interpolated(nt) = &self.token {
163+
if let token::NtExpr(..) = **nt {
164+
is_interpolated_expr = true;
165+
}
166+
}
167+
let tokens = if is_interpolated_expr {
168+
// We need to accept arbitrary interpolated expressions to continue
169+
// supporting things like `doc = $expr` that work on stable.
170+
// Non-literal interpolated expressions are rejected after expansion.
171+
self.parse_token_tree().into()
172+
} else {
173+
self.parse_unsuffixed_lit()?.tokens()
164174
};
165-
TokenStream::new(vec![eq.into(), tree.into()])
175+
TokenStream::from_streams(vec![eq.into(), tokens])
166176
} else {
167177
TokenStream::empty()
168178
};

src/libsyntax/tokenstream.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl TokenStream {
254254
}
255255
}
256256

257-
fn from_streams(mut streams: Vec<TokenStream>) -> TokenStream {
257+
pub(crate) fn from_streams(mut streams: Vec<TokenStream>) -> TokenStream {
258258
match streams.len() {
259259
0 => TokenStream::empty(),
260260
1 => streams.pop().unwrap(),

src/test/ui/attr-eq-token-tree.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// compile-pass
2-
31
#![feature(custom_attribute, unrestricted_attribute_tokens)]
42

5-
#[my_attr = !] // OK under feature gate
3+
#[my_attr = !] //~ ERROR unexpected token: `!`
64
fn main() {}

src/test/ui/attr-eq-token-tree.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected token: `!`
2+
--> $DIR/attr-eq-token-tree.rs:3:11
3+
|
4+
LL | #[my_attr = !] //~ ERROR unexpected token: `!`
5+
| ^
6+
7+
error: aborting due to previous error
8+

src/test/ui/macros/macro-attribute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#![feature(unrestricted_attribute_tokens)]
22

3-
#[doc = $not_there] //~ ERROR expected `]`, found `not_there`
3+
#[doc = $not_there] //~ ERROR unexpected token: `$`
44
fn main() { }
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected `]`, found `not_there`
2-
--> $DIR/macro-attribute.rs:3:10
1+
error: unexpected token: `$`
2+
--> $DIR/macro-attribute.rs:3:7
33
|
4-
LL | #[doc = $not_there] //~ ERROR expected `]`, found `not_there`
5-
| ^^^^^^^^^ expected `]`
4+
LL | #[doc = $not_there] //~ ERROR unexpected token: `$`
5+
| ^
66

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(custom_attribute)]
2+
3+
macro_rules! check {
4+
($expr: expr) => (
5+
#[my_attr = $expr] //~ ERROR suffixed literals are not allowed in attributes
6+
//~| ERROR unexpected token: `-0`
7+
//~| ERROR unexpected token: `0 + 0`
8+
use main as _;
9+
);
10+
}
11+
12+
check!("0"); // OK
13+
check!(0); // OK
14+
check!(0u8); // ERROR, see above
15+
check!(-0); // ERROR, see above
16+
check!(0 + 0); // ERROR, see above
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error: suffixed literals are not allowed in attributes
2+
--> $DIR/malformed-interpolated.rs:5:21
3+
|
4+
LL | #[my_attr = $expr] //~ ERROR suffixed literals are not allowed in attributes
5+
| ^^^^^
6+
...
7+
LL | check!(0u8); // ERROR, see above
8+
| ------------ in this macro invocation
9+
|
10+
= help: instead of using a suffixed literal (1u8, 1.0f32, etc.), use an unsuffixed version (1, 1.0, etc.).
11+
12+
error: unexpected token: `-0`
13+
--> $DIR/malformed-interpolated.rs:5:19
14+
|
15+
LL | #[my_attr = $expr] //~ ERROR suffixed literals are not allowed in attributes
16+
| ^
17+
...
18+
LL | check!(-0); // ERROR, see above
19+
| ----------- in this macro invocation
20+
|
21+
= help: try enabling `#![feature(unrestricted_attribute_tokens)]`
22+
23+
error: unexpected token: `0 + 0`
24+
--> $DIR/malformed-interpolated.rs:5:19
25+
|
26+
LL | #[my_attr = $expr] //~ ERROR suffixed literals are not allowed in attributes
27+
| ^
28+
...
29+
LL | check!(0 + 0); // ERROR, see above
30+
| -------------- in this macro invocation
31+
|
32+
= help: try enabling `#![feature(unrestricted_attribute_tokens)]`
33+
34+
error: aborting due to 3 previous errors
35+
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: unexpected token: `]`
2-
--> $DIR/attr-bad-meta-2.rs:1:9
2+
--> $DIR/attr-bad-meta-2.rs:1:8
33
|
44
LL | #[path =] //~ ERROR unexpected token: `]`
5-
| ^ unexpected token after this
5+
| ^
66

77
error: aborting due to previous error
88

src/test/ui/proc-macro/proc-macro-gates.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod _test2_inner {
1919
//~| ERROR: non-builtin inner attributes are unstable
2020
}
2121

22-
#[a = y] //~ ERROR: must only be followed by a delimiter token
22+
#[a = "y"] //~ ERROR: must only be followed by a delimiter token
2323
fn _test3() {}
2424

2525
fn attrs() {

src/test/ui/proc-macro/proc-macro-gates.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ LL | #![a] //~ ERROR: custom attributes cannot be applied to modules
3333
error: custom attribute invocations must be of the form #[foo] or #[foo(..)], the macro name must only be followed by a delimiter token
3434
--> $DIR/proc-macro-gates.rs:22:1
3535
|
36-
LL | #[a = y] //~ ERROR: must only be followed by a delimiter token
37-
| ^^^^^^^^
36+
LL | #[a = "y"] //~ ERROR: must only be followed by a delimiter token
37+
| ^^^^^^^^^^
3838

3939
error[E0658]: custom attributes cannot be applied to statements (see issue #54727)
4040
--> $DIR/proc-macro-gates.rs:31:5

0 commit comments

Comments
 (0)