Skip to content

Commit 4796551

Browse files
committed
wip
1 parent c1a6887 commit 4796551

File tree

23 files changed

+67
-34
lines changed

23 files changed

+67
-34
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,7 @@ pub struct Expr {
11671167
pub span: Span,
11681168
pub attrs: AttrVec,
11691169
pub tokens: Option<LazyAttrTokenStream>,
1170+
pub metavar_source_span: Option<Span>,
11701171
}
11711172

11721173
impl Expr {
@@ -3379,7 +3380,7 @@ mod size_asserts {
33793380
static_assert_size!(AssocItemKind, 16);
33803381
static_assert_size!(Attribute, 32);
33813382
static_assert_size!(Block, 32);
3382-
static_assert_size!(Expr, 72);
3383+
static_assert_size!(Expr, 80);
33833384
static_assert_size!(ExprKind, 40);
33843385
static_assert_size!(Fn, 160);
33853386
static_assert_size!(ForeignItem, 96);

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ pub fn mk_attr_name_value_str(
607607
span,
608608
attrs: AttrVec::new(),
609609
tokens: None,
610+
metavar_source_span: None,
610611
});
611612
let path = Path::from_ident(Ident::new(name, span));
612613
let args = AttrArgs::Eq(span, AttrArgsEq::Ast(expr));

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ fn noop_visit_format_args<T: MutVisitor>(fmt: &mut FormatArgs, vis: &mut T) {
13851385
}
13861386

13871387
pub fn noop_visit_expr<T: MutVisitor>(
1388-
Expr { kind, id, span, attrs, tokens }: &mut Expr,
1388+
Expr { kind, id, span, attrs, tokens, metavar_source_span }: &mut Expr,
13891389
vis: &mut T,
13901390
) {
13911391
match kind {
@@ -1562,6 +1562,9 @@ pub fn noop_visit_expr<T: MutVisitor>(
15621562
}
15631563
vis.visit_id(id);
15641564
vis.visit_span(span);
1565+
if let Some(metavar_source_span) = metavar_source_span {
1566+
vis.visit_span(metavar_source_span);
1567+
}
15651568
visit_attrs(attrs, vis);
15661569
visit_lazy_tts(tokens, vis);
15671570
}
@@ -1678,6 +1681,7 @@ impl DummyAstNode for Expr {
16781681
span: Default::default(),
16791682
attrs: Default::default(),
16801683
tokens: Default::default(),
1684+
metavar_source_span: None,
16811685
}
16821686
}
16831687
}

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ pub enum TokenKind {
333333
/// - It prevents `Token` from implementing `Copy`.
334334
/// It adds complexity and likely slows things down. Please don't add new
335335
/// occurrences of this token kind!
336-
Interpolated(Lrc<(Nonterminal, Span)>),
336+
Interpolated(Lrc<(Nonterminal, Span, Option<Span>)>),
337337

338338
/// A doc comment token.
339339
/// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ast_lowering_abi_specified_multiple_times =
55
66
ast_lowering_arbitrary_expression_in_pattern =
77
arbitrary expressions aren't allowed in patterns
8-
.pattern_from_macro_note = maybe the pattern comes from a macro meta variable?
8+
.pattern_from_macro_note = the expression looks like a pattern but it comes from an `:expr` meta variable
99
1010
ast_lowering_argument = argument
1111

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
219219
span: *op_sp,
220220
attrs: AttrVec::new(),
221221
tokens: None,
222+
metavar_source_span: None,
222223
};
223224

224225
// Wrap the expression in an AnonConst.

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ pub struct ArbitraryExpressionInPattern {
369369
#[primary_span]
370370
pub span: Span,
371371
#[note(ast_lowering_pattern_from_macro_note)]
372-
pub pattern_from_macro_note: bool,
372+
pub pattern_from_macro_note: Option<Span>,
373373
}
374374

375375
#[derive(Diagnostic)]

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11791179
span,
11801180
attrs: AttrVec::new(),
11811181
tokens: None,
1182+
metavar_source_span: None,
11821183
};
11831184

11841185
let ct = self.with_new_scopes(span, |this| hir::AnonConst {

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
339339
ExprKind::Path(..) if allow_paths => {}
340340
ExprKind::Unary(UnOp::Neg, inner) if matches!(inner.kind, ExprKind::Lit(_)) => {}
341341
_ => {
342-
let marks = expr.span.ctxt().marks().len() > 0;
342+
//tracing::error!("tokens: {:?}", expr.tokens);
343+
let marks = expr.metavar_source_span;
343344
let pattern_from_macro =
344-
expr.is_approximately_pattern() && marks;
345+
expr.is_approximately_pattern().then_some(marks).flatten();
345346
let guar = self.dcx().emit_err(ArbitraryExpressionInPattern {
346347
span: expr.span,
347348
pattern_from_macro_note: pattern_from_macro,

compiler/rustc_builtin_macros/src/asm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ pub(super) fn expand_asm<'cx>(
760760
span: sp,
761761
attrs: ast::AttrVec::new(),
762762
tokens: None,
763+
metavar_source_span: None,
763764
}),
764765
Err(guar) => DummyResult::raw_expr(sp, Some(guar)),
765766
};

compiler/rustc_builtin_macros/src/concat_idents.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub fn expand_concat_idents<'cx>(
5555
span: self.ident.span,
5656
attrs: AttrVec::new(),
5757
tokens: None,
58+
metavar_source_span: None,
5859
}))
5960
}
6061

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ impl DummyResult {
572572
span: sp,
573573
attrs: ast::AttrVec::new(),
574574
tokens: None,
575+
metavar_source_span: None,
575576
})
576577
}
577578

compiler/rustc_expand/src/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl<'a> ExtCtxt<'a> {
9696
span,
9797
attrs: AttrVec::new(),
9898
tokens: None,
99+
metavar_source_span: None,
99100
}),
100101
}
101102
}
@@ -264,7 +265,7 @@ impl<'a> ExtCtxt<'a> {
264265
}
265266

266267
pub fn expr(&self, span: Span, kind: ast::ExprKind) -> P<ast::Expr> {
267-
P(ast::Expr { id: ast::DUMMY_NODE_ID, kind, span, attrs: AttrVec::new(), tokens: None })
268+
P(ast::Expr { id: ast::DUMMY_NODE_ID, kind, span, attrs: AttrVec::new(), tokens: None, metavar_source_span: None })
268269
}
269270

270271
pub fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::mbe::{self, KleeneOp, MetaVarExpr};
88
use rustc_ast::mut_visit::{self, MutVisitor};
99
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
1010
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
11-
use rustc_data_structures::fx::FxHashMap;
11+
use rustc_data_structures::{fx::FxHashMap, sync::Lrc};
1212
use rustc_errors::{pluralize, Diag, PResult};
1313
use rustc_parse::parser::ParseNtResult;
1414
use rustc_span::hygiene::{LocalExpnId, Transparency};
@@ -249,7 +249,7 @@ pub(super) fn transcribe<'a>(
249249
// Find the matched nonterminal from the macro invocation, and use it to replace
250250
// the meta-var.
251251
let ident = MacroRulesNormalizedIdent::new(original_ident);
252-
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
252+
if let Some((cur_matched, cur_matched_ident)) = lookup_cur_matched(ident, interp, &repeats) {
253253
let tt = match cur_matched {
254254
MatchedSingle(ParseNtResult::Tt(tt)) => {
255255
// `tt`s are emitted into the output stream directly as "raw tokens",
@@ -261,7 +261,9 @@ pub(super) fn transcribe<'a>(
261261
// `Delimiter::Invisible` to maintain parsing priorities.
262262
// `Interpolated` is currently used for such groups in rustc parser.
263263
marker.visit_span(&mut sp);
264-
TokenTree::token_alone(token::Interpolated(nt.clone()), sp)
264+
//tracing::error!("transcribe: {:}: {:?}", original_ident, nt.0);
265+
let nonterminal = Lrc::new((nt.0.clone(), nt.1, Some(cur_matched_ident.ident().span)));
266+
TokenTree::token_alone(token::Interpolated(nonterminal), sp)
265267
}
266268
MatchedSeq(..) => {
267269
// We were unable to descend far enough. This is an error.
@@ -419,16 +421,16 @@ fn lookup_cur_matched<'a>(
419421
ident: MacroRulesNormalizedIdent,
420422
interpolations: &'a FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
421423
repeats: &[(usize, usize)],
422-
) -> Option<&'a NamedMatch> {
423-
interpolations.get(&ident).map(|mut matched| {
424+
) -> Option<(&'a NamedMatch, &'a MacroRulesNormalizedIdent)> {
425+
interpolations.get_key_value(&ident).map(|(ident, mut matched)| {
424426
for &(idx, _) in repeats {
425427
match matched {
426428
MatchedSingle(_) => break,
427429
MatchedSeq(ads) => matched = ads.get(idx).unwrap(),
428430
}
429431
}
430432

431-
matched
433+
(matched, ident)
432434
})
433435
}
434436

@@ -512,7 +514,7 @@ fn lockstep_iter_size(
512514
TokenTree::MetaVar(_, name) | TokenTree::MetaVarDecl(_, name, _) => {
513515
let name = MacroRulesNormalizedIdent::new(*name);
514516
match lookup_cur_matched(name, interpolations, repeats) {
515-
Some(matched) => match matched {
517+
Some((matched, _)) => match matched {
516518
MatchedSingle(_) => LockstepIterSize::Unconstrained,
517519
MatchedSeq(ads) => LockstepIterSize::Constraint(ads.len(), name),
518520
},
@@ -526,7 +528,7 @@ fn lockstep_iter_size(
526528
};
527529
let name = MacroRulesNormalizedIdent::new(ident);
528530
match lookup_cur_matched(name, interpolations, repeats) {
529-
Some(MatchedSeq(ads)) => {
531+
Some((MatchedSeq(ads), _)) => {
530532
default_rslt.with(LockstepIterSize::Constraint(ads.len(), name))
531533
}
532534
_ => default_rslt,

compiler/rustc_expand/src/placeholders.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn placeholder(
4040
attrs: ast::AttrVec::new(),
4141
kind: ast::ExprKind::MacCall(mac_placeholder()),
4242
tokens: None,
43+
metavar_source_span: None,
4344
})
4445
};
4546
let ty =

compiler/rustc_expand/src/proc_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl MultiItemModifier for DeriveProcMacro {
127127
Annotatable::Stmt(stmt) => token::NtStmt(stmt),
128128
_ => unreachable!(),
129129
};
130-
TokenStream::token_alone(token::Interpolated(Lrc::new((nt, span))), DUMMY_SP)
130+
TokenStream::token_alone(token::Interpolated(Lrc::new((nt, span, None))), DUMMY_SP)
131131
} else {
132132
item.to_tokens()
133133
};

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl RecoverQPath for Expr {
113113
attrs: AttrVec::new(),
114114
id: ast::DUMMY_NODE_ID,
115115
tokens: None,
116+
metavar_source_span: None,
116117
}
117118
}
118119
}

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ macro_rules! maybe_whole_expr {
4747
if let token::Interpolated(nt) = &$p.token.kind {
4848
match &nt.0 {
4949
token::NtExpr(e) | token::NtLiteral(e) => {
50-
let e = e.clone();
50+
let mut e = e.clone();
51+
let metavar_source_span = nt.2;
5152
$p.bump();
53+
e.metavar_source_span = metavar_source_span;
5254
return Ok(e);
5355
}
5456
token::NtPath(path) => {
@@ -3818,7 +3820,7 @@ impl<'a> Parser<'a> {
38183820
}
38193821

38203822
pub(crate) fn mk_expr_with_attrs(&self, span: Span, kind: ExprKind, attrs: AttrVec) -> P<Expr> {
3821-
P(Expr { kind, span, attrs, id: DUMMY_NODE_ID, tokens: None })
3823+
P(Expr { kind, span, attrs, id: DUMMY_NODE_ID, tokens: None, metavar_source_span: None })
38223824
}
38233825

38243826
pub(crate) fn mk_expr(&self, span: Span, kind: ExprKind) -> P<Expr> {

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,9 @@ impl MacroRulesNormalizedIdent {
22032203
pub fn new(ident: Ident) -> Self {
22042204
Self(ident.normalize_to_macro_rules())
22052205
}
2206+
pub fn ident(&self) -> Ident {
2207+
self.0
2208+
}
22062209
}
22072210

22082211
impl fmt::Debug for MacroRulesNormalizedIdent {

tests/ui/lowering/issue-99380.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error: arbitrary expressions aren't allowed in patterns
33
|
44
LL | foo!(Some(3));
55
| ^^^^^^^
6+
|
7+
note: the expression looks like a pattern but it comes from an `:expr` meta variable
8+
--> $DIR/issue-99380.rs:2:7
9+
|
10+
LL | ($p:expr) => {
11+
| ^
612

713
error: aborting due to 1 previous error
814

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +0,0 @@
1-
error: arbitrary expressions aren't allowed in patterns
2-
--> $DIR/vec-macro-in-pattern.rs:7:14
3-
|
4-
LL | Some(vec![43]) => {}
5-
| ^^^^^^^^
6-
|
7-
= note: maybe the pattern comes from a macro meta variable?
8-
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
9-
10-
error: aborting due to 1 previous error
11-

tests/ui/match/expr_before_ident_pat.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ error: arbitrary expressions aren't allowed in patterns
1010
LL | funny!(a, a);
1111
| ^
1212
|
13-
= note: maybe the pattern comes from a macro meta variable?
13+
note: the expression looks like a pattern but it comes from an `:expr` meta variable
14+
--> $DIR/expr_before_ident_pat.rs:2:7
15+
|
16+
LL | ($a:expr, $b:ident) => {
17+
| ^
1418

1519
error: aborting due to 2 previous errors
1620

tests/ui/pattern/issue-92074-macro-ice.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ LL | () => { force_expr!(Vec::new()) }
77
LL | assert!(matches!(x, En::A(make_vec!())));
88
| ----------- in this macro invocation
99
|
10-
= note: maybe the pattern comes from a macro meta variable?
10+
note: the expression looks like a pattern but it comes from an `:expr` meta variable
11+
--> $DIR/issue-92074-macro-ice.rs:10:7
12+
|
13+
LL | ($e:expr) => { $e }
14+
| ^
1115
= note: this error originates in the macro `make_vec` (in Nightly builds, run with -Z macro-backtrace for more info)
1216

1317
error: arbitrary expressions aren't allowed in patterns
@@ -19,7 +23,11 @@ LL | () => { force_pat!(get_usize(), get_usize()) }
1923
LL | assert!(matches!(5, make_pat!()));
2024
| ----------- in this macro invocation
2125
|
22-
= note: maybe the pattern comes from a macro meta variable?
26+
note: the expression looks like a pattern but it comes from an `:expr` meta variable
27+
--> $DIR/issue-92074-macro-ice.rs:14:7
28+
|
29+
LL | ($a:expr, $b:expr) => { $a..=$b }
30+
| ^
2331
= note: this error originates in the macro `make_pat` (in Nightly builds, run with -Z macro-backtrace for more info)
2432

2533
error: arbitrary expressions aren't allowed in patterns
@@ -31,7 +39,11 @@ LL | () => { force_pat!(get_usize(), get_usize()) }
3139
LL | assert!(matches!(5, make_pat!()));
3240
| ----------- in this macro invocation
3341
|
34-
= note: maybe the pattern comes from a macro meta variable?
42+
note: the expression looks like a pattern but it comes from an `:expr` meta variable
43+
--> $DIR/issue-92074-macro-ice.rs:14:16
44+
|
45+
LL | ($a:expr, $b:expr) => { $a..=$b }
46+
| ^
3547
= note: this error originates in the macro `make_pat` (in Nightly builds, run with -Z macro-backtrace for more info)
3648

3749
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)