From f9fd803ecb004291566b350f80c10fecdac320a1 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 7 Dec 2019 17:55:07 +0300 Subject: [PATCH] [experiment] Expand macros in key-value attributes --- src/librustc/hir/lowering/expr.rs | 2 +- src/librustc_parse/parser/mod.rs | 21 +++++-------------- src/test/rustdoc/external-doc.rs | 7 +++++++ src/test/ui/attr-eq-token-tree.rs | 2 +- src/test/ui/attr-eq-token-tree.stderr | 6 +++--- src/test/ui/macros/macro-attribute.rs | 2 +- src/test/ui/macros/macro-attribute.stderr | 4 ++-- .../ui/malformed/malformed-interpolated.rs | 7 +++---- .../malformed/malformed-interpolated.stderr | 20 +++++++----------- src/test/ui/parser/attr-bad-meta-2.rs | 2 +- src/test/ui/parser/attr-bad-meta-2.stderr | 4 ++-- src/test/ui/proc-macro/attributes-included.rs | 4 ++-- .../ui/proc-macro/attributes-included.stderr | 6 +++--- 13 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/librustc/hir/lowering/expr.rs b/src/librustc/hir/lowering/expr.rs index f8465baeb1305..6df21f3d4a5da 100644 --- a/src/librustc/hir/lowering/expr.rs +++ b/src/librustc/hir/lowering/expr.rs @@ -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(), } } diff --git a/src/librustc_parse/parser/mod.rs b/src/librustc_parse/parser/mod.rs index 07e99cfe01292..4b6bb2f8542af 100644 --- a/src/librustc_parse/parser/mod.rs +++ b/src/librustc_parse/parser/mod.rs @@ -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}; @@ -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 } diff --git a/src/test/rustdoc/external-doc.rs b/src/test/rustdoc/external-doc.rs index 4a13f4069c40a..9586e6daf3fc1 100644 --- a/src/test/rustdoc/external-doc.rs +++ b/src/test/rustdoc/external-doc.rs @@ -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; diff --git a/src/test/ui/attr-eq-token-tree.rs b/src/test/ui/attr-eq-token-tree.rs index c301492b9e21a..330b119772a64 100644 --- a/src/test/ui/attr-eq-token-tree.rs +++ b/src/test/ui/attr-eq-token-tree.rs @@ -1,2 +1,2 @@ -#[my_attr = !] //~ ERROR unexpected token: `!` +#[my_attr = !] //~ ERROR expected expression, found `]` fn main() {} diff --git a/src/test/ui/attr-eq-token-tree.stderr b/src/test/ui/attr-eq-token-tree.stderr index bb37c2e0cc473..1846444b668f4 100644 --- a/src/test/ui/attr-eq-token-tree.stderr +++ b/src/test/ui/attr-eq-token-tree.stderr @@ -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 diff --git a/src/test/ui/macros/macro-attribute.rs b/src/test/ui/macros/macro-attribute.rs index f580dfa8e34ed..88834a96721b9 100644 --- a/src/test/ui/macros/macro-attribute.rs +++ b/src/test/ui/macros/macro-attribute.rs @@ -1,2 +1,2 @@ -#[doc = $not_there] //~ ERROR unexpected token: `$` +#[doc = $not_there] //~ ERROR expected expression, found `$` fn main() { } diff --git a/src/test/ui/macros/macro-attribute.stderr b/src/test/ui/macros/macro-attribute.stderr index d28ce25341d3a..3316d38726487 100644 --- a/src/test/ui/macros/macro-attribute.stderr +++ b/src/test/ui/macros/macro-attribute.stderr @@ -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 diff --git a/src/test/ui/malformed/malformed-interpolated.rs b/src/test/ui/malformed/malformed-interpolated.rs index 5101b5caeea09..b962447e7ed04 100644 --- a/src/test/ui/malformed/malformed-interpolated.rs +++ b/src/test/ui/malformed/malformed-interpolated.rs @@ -2,8 +2,7 @@ macro_rules! check { ($expr: expr) => ( - #[rustc_dummy = $expr] //~ ERROR unexpected token: `-0` - //~| ERROR unexpected token: `0 + 0` + #[rustc_dummy = $expr] use main as _; ); } @@ -11,7 +10,7 @@ macro_rules! check { 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() {} diff --git a/src/test/ui/malformed/malformed-interpolated.stderr b/src/test/ui/malformed/malformed-interpolated.stderr index bcd2ef545d815..2a1112c4a98c4 100644 --- a/src/test/ui/malformed/malformed-interpolated.stderr +++ b/src/test/ui/malformed/malformed-interpolated.stderr @@ -1,5 +1,5 @@ error: suffixed literals are not allowed in attributes - --> $DIR/malformed-interpolated.rs:13:8 + --> $DIR/malformed-interpolated.rs:12:8 | LL | check!(0u8); | ^^^ @@ -7,22 +7,16 @@ 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 diff --git a/src/test/ui/parser/attr-bad-meta-2.rs b/src/test/ui/parser/attr-bad-meta-2.rs index cefd3369742be..db612ed883d7f 100644 --- a/src/test/ui/parser/attr-bad-meta-2.rs +++ b/src/test/ui/parser/attr-bad-meta-2.rs @@ -1,2 +1,2 @@ -#[path =] //~ ERROR unexpected token: `]` +#[path =] //~ ERROR expected expression, found `]` mod m {} diff --git a/src/test/ui/parser/attr-bad-meta-2.stderr b/src/test/ui/parser/attr-bad-meta-2.stderr index 2d772dae69125..6fc6fb665a807 100644 --- a/src/test/ui/parser/attr-bad-meta-2.stderr +++ b/src/test/ui/parser/attr-bad-meta-2.stderr @@ -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 diff --git a/src/test/ui/proc-macro/attributes-included.rs b/src/test/ui/proc-macro/attributes-included.rs index 4769607ff395d..12dc07784e62e 100644 --- a/src/test/ui/proc-macro/attributes-included.rs +++ b/src/test/ui/proc-macro/attributes-included.rs @@ -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() { diff --git a/src/test/ui/proc-macro/attributes-included.stderr b/src/test/ui/proc-macro/attributes-included.stderr index 0f74f45e102f7..c0fb2e489e34d 100644 --- a/src/test/ui/proc-macro/attributes-included.stderr +++ b/src/test/ui/proc-macro/attributes-included.stderr @@ -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