Skip to content

Commit 4574033

Browse files
committed
Remove NtPat.
XXX: tests/ui/macros/trace_faulty_macros.rs regresses because I haven't fixed `expected_expression_found` properly yet.
1 parent f1a2ea3 commit 4574033

18 files changed

+75
-56
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ impl HasTokens for Nonterminal {
234234
Nonterminal::NtItem(item) => item.tokens(),
235235
Nonterminal::NtStmt(stmt) => stmt.tokens(),
236236
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
237-
Nonterminal::NtPat(pat) => pat.tokens(),
238237
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
239238
Nonterminal::NtPath(path) => path.tokens(),
240239
Nonterminal::NtBlock(block) => block.tokens(),
@@ -245,7 +244,6 @@ impl HasTokens for Nonterminal {
245244
Nonterminal::NtItem(item) => item.tokens_mut(),
246245
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
247246
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
248-
Nonterminal::NtPat(pat) => pat.tokens_mut(),
249247
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
250248
Nonterminal::NtPath(path) => path.tokens_mut(),
251249
Nonterminal::NtBlock(block) => block.tokens_mut(),

compiler/rustc_ast/src/mut_visit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,6 @@ fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
835835
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
836836
})
837837
}),
838-
token::NtPat(pat) => vis.visit_pat(pat),
839838
token::NtExpr(expr) => vis.visit_expr(expr),
840839
token::NtLiteral(expr) => vis.visit_expr(expr),
841840
token::NtMeta(item) => {

compiler/rustc_ast/src/token.rs

-5
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,6 @@ impl Token {
561561
| Lt | BinOp(Shl) // associated path
562562
| PathSep => true, // global path
563563
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) |
564-
NtPat(..) |
565564
NtBlock(..) |
566565
NtPath(..)),
567566
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
@@ -922,7 +921,6 @@ pub enum Nonterminal {
922921
NtItem(P<ast::Item>),
923922
NtBlock(P<ast::Block>),
924923
NtStmt(P<ast::Stmt>),
925-
NtPat(P<ast::Pat>),
926924
NtExpr(P<ast::Expr>),
927925
NtLiteral(P<ast::Expr>),
928926
/// Stuff inside brackets for attributes
@@ -1014,7 +1012,6 @@ impl Nonterminal {
10141012
NtItem(item) => item.span,
10151013
NtBlock(block) => block.span,
10161014
NtStmt(stmt) => stmt.span,
1017-
NtPat(pat) => pat.span,
10181015
NtExpr(expr) | NtLiteral(expr) => expr.span,
10191016
NtMeta(attr_item) => attr_item.span(),
10201017
NtPath(path) => path.span,
@@ -1026,7 +1023,6 @@ impl Nonterminal {
10261023
NtItem(..) => "item",
10271024
NtBlock(..) => "block",
10281025
NtStmt(..) => "statement",
1029-
NtPat(..) => "pattern",
10301026
NtExpr(..) => "expression",
10311027
NtLiteral(..) => "literal",
10321028
NtMeta(..) => "attribute",
@@ -1051,7 +1047,6 @@ impl fmt::Debug for Nonterminal {
10511047
NtItem(..) => f.pad("NtItem(..)"),
10521048
NtBlock(..) => f.pad("NtBlock(..)"),
10531049
NtStmt(..) => f.pad("NtStmt(..)"),
1054-
NtPat(..) => f.pad("NtPat(..)"),
10551050
NtExpr(..) => f.pad("NtExpr(..)"),
10561051
NtLiteral(..) => f.pad("NtLiteral(..)"),
10571052
NtMeta(..) => f.pad("NtMeta(..)"),

compiler/rustc_ast/src/tokenstream.rs

-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ impl TokenStream {
473473
TokenStream::token_alone(token::Semi, stmt.span)
474474
}
475475
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
476-
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
477476
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
478477
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
479478
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),

compiler/rustc_ast_pretty/src/pprust/state.rs

-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
859859
token::NtItem(e) => self.item_to_string(e),
860860
token::NtBlock(e) => self.block_to_string(e),
861861
token::NtStmt(e) => self.stmt_to_string(e),
862-
token::NtPat(e) => self.pat_to_string(e),
863862
token::NtLiteral(e) => self.expr_to_string(e),
864863
}
865864
}

compiler/rustc_expand/src/mbe/transcribe.rs

+7
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@ pub(super) fn transcribe<'a>(
283283
let kind = token::NtLifetime(*ident);
284284
TokenTree::token_alone(kind, sp)
285285
}
286+
MatchedSingle(ParseNtResult::PatParam(ref pat, inferred)) => mk_delimited(
287+
NonterminalKind::PatParam { inferred: *inferred },
288+
TokenStream::from_ast(pat),
289+
),
290+
MatchedSingle(ParseNtResult::PatWithOr(ref pat)) => {
291+
mk_delimited(NonterminalKind::PatWithOr, TokenStream::from_ast(pat))
292+
}
286293
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
287294
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
288295
}

compiler/rustc_parse/src/parser/diagnostics.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ use ast::token::IdentIsRaw;
2525
use rustc_ast as ast;
2626
use rustc_ast::ptr::P;
2727
use rustc_ast::token::{self, Delimiter, Lit, LitKind, Token, TokenKind};
28-
use rustc_ast::tokenstream::AttrTokenTree;
2928
use rustc_ast::util::parser::AssocOp;
3029
use rustc_ast::{
3130
AngleBracketedArg, AngleBracketedArgs, AnonConst, AttrVec, BinOpKind, BindingMode, Block,
32-
BlockCheckMode, Expr, ExprKind, GenericArg, Generics, HasTokens, Item, ItemKind, Param, Pat,
33-
PatKind, Path, PathSegment, QSelf, Recovered, Ty, TyKind,
31+
BlockCheckMode, Expr, ExprKind, GenericArg, Generics, Item, ItemKind, Param, Pat, PatKind,
32+
Path, PathSegment, QSelf, Recovered, Ty, TyKind,
3433
};
3534
use rustc_ast_pretty::pprust;
3635
use rustc_data_structures::fx::FxHashSet;
@@ -2366,6 +2365,8 @@ impl<'a> Parser<'a> {
23662365
}
23672366
err.span_label(span, "expected expression");
23682367

2368+
/* njn: temp disabled, which hurts tests/ui/macros/trace_faulty_macros.rs
2369+
23692370
// Walk the chain of macro expansions for the current token to point at how the original
23702371
// code was interpreted. This helps the user realize when a macro argument of one type is
23712372
// later reinterpreted as a different type, like `$x:expr` being reinterpreted as `$x:pat`
@@ -2411,6 +2412,7 @@ impl<'a> Parser<'a> {
24112412
tokens",
24122413
);
24132414
}
2415+
*/
24142416
err
24152417
}
24162418

compiler/rustc_parse/src/parser/item.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::maybe_whole;
99
use ast::token::IdentIsRaw;
1010
use rustc_ast::ast::*;
1111
use rustc_ast::ptr::P;
12-
use rustc_ast::token::{self, Delimiter, TokenKind};
12+
use rustc_ast::token::{self, Delimiter, InvisibleOrigin, NonterminalKind, TokenKind};
1313
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
1414
use rustc_ast::util::case::Case;
1515
use rustc_ast::{self as ast};
@@ -2856,8 +2856,10 @@ impl<'a> Parser<'a> {
28562856

28572857
fn is_named_param(&self) -> bool {
28582858
let offset = match &self.token.kind {
2859-
token::Interpolated(nt) => match &**nt {
2860-
token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
2859+
token::OpenDelim(Delimiter::Invisible(origin)) => match origin {
2860+
InvisibleOrigin::MetaVar(
2861+
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr,
2862+
) => return self.check_noexpect_past_close_delim(&token::Colon),
28612863
_ => 0,
28622864
},
28632865
token::BinOp(token::And) | token::AndAnd => 1,

compiler/rustc_parse/src/parser/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,8 @@ pub enum ParseNtResult {
17131713
Tt(TokenTree),
17141714
Ident(Ident, IdentIsRaw),
17151715
Lifetime(Ident),
1716+
PatParam(P<ast::Pat>, /* inferred */ bool),
1717+
PatWithOr(P<ast::Pat>),
17161718
Ty(P<ast::Ty>),
17171719
Vis(P<ast::Visibility>),
17181720

compiler/rustc_parse/src/parser/nonterminal.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ impl<'a> Parser<'a> {
4747
fn nt_may_be_ident(nt: &Nonterminal) -> bool {
4848
match nt {
4949
NtStmt(_)
50-
| NtPat(_)
5150
| NtExpr(_)
5251
| NtLiteral(_) // `true`, `false`
5352
| NtMeta(_)
@@ -83,7 +82,7 @@ impl<'a> Parser<'a> {
8382
token::NtLifetime(..) => true,
8483
token::Interpolated(nt) => match &**nt {
8584
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
86-
NtItem(_) | NtPat(_) | NtMeta(_) | NtPath(_) => false,
85+
NtItem(_) | NtMeta(_) | NtPath(_) => false,
8786
},
8887
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
8988
NonterminalKind::Block
@@ -175,19 +174,22 @@ impl<'a> Parser<'a> {
175174
.create_err(UnexpectedNonterminal::Statement(self.token.span)));
176175
}
177176
},
178-
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr => {
179-
NtPat(self.collect_tokens_no_attrs(|this| match kind {
180-
NonterminalKind::PatParam { .. } => this.parse_pat_no_top_alt(None, None),
181-
NonterminalKind::PatWithOr => this.parse_pat_allow_top_alt(
177+
NonterminalKind::PatParam { inferred } => {
178+
return Ok(ParseNtResult::PatParam(
179+
self.collect_tokens_no_attrs(|this| this.parse_pat_no_top_alt(None, None))?,
180+
inferred,
181+
));
182+
}
183+
NonterminalKind::PatWithOr => {
184+
return Ok(ParseNtResult::PatWithOr(self.collect_tokens_no_attrs(|this| {
185+
this.parse_pat_allow_top_alt(
182186
None,
183187
RecoverComma::No,
184188
RecoverColon::No,
185189
CommaRecoveryMode::EitherTupleOrPipe,
186-
),
187-
_ => unreachable!(),
188-
})?)
190+
)
191+
})?));
189192
}
190-
191193
NonterminalKind::Expr => NtExpr(self.parse_expr_force_collect()?),
192194
NonterminalKind::Literal => {
193195
// The `:literal` matcher does not support attributes

compiler/rustc_parse/src/parser/pat.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use super::{ForceCollect, Parser, PathStyle, Restrictions, Trailing, TrailingToken};
1+
use super::{
2+
ForceCollect, ParseNtResult, Parser, PathStyle, Restrictions, Trailing, TrailingToken,
3+
};
24
use crate::errors::{
35
self, AmbiguousRangePattern, DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed,
46
DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt,
@@ -11,10 +13,10 @@ use crate::errors::{
1113
UnexpectedVertVertInPattern,
1214
};
1315
use crate::parser::expr::could_be_unclosed_char_literal;
14-
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
16+
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_reparse_metavar_seq};
1517
use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor};
1618
use rustc_ast::ptr::P;
17-
use rustc_ast::token::{self, BinOpToken, Delimiter, Token};
19+
use rustc_ast::token::{self, BinOpToken, Delimiter, NonterminalKind, Token};
1820
use rustc_ast::{
1921
self as ast, AttrVec, BindingMode, ByRef, Expr, ExprKind, MacCall, Mutability, Pat, PatField,
2022
PatFieldsRest, PatKind, Path, QSelf, RangeEnd, RangeSyntax,
@@ -435,7 +437,26 @@ impl<'a> Parser<'a> {
435437
syntax_loc: Option<PatternLocation>,
436438
) -> PResult<'a, P<Pat>> {
437439
maybe_recover_from_interpolated_ty_qpath!(self, true);
438-
maybe_whole!(self, NtPat, |pat| pat);
440+
441+
// Must try both kinds of pattern nonterminals.
442+
if let Some(pat) = maybe_reparse_metavar_seq!(
443+
self,
444+
NonterminalKind::PatParam { inferred },
445+
NonterminalKind::PatParam { inferred },
446+
ParseNtResult::PatParam(pat, _),
447+
pat
448+
) {
449+
return Ok(pat);
450+
}
451+
if let Some(pat) = maybe_reparse_metavar_seq!(
452+
self,
453+
NonterminalKind::PatWithOr,
454+
NonterminalKind::PatWithOr,
455+
ParseNtResult::PatWithOr(pat),
456+
pat
457+
) {
458+
return Ok(pat);
459+
}
439460

440461
let mut lo = self.token.span;
441462

@@ -756,10 +777,10 @@ impl<'a> Parser<'a> {
756777
self.recover_additional_muts();
757778

758779
// Make sure we don't allow e.g. `let mut $p;` where `$p:pat`.
759-
if let token::Interpolated(nt) = &self.token.kind {
760-
if let token::NtPat(..) = &**nt {
761-
self.expected_ident_found_err().emit();
762-
}
780+
if let Some(NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr) =
781+
self.token.is_metavar_seq()
782+
{
783+
self.expected_ident_found_err().emit();
763784
}
764785

765786
// Parse the pattern we hope to be an identifier.

tests/ui/macros/stringify.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ fn test_pat() {
577577
// PatKind::Struct
578578
c1!(pat, [ Struct {} ], "Struct {}");
579579
c1!(pat, [ Struct::<u8> {} ], "Struct::<u8> {}");
580-
c2!(pat, [ Struct ::< u8 > {} ], "Struct::<u8> {}", "Struct ::< u8 > {}");
580+
c1!(pat, [ Struct ::< u8 > {} ], "Struct ::< u8 > {}");
581581
c1!(pat, [ Struct::<'static> {} ], "Struct::<'static> {}");
582582
c1!(pat, [ Struct { x } ], "Struct { x }");
583583
c1!(pat, [ Struct { x: _x } ], "Struct { x: _x }");
@@ -597,8 +597,8 @@ fn test_pat() {
597597

598598
// PatKind::Or
599599
c1!(pat, [ true | false ], "true | false");
600-
c2!(pat, [ | true ], "true", "| true");
601-
c2!(pat, [ |true| false ], "true | false", "|true| false");
600+
c1!(pat, [ | true ], "| true");
601+
c1!(pat, [ |true| false ], "|true| false");
602602

603603
// PatKind::Path
604604
c1!(pat, [ crate::Path ], "crate::Path");
@@ -631,7 +631,7 @@ fn test_pat() {
631631
// PatKind::Slice
632632
c1!(pat, [ [] ], "[]");
633633
c1!(pat, [ [true] ], "[true]");
634-
c2!(pat, [ [true,] ], "[true]", "[true,]");
634+
c1!(pat, [ [true,] ], "[true,]");
635635
c1!(pat, [ [true, false] ], "[true, false]");
636636

637637
// PatKind::Rest

tests/ui/macros/trace_faulty_macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ macro_rules! test {
4646
(let $p:pat = $e:expr) => {test!(($p,$e))};
4747
// this should be expr
4848
// vvv
49-
(($p:pat, $e:pat)) => {let $p = $e;}; //~ ERROR expected expression, found pattern `1 + 1`
49+
(($p:pat, $e:pat)) => {let $p = $e;}; //~ ERROR expected expression, found `pat` metavariable
5050
}
5151

5252
fn foo() {

tests/ui/macros/trace_faulty_macros.stderr

+4-11
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ LL | my_recursive_macro!();
5050
= note: expanding `my_recursive_macro! { }`
5151
= note: to `my_recursive_macro! ();`
5252

53-
error: expected expression, found pattern `A { a: a, b: 0, c: _, .. }`
53+
error: expected expression, found `pat` metavariable
5454
--> $DIR/trace_faulty_macros.rs:16:9
5555
|
5656
LL | $a
@@ -69,22 +69,15 @@ LL | #[derive(Debug)]
6969
LL | fn use_derive_macro_as_attr() {}
7070
| -------------------------------- not a `struct`, `enum` or `union`
7171

72-
error: expected expression, found pattern `1 + 1`
72+
error: expected expression, found `pat` metavariable
7373
--> $DIR/trace_faulty_macros.rs:49:37
7474
|
75-
LL | (let $p:pat = $e:expr) => {test!(($p,$e))};
76-
| -- this is interpreted as expression, but it is expected to be pattern
77-
...
7875
LL | (($p:pat, $e:pat)) => {let $p = $e;};
7976
| ^^ expected expression
8077
...
8178
LL | test!(let x = 1+1);
82-
| ------------------
83-
| | |
84-
| | this is expected to be expression
85-
| in this macro invocation
79+
| ------------------ in this macro invocation
8680
|
87-
= note: when forwarding a matched fragment to another macro-by-example, matchers in the second macro will see an opaque AST of the fragment type, not the underlying tokens
8881
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
8982

9083
note: trace_macro
@@ -96,7 +89,7 @@ LL | let a = pat_macro!();
9689
= note: expanding `pat_macro! { }`
9790
= note: to `pat_macro! (A { a : a, b : 0, c : _, .. });`
9891
= note: expanding `pat_macro! { A { a : a, b : 0, c : _, .. } }`
99-
= note: to `A { a: a, b: 0, c: _, .. }`
92+
= note: to `A { a : a, b : 0, c : _, .. }`
10093

10194
note: trace_macro
10295
--> $DIR/trace_faulty_macros.rs:53:5

tests/ui/parser/issues/issue-65122-mac-invoc-in-mut-patterns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ macro_rules! mac2 {
1212
($eval:pat) => {
1313
let mut $eval = ();
1414
//~^ ERROR `mut` must be followed by a named binding
15-
//~| ERROR expected identifier, found `does_not_exist!()`
15+
//~| ERROR expected identifier, found metavariable
1616
};
1717
}
1818

tests/ui/parser/issues/issue-65122-mac-invoc-in-mut-patterns.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ LL | mac1! { does_not_exist!() }
1010
= note: `mut` may be followed by `variable` and `variable @ pattern`
1111
= note: this error originates in the macro `mac1` (in Nightly builds, run with -Z macro-backtrace for more info)
1212

13-
error: expected identifier, found `does_not_exist!()`
13+
error: expected identifier, found metavariable
1414
--> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:13:17
1515
|
1616
LL | let mut $eval = ();
17-
| ^^^^^ expected identifier
17+
| ^^^^^ expected identifier, found metavariable
1818
...
1919
LL | mac2! { does_not_exist!() }
2020
| --------------------------- in this macro invocation

tests/ui/parser/mut-patterns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn main() {
4141
// Make sure we don't accidentally allow `mut $p` where `$p:pat`.
4242
macro_rules! foo {
4343
($p:pat) => {
44-
let mut $p = 0; //~ ERROR expected identifier, found `x`
44+
let mut $p = 0; //~ ERROR expected identifier, found metavariable
4545
}
4646
}
4747
foo!(x);

tests/ui/parser/mut-patterns.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ LL | let mut W(mut a, W(b, W(ref c, W(d, B { box f }))))
9999
|
100100
= note: `mut` may be followed by `variable` and `variable @ pattern`
101101

102-
error: expected identifier, found `x`
102+
error: expected identifier, found metavariable
103103
--> $DIR/mut-patterns.rs:44:21
104104
|
105105
LL | let mut $p = 0;
106-
| ^^ expected identifier
106+
| ^^ expected identifier, found metavariable
107107
...
108108
LL | foo!(x);
109109
| ------- in this macro invocation

0 commit comments

Comments
 (0)