Skip to content

Commit 95ab222

Browse files
committed
Prepare for invisible delimiters.
Current places where `Interpolated` is used are going to change to instead use invisible delimiters. This prepares for that. - It adds invisible delimiter cases to the `can_begin_*`/`may_be_*` methods and the `failed_to_match_macro` that are equivalent to the existing `Interpolated` cases. - It adds panics/asserts in some places where invisible delimiters should never occur. - In `Parser::parse_struct_fields` it excludes an ident + invisible delimiter from special consideration in an error message, because that's quite different to an ident + paren/brace/bracket.
1 parent 4451dc9 commit 95ab222

File tree

5 files changed

+109
-14
lines changed

5 files changed

+109
-14
lines changed

compiler/rustc_ast/src/token.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,11 @@ impl Token {
599599
/// **NB**: Take care when modifying this function, since it will change
600600
/// the stable set of tokens that are allowed to match an expr nonterminal.
601601
pub fn can_begin_expr(&self) -> bool {
602+
use Delimiter::*;
602603
match self.uninterpolate().kind {
603604
Ident(name, is_raw) =>
604605
ident_can_begin_expr(name, self.span, is_raw), // value name or keyword
605-
OpenDelim(..) | // tuple, array or block
606+
OpenDelim(Parenthesis | Brace | Bracket) | // tuple, array or block
606607
Literal(..) | // literal
607608
Not | // operator not
608609
BinOp(Minus) | // unary minus
@@ -613,7 +614,7 @@ impl Token {
613614
// DotDotDot is no longer supported, but we need some way to display the error
614615
DotDot | DotDotDot | DotDotEq | // range notation
615616
Lt | BinOp(Shl) | // associated path
616-
PathSep | // global path
617+
PathSep | // global path
617618
Lifetime(..) | // labeled loop
618619
Pound => true, // expression attributes
619620
Interpolated(ref nt) =>
@@ -623,6 +624,12 @@ impl Token {
623624
NtLiteral(..) |
624625
NtPath(..)
625626
),
627+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
628+
MetaVarKind::Block |
629+
MetaVarKind::Expr { .. } |
630+
MetaVarKind::Literal |
631+
MetaVarKind::Path
632+
))) => true,
626633
_ => false,
627634
}
628635
}
@@ -656,6 +663,14 @@ impl Token {
656663
| NtPath(..)
657664
| NtTy(..)
658665
),
666+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
667+
MetaVarKind::Expr { .. } |
668+
MetaVarKind::Literal |
669+
MetaVarKind::Meta |
670+
MetaVarKind::Pat(_) |
671+
MetaVarKind::Path |
672+
MetaVarKind::Ty
673+
))) => true,
659674
_ => false,
660675
}
661676
}
@@ -676,6 +691,10 @@ impl Token {
676691
Lt | BinOp(Shl) | // associated path
677692
PathSep => true, // global path
678693
Interpolated(ref nt) => matches!(&**nt, NtTy(..) | NtPath(..)),
694+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
695+
MetaVarKind::Ty |
696+
MetaVarKind::Path
697+
))) => true,
679698
// For anonymous structs or unions, which only appear in specific positions
680699
// (type of struct fields or union fields), we don't consider them as regular types
681700
_ => false,
@@ -688,6 +707,9 @@ impl Token {
688707
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
689708
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
690709
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
710+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
711+
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
712+
))) => true,
691713
_ => false,
692714
}
693715
}
@@ -744,6 +766,13 @@ impl Token {
744766
},
745767
_ => false,
746768
},
769+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
770+
MetaVarKind::Literal => true,
771+
MetaVarKind::Expr { can_begin_literal_maybe_minus, .. } => {
772+
can_begin_literal_maybe_minus
773+
}
774+
_ => false,
775+
},
747776
_ => false,
748777
}
749778
}
@@ -759,6 +788,11 @@ impl Token {
759788
},
760789
_ => false,
761790
},
791+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
792+
MetaVarKind::Literal => true,
793+
MetaVarKind::Expr { can_begin_string_literal, .. } => can_begin_string_literal,
794+
_ => false,
795+
},
762796
_ => false,
763797
}
764798
}

compiler/rustc_expand/src/mbe/diagnostics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22

3-
use rustc_ast::token::{self, Token, TokenKind};
3+
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
44
use rustc_ast::tokenstream::TokenStream;
55
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, DiagMessage};
66
use rustc_macros::Subdiagnostic;
@@ -68,7 +68,9 @@ pub(super) fn failed_to_match_macro(
6868

6969
if let MatcherLoc::Token { token: expected_token } = &remaining_matcher
7070
&& (matches!(expected_token.kind, TokenKind::Interpolated(_))
71-
|| matches!(token.kind, TokenKind::Interpolated(_)))
71+
|| matches!(token.kind, TokenKind::Interpolated(_))
72+
|| matches!(expected_token.kind, TokenKind::OpenDelim(Delimiter::Invisible(_)))
73+
|| matches!(token.kind, TokenKind::OpenDelim(Delimiter::Invisible(_))))
7274
{
7375
err.note("captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens");
7476
err.note("see <https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment> for more information");

compiler/rustc_parse/src/lexer/tokentrees.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,19 @@ impl<'psess, 'src> TokenTreesReader<'psess, 'src> {
4343
let mut buf = Vec::new();
4444
loop {
4545
match self.token.kind {
46-
token::OpenDelim(delim) => buf.push(match self.lex_token_tree_open_delim(delim) {
47-
Ok(val) => val,
48-
Err(errs) => return (open_spacing, TokenStream::new(buf), Err(errs)),
49-
}),
46+
token::OpenDelim(delim) => {
47+
// Invisible delimiters cannot occur here because `TokenTreesReader` parses
48+
// code directly from strings, with no macro expansion involved.
49+
debug_assert!(!matches!(delim, Delimiter::Invisible(_)));
50+
buf.push(match self.lex_token_tree_open_delim(delim) {
51+
Ok(val) => val,
52+
Err(errs) => return (open_spacing, TokenStream::new(buf), Err(errs)),
53+
})
54+
}
5055
token::CloseDelim(delim) => {
56+
// Invisible delimiters cannot occur here because `TokenTreesReader` parses
57+
// code directly from strings, with no macro expansion involved.
58+
debug_assert!(!matches!(delim, Delimiter::Invisible(_)));
5159
return (
5260
open_spacing,
5361
TokenStream::new(buf),

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3591,11 +3591,19 @@ impl<'a> Parser<'a> {
35913591
&& !self.token.is_reserved_ident()
35923592
&& self.look_ahead(1, |t| {
35933593
AssocOp::from_token(t).is_some()
3594-
|| matches!(t.kind, token::OpenDelim(_))
3594+
|| matches!(
3595+
t.kind,
3596+
token::OpenDelim(
3597+
Delimiter::Parenthesis
3598+
| Delimiter::Bracket
3599+
| Delimiter::Brace
3600+
)
3601+
)
35953602
|| *t == token::Dot
35963603
})
35973604
{
3598-
// Looks like they tried to write a shorthand, complex expression.
3605+
// Looks like they tried to write a shorthand, complex expression,
3606+
// E.g.: `n + m`, `f(a)`, `a[i]`, `S { x: 3 }`, or `x.y`.
35993607
e.span_suggestion_verbose(
36003608
self.token.span.shrink_to_lo(),
36013609
"try naming a field",

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use rustc_ast::ptr::P;
33
use rustc_ast::token::Nonterminal::*;
44
use rustc_ast::token::NtExprKind::*;
55
use rustc_ast::token::NtPatKind::*;
6-
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token};
6+
use rustc_ast::token::{
7+
self, Delimiter, InvisibleOrigin, MetaVarKind, Nonterminal, NonterminalKind, Token,
8+
};
79
use rustc_ast_pretty::pprust;
810
use rustc_data_structures::sync::Lrc;
911
use rustc_errors::PResult;
@@ -22,7 +24,29 @@ impl<'a> Parser<'a> {
2224
#[inline]
2325
pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
2426
/// Checks whether the non-terminal may contain a single (non-keyword) identifier.
25-
fn may_be_ident(nt: &token::Nonterminal) -> bool {
27+
fn may_be_ident(kind: MetaVarKind) -> bool {
28+
use MetaVarKind::*;
29+
match kind {
30+
Stmt
31+
| Pat(_)
32+
| Expr { .. }
33+
| Ty
34+
| Literal // `true`, `false`
35+
| Meta
36+
| Path => true,
37+
38+
Item
39+
| Block
40+
| Vis => false,
41+
42+
Ident
43+
| Lifetime
44+
| TT => unreachable!(),
45+
}
46+
}
47+
48+
/// Old variant of `may_be_ident`. Being phased out.
49+
fn nt_may_be_ident(nt: &Nonterminal) -> bool {
2650
match nt {
2751
NtStmt(_)
2852
| NtPat(_)
@@ -69,7 +93,8 @@ impl<'a> Parser<'a> {
6993
| token::Ident(..)
7094
| token::NtIdent(..)
7195
| token::NtLifetime(..)
72-
| token::Interpolated(_) => true,
96+
| token::Interpolated(_)
97+
| token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(_))) => true,
7398
_ => token.can_begin_type(),
7499
},
75100
NonterminalKind::Block => match &token.kind {
@@ -79,11 +104,29 @@ impl<'a> Parser<'a> {
79104
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
80105
NtItem(_) | NtPat(_) | NtTy(_) | NtMeta(_) | NtPath(_) | NtVis(_) => false,
81106
},
107+
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
108+
MetaVarKind::Block
109+
| MetaVarKind::Stmt
110+
| MetaVarKind::Expr { .. }
111+
| MetaVarKind::Literal => true,
112+
MetaVarKind::Item
113+
| MetaVarKind::Pat(_)
114+
| MetaVarKind::Ty
115+
| MetaVarKind::Meta
116+
| MetaVarKind::Path
117+
| MetaVarKind::Vis => false,
118+
MetaVarKind::Lifetime | MetaVarKind::Ident | MetaVarKind::TT => {
119+
unreachable!()
120+
}
121+
},
82122
_ => false,
83123
},
84124
NonterminalKind::Path | NonterminalKind::Meta => match &token.kind {
85125
token::PathSep | token::Ident(..) | token::NtIdent(..) => true,
86-
token::Interpolated(nt) => may_be_ident(nt),
126+
token::Interpolated(nt) => nt_may_be_ident(nt),
127+
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(kind))) => {
128+
may_be_ident(*kind)
129+
}
87130
_ => false,
88131
},
89132
NonterminalKind::Pat(pat_kind) => token.can_begin_pattern(pat_kind),

0 commit comments

Comments
 (0)