Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[experiment] Expand macros in inert key-value attributes #67121

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/hir/lowering/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl LoweringContext<'_> {
hir_id: self.lower_node_id(e.id),
kind,
span: e.span,
attrs: e.attrs.clone(),
attrs: self.lower_attrs_extendable(&e.attrs).into(),
}
}

Expand Down
21 changes: 5 additions & 16 deletions src/librustc_parse/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{Directory, DirectoryOwnership};
use crate::lexer::UnmatchedBrace;

use rustc_errors::{PResult, Applicability, DiagnosticBuilder, FatalError};
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::thin_vec::ThinVec;
use syntax::ast::{self, DUMMY_NODE_ID, AttrStyle, Attribute, CrateSugar, Extern, Ident, StrLit};
use syntax::ast::{IsAsync, MacArgs, MacDelimiter, Mutability, Visibility, VisibilityKind, Unsafety};
Expand Down Expand Up @@ -1028,22 +1029,10 @@ impl<'a> Parser<'a> {
} else if !delimited_only {
if self.eat(&token::Eq) {
let eq_span = self.prev_span;
let mut is_interpolated_expr = false;
if let token::Interpolated(nt) = &self.token.kind {
if let token::NtExpr(..) = **nt {
is_interpolated_expr = true;
}
}
let token_tree = if is_interpolated_expr {
// We need to accept arbitrary interpolated expressions to continue
// supporting things like `doc = $expr` that work on stable.
// Non-literal interpolated expressions are rejected after expansion.
self.parse_token_tree()
} else {
self.parse_unsuffixed_lit()?.token_tree()
};

MacArgs::Eq(eq_span, token_tree.into())
let expr = self.parse_expr()?;
let span = expr.span;
let kind = token::Interpolated(Lrc::new(token::NtExpr(expr)));
MacArgs::Eq(eq_span, TokenTree::token(kind, span).into())
} else {
MacArgs::Empty
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/rustdoc/external-doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@
#[doc(include = "auxiliary/external-doc.md")]
/// ## Inline Docs
pub struct CanHasDocs;

// @has external_doc/struct.IncludeStrDocs.html
// @has - '//h1' 'External Docs'
// @has - '//h2' 'Inline Docs'
#[doc = include_str!("auxiliary/external-doc.md")]
/// ## Inline Docs
pub struct IncludeStrDocs;
2 changes: 1 addition & 1 deletion src/test/ui/attr-eq-token-tree.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#[my_attr = !] //~ ERROR unexpected token: `!`
#[my_attr = !] //~ ERROR expected expression, found `]`
fn main() {}
6 changes: 3 additions & 3 deletions src/test/ui/attr-eq-token-tree.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: unexpected token: `!`
--> $DIR/attr-eq-token-tree.rs:1:13
error: expected expression, found `]`
--> $DIR/attr-eq-token-tree.rs:1:14
|
LL | #[my_attr = !]
| ^
| ^ expected expression

error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/macros/macro-attribute.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#[doc = $not_there] //~ ERROR unexpected token: `$`
#[doc = $not_there] //~ ERROR expected expression, found `$`
fn main() { }
4 changes: 2 additions & 2 deletions src/test/ui/macros/macro-attribute.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: unexpected token: `$`
error: expected expression, found `$`
--> $DIR/macro-attribute.rs:1:9
|
LL | #[doc = $not_there]
| ^
| ^ expected expression

error: aborting due to previous error

7 changes: 3 additions & 4 deletions src/test/ui/malformed/malformed-interpolated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

macro_rules! check {
($expr: expr) => (
#[rustc_dummy = $expr] //~ ERROR unexpected token: `-0`
//~| ERROR unexpected token: `0 + 0`
#[rustc_dummy = $expr]
use main as _;
);
}

check!("0"); // OK
check!(0); // OK
check!(0u8); //~ ERROR suffixed literals are not allowed in attributes
check!(-0); // ERROR, see above
check!(0 + 0); // ERROR, see above
check!(-0); //~ ERROR unexpected token: `-0`
check!(0 + 0); //~ ERROR unexpected token: `0 + 0`

fn main() {}
20 changes: 7 additions & 13 deletions src/test/ui/malformed/malformed-interpolated.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
error: suffixed literals are not allowed in attributes
--> $DIR/malformed-interpolated.rs:13:8
--> $DIR/malformed-interpolated.rs:12:8
|
LL | check!(0u8);
| ^^^
|
= help: instead of using a suffixed literal (1u8, 1.0f32, etc.), use an unsuffixed version (1, 1.0, etc.).

error: unexpected token: `-0`
--> $DIR/malformed-interpolated.rs:5:25
--> $DIR/malformed-interpolated.rs:13:8
|
LL | #[rustc_dummy = $expr]
| ^^^^^
...
LL | check!(-0); // ERROR, see above
| ----------- in this macro invocation
LL | check!(-0);
| ^^

error: unexpected token: `0 + 0`
--> $DIR/malformed-interpolated.rs:5:25
--> $DIR/malformed-interpolated.rs:14:8
|
LL | #[rustc_dummy = $expr]
| ^^^^^
...
LL | check!(0 + 0); // ERROR, see above
| -------------- in this macro invocation
LL | check!(0 + 0);
| ^^^^^

error: aborting due to 3 previous errors

2 changes: 1 addition & 1 deletion src/test/ui/parser/attr-bad-meta-2.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#[path =] //~ ERROR unexpected token: `]`
#[path =] //~ ERROR expected expression, found `]`
mod m {}
4 changes: 2 additions & 2 deletions src/test/ui/parser/attr-bad-meta-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: unexpected token: `]`
error: expected expression, found `]`
--> $DIR/attr-bad-meta-2.rs:1:9
|
LL | #[path =]
| ^
| ^ expected expression

error: aborting due to previous error

4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/attributes-included.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use attributes_included::*;
#[bar]
#[inline]
/// doc
#[foo]
#[foo] //~ WARN unused variable: `a`
#[inline]
/// doc
fn foo() {
let a: i32 = "foo"; //~ WARN: unused variable
let a: i32 = "foo";
}

fn main() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/proc-macro/attributes-included.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
warning: unused variable: `a`
--> $DIR/attributes-included.rs:17:9
--> $DIR/attributes-included.rs:13:1
|
LL | let a: i32 = "foo";
| ^ help: consider prefixing with an underscore: `_a`
LL | #[foo]
| ^^^^^^ help: consider prefixing with an underscore: `_a`
|
note: lint level defined here
--> $DIR/attributes-included.rs:4:9
Expand Down