Skip to content

Commit 7679b59

Browse files
Wip
1 parent 3564a09 commit 7679b59

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -665,14 +665,28 @@ impl<'sess> AttributeParser<'sess, Early> {
665665
target_node_id: NodeId,
666666
features: Option<&'sess Features>,
667667
) -> Option<Attribute> {
668+
let mut parsed = Self::parse_limited_all(sess, attrs, Some(sym), target_span, target_node_id, features, ShouldEmit::Nothing);
669+
assert!(parsed.len() <= 1);
670+
parsed.pop()
671+
}
672+
673+
pub fn parse_limited_all(
674+
sess: &'sess Session,
675+
attrs: &[ast::Attribute],
676+
parse_only: Option<Symbol>,
677+
target_span: Span,
678+
target_node_id: NodeId,
679+
features: Option<&'sess Features>,
680+
emit_errors: ShouldEmit,
681+
) -> Vec<Attribute> {
668682
let mut p = Self {
669683
features,
670684
tools: Vec::new(),
671-
parse_only: Some(sym),
685+
parse_only,
672686
sess,
673-
stage: Early { emit_errors: ShouldEmit::Nothing },
687+
stage: Early { emit_errors },
674688
};
675-
let mut parsed = p.parse_attribute_list(
689+
p.parse_attribute_list(
676690
attrs,
677691
target_span,
678692
target_node_id,
@@ -681,10 +695,7 @@ impl<'sess> AttributeParser<'sess, Early> {
681695
|_lint| {
682696
panic!("can't emit lints here for now (nothing uses this atm)");
683697
},
684-
);
685-
assert!(parsed.len() <= 1);
686-
687-
parsed.pop()
698+
)
688699
}
689700

690701
pub fn parse_single<T>(

compiler/rustc_expand/src/expand.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
use std::path::PathBuf;
22
use std::rc::Rc;
33
use std::sync::Arc;
4-
use std::{iter, mem};
4+
use std::{iter, mem, slice};
55

66
use rustc_ast::mut_visit::*;
77
use rustc_ast::ptr::P;
88
use rustc_ast::tokenstream::TokenStream;
99
use rustc_ast::visit::{self, AssocCtxt, Visitor, VisitorResult, try_visit, walk_list};
10-
use rustc_ast::{
11-
self as ast, AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, DUMMY_NODE_ID,
12-
ExprKind, ForeignItemKind, HasAttrs, HasNodeId, Inline, ItemKind, MacStmtStyle, MetaItemInner,
13-
MetaItemKind, ModKind, NodeId, PatKind, StmtKind, TyKind, token,
14-
};
10+
use rustc_ast::{self as ast, AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, DUMMY_NODE_ID, ExprKind, ForeignItemKind, HasAttrs, HasNodeId, Inline, ItemKind, MacStmtStyle, MetaItemInner, MetaItemKind, ModKind, NodeId, PatKind, StmtKind, TyKind, token, CRATE_NODE_ID};
1511
use rustc_ast_pretty::pprust;
16-
use rustc_attr_parsing::{EvalConfigResult, ShouldEmit, validate_attr};
12+
use rustc_attr_parsing::{EvalConfigResult, ShouldEmit, validate_attr, AttributeParser};
1713
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1814
use rustc_errors::PResult;
1915
use rustc_feature::Features;
@@ -2129,6 +2125,15 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
21292125
attr,
21302126
self.cx.current_expansion.lint_node_id,
21312127
);
2128+
AttributeParser::parse_limited_all(
2129+
self.cx.sess,
2130+
slice::from_ref(attr),
2131+
None,
2132+
call.span(),
2133+
CRATE_NODE_ID,
2134+
Some(self.cx.ecfg.features),
2135+
ShouldEmit::ErrorsAndLints,
2136+
);
21322137

21332138
let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
21342139
span = Some(current_span);
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
//@check-pass
21
#![feature(rustc_attrs)]
32

43
#[rustc_dummy = stringify!(a)] // OK
54
macro_rules! bar {
65
() => {};
76
}
87

9-
#[rustc_dummy = stringify!(b)]
8+
// FIXME?: `bar` here expands before `stringify` has a chance to expand.
9+
// `#[rustc_dummy = ...]` is validated and dropped during expansion of `bar`,
10+
// the "attribute value must be a literal" error comes from the validation.
11+
#[rustc_dummy = stringify!(b)] //~ ERROR attribute value must be a literal
1012
bar!();
1113

12-
macro_rules! becomes_lit {
13-
() => { 5 };
14-
}
15-
16-
macro_rules! becomes_invalid_lit {
17-
() => { 1 + 1 };
18-
}
19-
20-
#[rustc_dummy = becomes_lit!()]
21-
#[rustc_dummy = becomes_invalid_lit!()]
22-
bar!();
23-
24-
fn main() {}
14+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: attribute value must be a literal
2+
--> $DIR/key-value-expansion-on-mac.rs:11:17
3+
|
4+
LL | #[rustc_dummy = stringify!(b)]
5+
| ^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)