Skip to content

Commit 7b1887d

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 5fb9b2d commit 7b1887d

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
@@ -598,10 +598,11 @@ impl Token {
598598
/// **NB**: Take care when modifying this function, since it will change
599599
/// the stable set of tokens that are allowed to match an expr nonterminal.
600600
pub fn can_begin_expr(&self) -> bool {
601+
use Delimiter::*;
601602
match self.uninterpolate().kind {
602603
Ident(name, is_raw) =>
603604
ident_can_begin_expr(name, self.span, is_raw), // value name or keyword
604-
OpenDelim(..) | // tuple, array or block
605+
OpenDelim(Parenthesis | Brace | Bracket) | // tuple, array or block
605606
Literal(..) | // literal
606607
Not | // operator not
607608
BinOp(Minus) | // unary minus
@@ -612,7 +613,7 @@ impl Token {
612613
// DotDotDot is no longer supported, but we need some way to display the error
613614
DotDot | DotDotDot | DotDotEq | // range notation
614615
Lt | BinOp(Shl) | // associated path
615-
PathSep | // global path
616+
PathSep | // global path
616617
Lifetime(..) | // labeled loop
617618
Pound => true, // expression attributes
618619
Interpolated(ref nt) =>
@@ -622,6 +623,12 @@ impl Token {
622623
NtLiteral(..) |
623624
NtPath(..)
624625
),
626+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
627+
MetaVarKind::Block |
628+
MetaVarKind::Expr { .. } |
629+
MetaVarKind::Literal |
630+
MetaVarKind::Path
631+
))) => true,
625632
_ => false,
626633
}
627634
}
@@ -655,6 +662,14 @@ impl Token {
655662
| NtPath(..)
656663
| NtTy(..)
657664
),
665+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
666+
MetaVarKind::Expr { .. } |
667+
MetaVarKind::Literal |
668+
MetaVarKind::Meta |
669+
MetaVarKind::Pat(_) |
670+
MetaVarKind::Path |
671+
MetaVarKind::Ty
672+
))) => true,
658673
_ => false,
659674
}
660675
}
@@ -675,6 +690,10 @@ impl Token {
675690
Lt | BinOp(Shl) | // associated path
676691
PathSep => true, // global path
677692
Interpolated(ref nt) => matches!(&**nt, NtTy(..) | NtPath(..)),
693+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
694+
MetaVarKind::Ty |
695+
MetaVarKind::Path
696+
))) => true,
678697
// For anonymous structs or unions, which only appear in specific positions
679698
// (type of struct fields or union fields), we don't consider them as regular types
680699
_ => false,
@@ -687,6 +706,9 @@ impl Token {
687706
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
688707
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
689708
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
709+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
710+
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
711+
))) => true,
690712
_ => false,
691713
}
692714
}
@@ -743,6 +765,13 @@ impl Token {
743765
},
744766
_ => false,
745767
},
768+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
769+
MetaVarKind::Literal => true,
770+
MetaVarKind::Expr { can_begin_literal_maybe_minus, .. } => {
771+
can_begin_literal_maybe_minus
772+
}
773+
_ => false,
774+
},
746775
_ => false,
747776
}
748777
}
@@ -758,6 +787,11 @@ impl Token {
758787
},
759788
_ => false,
760789
},
790+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
791+
MetaVarKind::Literal => true,
792+
MetaVarKind::Expr { can_begin_string_literal, .. } => can_begin_string_literal,
793+
_ => false,
794+
},
761795
_ => false,
762796
}
763797
}

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
@@ -3582,11 +3582,19 @@ impl<'a> Parser<'a> {
35823582
&& !self.token.is_reserved_ident()
35833583
&& self.look_ahead(1, |t| {
35843584
AssocOp::from_token(t).is_some()
3585-
|| matches!(t.kind, token::OpenDelim(_))
3585+
|| matches!(
3586+
t.kind,
3587+
token::OpenDelim(
3588+
Delimiter::Parenthesis
3589+
| Delimiter::Bracket
3590+
| Delimiter::Brace
3591+
)
3592+
)
35863593
|| *t == token::Dot
35873594
})
35883595
{
3589-
// Looks like they tried to write a shorthand, complex expression.
3596+
// Looks like they tried to write a shorthand, complex expression,
3597+
// E.g.: `n + m`, `f(a)`, `a[i]`, `S { x: 3 }`, or `x.y`.
35903598
e.span_suggestion_verbose(
35913599
self.token.span.shrink_to_lo(),
35923600
"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)