Skip to content

Commit c193815

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 e651a19 commit c193815

19 files changed

+80
-74
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(),
@@ -246,7 +245,6 @@ impl HasTokens for Nonterminal {
246245
Nonterminal::NtItem(item) => item.tokens_mut(),
247246
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
248247
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
249-
Nonterminal::NtPat(pat) => pat.tokens_mut(),
250248
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
251249
Nonterminal::NtPath(path) => path.tokens_mut(),
252250
Nonterminal::NtBlock(block) => block.tokens_mut(),

compiler/rustc_ast/src/mut_visit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
836836
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
837837
})
838838
}),
839-
token::NtPat(pat) => vis.visit_pat(pat),
840839
token::NtExpr(expr) => vis.visit_expr(expr),
841840
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
842841
token::NtLifetime(ident) => vis.visit_ident(ident),

compiler/rustc_ast/src/token.rs

-5
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,6 @@ impl Token {
551551
| Lt | BinOp(Shl) // associated path
552552
| PathSep => true, // global path
553553
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
554-
NtPat(..) |
555554
NtBlock(..) |
556555
NtPath(..)),
557556
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
@@ -928,7 +927,6 @@ pub enum Nonterminal {
928927
NtItem(P<ast::Item>),
929928
NtBlock(P<ast::Block>),
930929
NtStmt(P<ast::Stmt>),
931-
NtPat(P<ast::Pat>),
932930
NtExpr(P<ast::Expr>),
933931
NtIdent(Ident, IdentIsRaw),
934932
NtLifetime(Ident),
@@ -1022,7 +1020,6 @@ impl Nonterminal {
10221020
NtItem(item) => item.span,
10231021
NtBlock(block) => block.span,
10241022
NtStmt(stmt) => stmt.span,
1025-
NtPat(pat) => pat.span,
10261023
NtExpr(expr) | NtLiteral(expr) => expr.span,
10271024
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
10281025
NtMeta(attr_item) => attr_item.span(),
@@ -1035,7 +1032,6 @@ impl Nonterminal {
10351032
NtItem(..) => "item",
10361033
NtBlock(..) => "block",
10371034
NtStmt(..) => "statement",
1038-
NtPat(..) => "pattern",
10391035
NtExpr(..) => "expression",
10401036
NtLiteral(..) => "literal",
10411037
NtIdent(..) => "identifier",
@@ -1068,7 +1064,6 @@ impl fmt::Debug for Nonterminal {
10681064
NtItem(..) => f.pad("NtItem(..)"),
10691065
NtBlock(..) => f.pad("NtBlock(..)"),
10701066
NtStmt(..) => f.pad("NtStmt(..)"),
1071-
NtPat(..) => f.pad("NtPat(..)"),
10721067
NtExpr(..) => f.pad("NtExpr(..)"),
10731068
NtIdent(..) => f.pad("NtIdent(..)"),
10741069
NtLiteral(..) => f.pad("NtLiteral(..)"),

compiler/rustc_ast/src/tokenstream.rs

-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ impl TokenStream {
480480
TokenStream::token_alone(token::Semi, stmt.span)
481481
}
482482
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
483-
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
484483
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
485484
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
486485
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
@@ -850,7 +850,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
850850
token::NtItem(e) => self.item_to_string(e),
851851
token::NtBlock(e) => self.block_to_string(e),
852852
token::NtStmt(e) => self.stmt_to_string(e),
853-
token::NtPat(e) => self.pat_to_string(e),
854853
&token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw.into()).to_string(),
855854
token::NtLifetime(e) => e.to_string(),
856855
token::NtLiteral(e) => self.expr_to_string(e),

compiler/rustc_expand/src/mbe/transcribe.rs

+7
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ pub(super) fn transcribe<'a>(
266266
// without wrapping them into groups.
267267
maybe_use_metavar_location(cx, &stack, sp, tt, &mut marker)
268268
}
269+
MatchedSingle(ParseNtResult::PatParam(ref pat, inferred)) => mk_delimited(
270+
NonterminalKind::PatParam { inferred: *inferred },
271+
TokenStream::from_ast(pat),
272+
),
273+
MatchedSingle(ParseNtResult::PatWithOr(ref pat)) => {
274+
mk_delimited(NonterminalKind::PatWithOr, TokenStream::from_ast(pat))
275+
}
269276
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
270277
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
271278
}

compiler/rustc_parse/src/errors.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1143,9 +1143,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedSemi {
11431143
Some(TokenDescription::DocComment) => {
11441144
fluent::parse_expected_semi_found_doc_comment_str
11451145
}
1146-
Some(TokenDescription::MetaVar(_)) => {
1147-
fluent::parse_expected_semi_found_metavar_str
1148-
}
1146+
Some(TokenDescription::MetaVar(_)) => fluent::parse_expected_semi_found_metavar_str,
11491147
None => fluent::parse_expected_semi_found_str,
11501148
},
11511149
);

compiler/rustc_parse/src/parser/diagnostics.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ use parser::Recovered;
2626
use rustc_ast as ast;
2727
use rustc_ast::ptr::P;
2828
use rustc_ast::token::{self, Delimiter, Lit, LitKind, Token, TokenKind};
29-
use rustc_ast::tokenstream::AttrTokenTree;
3029
use rustc_ast::util::parser::AssocOp;
3130
use rustc_ast::{
3231
AngleBracketedArg, AngleBracketedArgs, AnonConst, AttrVec, BinOpKind, BindingAnnotation, Block,
33-
BlockCheckMode, Expr, ExprKind, GenericArg, Generics, HasTokens, Item, ItemKind, Param, Pat,
34-
PatKind, Path, PathSegment, QSelf, Ty, TyKind,
32+
BlockCheckMode, Expr, ExprKind, GenericArg, Generics, Item, ItemKind, Param, Pat, PatKind,
33+
Path, PathSegment, QSelf, Ty, TyKind,
3534
};
3635
use rustc_ast_pretty::pprust;
3736
use rustc_data_structures::fx::FxHashSet;
@@ -2363,15 +2362,14 @@ impl<'a> Parser<'a> {
23632362
}
23642363
err.span_label(span, "expected expression");
23652364

2365+
/* njn: temp disabled, which hurts tests/ui/macros/trace_faulty_macros.rs
2366+
23662367
// Walk the chain of macro expansions for the current token to point at how the original
23672368
// code was interpreted. This helps the user realize when a macro argument of one type is
23682369
// later reinterpreted as a different type, like `$x:expr` being reinterpreted as `$x:pat`
23692370
// in a subsequent macro invocation (#71039).
23702371
let mut tok = self.token.clone();
23712372
let mut labels = vec![];
2372-
if let token::OpenDelim(Delimiter::Invisible(_)) = &tok.kind {
2373-
panic!("njn: invis-delim?");
2374-
}
23752373
while let TokenKind::Interpolated(node) = &tok.kind {
23762374
let tokens = node.0.tokens();
23772375
labels.push(node.clone());
@@ -2381,9 +2379,6 @@ impl<'a> Parser<'a> {
23812379
&& let [AttrTokenTree::Token(token, _)] = &tokens[..]
23822380
{
23832381
tok = token.clone();
2384-
if let token::OpenDelim(Delimiter::Invisible(_)) = &tok.kind {
2385-
panic!("njn: invis-delim?");
2386-
}
23872382
} else {
23882383
break;
23892384
}
@@ -2421,6 +2416,7 @@ impl<'a> Parser<'a> {
24212416
tokens",
24222417
);
24232418
}
2419+
*/
24242420
err
24252421
}
24262422

compiler/rustc_parse/src/parser/item.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::maybe_whole;
1010
use ast::token::IdentIsRaw;
1111
use rustc_ast::ast::*;
1212
use rustc_ast::ptr::P;
13-
use rustc_ast::token::{self, Delimiter, TokenKind};
13+
use rustc_ast::token::{self, Delimiter, InvisibleOrigin, NonterminalKind, TokenKind};
1414
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
1515
use rustc_ast::util::case::Case;
1616
use rustc_ast::{self as ast};
@@ -2838,9 +2838,10 @@ impl<'a> Parser<'a> {
28382838

28392839
fn is_named_param(&self) -> bool {
28402840
let offset = match &self.token.kind {
2841-
// njn: nothing to do, just handle NtPat later
2842-
token::Interpolated(nt) => match &nt.0 {
2843-
token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
2841+
token::OpenDelim(Delimiter::Invisible(origin)) => match origin {
2842+
InvisibleOrigin::MetaVar(
2843+
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr,
2844+
) => return self.check_noexpect_past_close_delim(&token::Colon),
28442845
_ => 0,
28452846
},
28462847
token::BinOp(token::And) | token::AndAnd => 1,

compiler/rustc_parse/src/parser/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,8 @@ pub enum FlatToken {
16791679
#[derive(Clone, Debug)]
16801680
pub enum ParseNtResult<NtType> {
16811681
Tt(TokenTree),
1682+
PatParam(P<ast::Pat>, /* inferred */ bool),
1683+
PatWithOr(P<ast::Pat>),
16821684
Ty(P<ast::Ty>),
16831685
Vis(P<ast::Visibility>),
16841686

@@ -1693,6 +1695,8 @@ impl<T> ParseNtResult<T> {
16931695
{
16941696
match self {
16951697
ParseNtResult::Tt(tt) => ParseNtResult::Tt(tt),
1698+
ParseNtResult::PatParam(x, inf) => ParseNtResult::PatParam(x, inf),
1699+
ParseNtResult::PatWithOr(x) => ParseNtResult::PatWithOr(x),
16961700
ParseNtResult::Ty(x) => ParseNtResult::Ty(x),
16971701
ParseNtResult::Vis(x) => ParseNtResult::Vis(x),
16981702
ParseNtResult::Nt(nt) => ParseNtResult::Nt(f(nt)),

compiler/rustc_parse/src/parser/nonterminal.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ impl<'a> Parser<'a> {
4646
fn nt_may_be_ident(nt: &token::Nonterminal) -> bool {
4747
match nt {
4848
NtStmt(_)
49-
| NtPat(_)
5049
| NtExpr(_)
5150
| NtIdent(..)
5251
| NtLiteral(_) // `true`, `false`
@@ -82,7 +81,7 @@ impl<'a> Parser<'a> {
8281
token::OpenDelim(Delimiter::Brace) => true,
8382
token::Interpolated(nt) => match &nt.0 {
8483
NtBlock(_) | NtLifetime(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
85-
NtItem(_) | NtPat(_) | NtIdent(..) | NtMeta(_) | NtPath(_) => false,
84+
NtItem(_) | NtIdent(..) | NtMeta(_) | NtPath(_) => false,
8685
},
8786
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
8887
NonterminalKind::Block
@@ -182,19 +181,22 @@ impl<'a> Parser<'a> {
182181
.create_err(UnexpectedNonterminal::Statement(self.token.span)));
183182
}
184183
},
185-
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr => {
186-
NtPat(self.collect_tokens_no_attrs(|this| match kind {
187-
NonterminalKind::PatParam { .. } => this.parse_pat_no_top_alt(None, None),
188-
NonterminalKind::PatWithOr => this.parse_pat_allow_top_alt(
184+
NonterminalKind::PatParam { inferred } => {
185+
return Ok(ParseNtResult::PatParam(
186+
self.collect_tokens_no_attrs(|this| this.parse_pat_no_top_alt(None, None))?,
187+
inferred,
188+
));
189+
}
190+
NonterminalKind::PatWithOr => {
191+
return Ok(ParseNtResult::PatWithOr(self.collect_tokens_no_attrs(|this| {
192+
this.parse_pat_allow_top_alt(
189193
None,
190194
RecoverComma::No,
191195
RecoverColon::No,
192196
CommaRecoveryMode::EitherTupleOrPipe,
193-
),
194-
_ => unreachable!(),
195-
})?)
197+
)
198+
})?));
196199
}
197-
198200
NonterminalKind::Expr => NtExpr(self.parse_expr_force_collect()?),
199201
NonterminalKind::Literal => {
200202
// The `:literal` matcher does not support attributes

compiler/rustc_parse/src/parser/pat.rs

+30-11
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, BindingAnnotation, ByRef, Expr, ExprKind, MacCall, Mutability, Pat,
2022
PatField, PatFieldsRest, PatKind, Path, QSelf, RangeEnd, RangeSyntax,
@@ -435,7 +437,27 @@ 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+
// Need to try both kinds of pattern nonterminals.
442+
// njn: is that true?
443+
if let Some(pat) = maybe_reparse_metavar_seq!(
444+
self,
445+
NonterminalKind::PatParam { inferred },
446+
NonterminalKind::PatParam { inferred },
447+
ParseNtResult::PatParam(pat, _),
448+
pat
449+
) {
450+
return Ok(pat);
451+
}
452+
if let Some(pat) = maybe_reparse_metavar_seq!(
453+
self,
454+
NonterminalKind::PatWithOr,
455+
NonterminalKind::PatWithOr,
456+
ParseNtResult::PatWithOr(pat),
457+
pat
458+
) {
459+
return Ok(pat);
460+
}
439461

440462
let mut lo = self.token.span;
441463

@@ -756,13 +778,10 @@ impl<'a> Parser<'a> {
756778
self.recover_additional_muts();
757779

758780
// 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.0 {
761-
self.expected_ident_found_err().emit();
762-
}
763-
}
764-
if let token::OpenDelim(Delimiter::Invisible(_)) = &self.token.kind {
765-
panic!("njn: invis-delim?");
781+
if let Some(NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr) =
782+
self.token.is_metavar_seq()
783+
{
784+
self.expected_ident_found_err().emit();
766785
}
767786

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

tests/ui/macros/stringify.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ fn test_pat() {
562562
// PatKind::Struct
563563
c1!(pat, [ Struct {} ], "Struct {}");
564564
c1!(pat, [ Struct::<u8> {} ], "Struct::<u8> {}");
565-
c2!(pat, [ Struct ::< u8 > {} ], "Struct::<u8> {}", "Struct ::< u8 > {}");
565+
c1!(pat, [ Struct ::< u8 > {} ], "Struct ::< u8 > {}");
566566
c1!(pat, [ Struct::<'static> {} ], "Struct::<'static> {}");
567567
c1!(pat, [ Struct { x } ], "Struct { x }");
568568
c1!(pat, [ Struct { x: _x } ], "Struct { x: _x }");
@@ -582,8 +582,8 @@ fn test_pat() {
582582

583583
// PatKind::Or
584584
c1!(pat, [ true | false ], "true | false");
585-
c2!(pat, [ | true ], "true", "| true");
586-
c2!(pat, [ |true| false ], "true | false", "|true| false");
585+
c1!(pat, [ | true ], "| true");
586+
c1!(pat, [ |true| false ], "|true| false");
587587

588588
// PatKind::Path
589589
c1!(pat, [ crate::Path ], "crate::Path");
@@ -616,7 +616,7 @@ fn test_pat() {
616616
// PatKind::Slice
617617
c1!(pat, [ [] ], "[]");
618618
c1!(pat, [ [true] ], "[true]");
619-
c2!(pat, [ [true,] ], "[true]", "[true,]");
619+
c1!(pat, [ [true,] ], "[true,]");
620620
c1!(pat, [ [true, false] ], "[true, false]");
621621

622622
// 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

+5-16
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,26 +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-
| |
78-
| this macro fragment matcher is expression
79-
...
8075
LL | (($p:pat, $e:pat)) => {let $p = $e;};
81-
| ------ ^^ expected expression
82-
| |
83-
| this macro fragment matcher is pattern
76+
| ^^ expected expression
8477
...
8578
LL | test!(let x = 1+1);
86-
| ------------------
87-
| | |
88-
| | this is expected to be expression
89-
| in this macro invocation
79+
| ------------------ in this macro invocation
9080
|
91-
= 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
9281
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
9382

9483
note: trace_macro
@@ -100,7 +89,7 @@ LL | let a = pat_macro!();
10089
= note: expanding `pat_macro! { }`
10190
= note: to `pat_macro! (A { a : a, b : 0, c : _, .. });`
10291
= note: expanding `pat_macro! { A { a : a, b : 0, c : _, .. } }`
103-
= note: to `A { a: a, b: 0, c: _, .. }`
92+
= note: to `A { a : a, b : 0, c : _, .. }`
10493

10594
note: trace_macro
10695
--> $DIR/trace_faulty_macros.rs:53:5

0 commit comments

Comments
 (0)