Skip to content

Rollup of 9 pull requests #129729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6ed283b
rustdoc-json: Add test for `Self` type
aDotInTheVoid Aug 15, 2024
42a901a
Don't use TyKind in lint
compiler-errors Aug 24, 2024
af05882
Deny wasm_c_abi lint to nudge the last 25%
workingjubilee Aug 24, 2024
c61f85b
Don't make pattern nonterminals match statement nonterminals
compiler-errors Jan 22, 2024
f1e2991
std: make `thread::current` available in all `thread_local!` destructors
joboet Jul 18, 2024
cfbd250
bless miri tests
joboet Jul 20, 2024
a1c36c6
linker: Synchronize native library search in rustc and linker
petrochenkov Aug 14, 2024
05bd36d
linker: Better support alternative static library naming on MSVC
petrochenkov Aug 21, 2024
ac8f132
docs: Update docs for the rustc's `-L` option
petrochenkov Aug 27, 2024
ee05de8
Re-enable android tests/benches in alloc
saethlin Aug 27, 2024
83de14c
Enable some ilog2 tests as well
saethlin Aug 27, 2024
ae6f8a7
allow BufReader::peek to be called on unsized types
lolbinarycat Aug 28, 2024
0cac915
Improve `isqrt` tests and add benchmarks
ChaiTRex Aug 26, 2024
7af8e21
Speed up `checked_isqrt` and `isqrt` methods
ChaiTRex Aug 26, 2024
120e437
Rollup merge of #120221 - compiler-errors:statements-are-not-patterns…
workingjubilee Aug 29, 2024
772a670
Rollup merge of #127912 - joboet:tls_dtor_thread_current, r=cupiver
workingjubilee Aug 29, 2024
0e039de
Rollup merge of #128166 - ChaiTRex:isqrt, r=tgross35
workingjubilee Aug 29, 2024
10db657
Rollup merge of #129123 - aDotInTheVoid:rustdoc-json-self, r=fmease
workingjubilee Aug 29, 2024
9d9c153
Rollup merge of #129366 - petrochenkov:libsearch, r=jieyouxu
workingjubilee Aug 29, 2024
8f2dad9
Rollup merge of #129527 - compiler-errors:lint-nit, r=Nadrieril
workingjubilee Aug 29, 2024
a4ae0e3
Rollup merge of #129534 - workingjubilee:ratchet-wasm-c-abi-fcw-to-de…
workingjubilee Aug 29, 2024
09a2515
Rollup merge of #129640 - saethlin:unignore-android-in-alloc, r=tgross35
workingjubilee Aug 29, 2024
6027230
Rollup merge of #129675 - lolbinarycat:bufreader_peek_unsized, r=work…
workingjubilee Aug 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Don't make pattern nonterminals match statement nonterminals
  • Loading branch information
compiler-errors committed Aug 26, 2024
commit c61f85b6dd47343abe6383ea2eb71f0b3a7d0e2b
57 changes: 36 additions & 21 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ impl Token {
}

/// Returns `true` if the token can appear at the start of an expression.
///
/// **NB**: Take care when modifying this function, since it will change
/// the stable set of tokens that are allowed to match an expr nonterminal.
pub fn can_begin_expr(&self) -> bool {
match self.uninterpolate().kind {
Ident(name, is_raw) =>
Expand All @@ -504,34 +507,46 @@ impl Token {
PathSep | // global path
Lifetime(..) | // labeled loop
Pound => true, // expression attributes
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) |
NtExpr(..) |
NtBlock(..) |
NtPath(..)),
Interpolated(ref nt) =>
matches!(&**nt,
NtBlock(..) |
NtExpr(..) |
NtLiteral(..) |
NtPath(..)
),
_ => false,
}
}

/// Returns `true` if the token can appear at the start of a pattern.
///
/// Shamelessly borrowed from `can_begin_expr`, only used for diagnostics right now.
pub fn can_begin_pattern(&self) -> bool {
match self.uninterpolate().kind {
Ident(name, is_raw) =>
ident_can_begin_expr(name, self.span, is_raw), // value name or keyword
| OpenDelim(Delimiter::Bracket | Delimiter::Parenthesis) // tuple or array
| Literal(..) // literal
| BinOp(Minus) // unary minus
| BinOp(And) // reference
| AndAnd // double reference
// DotDotDot is no longer supported
| DotDot | DotDotDot | DotDotEq // ranges
| Lt | BinOp(Shl) // associated path
| PathSep => true, // global path
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) |
NtPat(..) |
NtBlock(..) |
NtPath(..)),
pub fn can_begin_pattern(&self, pat_kind: NtPatKind) -> bool {
match &self.uninterpolate().kind {
// box, ref, mut, and other identifiers (can stricten)
Ident(..) | NtIdent(..) |
OpenDelim(Delimiter::Parenthesis) | // tuple pattern
OpenDelim(Delimiter::Bracket) | // slice pattern
BinOp(And) | // reference
BinOp(Minus) | // negative literal
AndAnd | // double reference
Literal(_) | // literal
DotDot | // range pattern (future compat)
DotDotDot | // range pattern (future compat)
PathSep | // path
Lt | // path (UFCS constant)
BinOp(Shl) => true, // path (double UFCS)
// leading vert `|` or-pattern
BinOp(Or) => matches!(pat_kind, PatWithOr),
Interpolated(nt) =>
matches!(&**nt,
| NtExpr(..)
| NtLiteral(..)
| NtMeta(..)
| NtPat(..)
| NtPath(..)
| NtTy(..)
),
_ => false,
}
}
Expand Down
20 changes: 1 addition & 19 deletions compiler/rustc_parse/src/parser/nonterminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,7 @@ impl<'a> Parser<'a> {
token::Interpolated(nt) => may_be_ident(nt),
_ => false,
},
NonterminalKind::Pat(pat_kind) => match &token.kind {
// box, ref, mut, and other identifiers (can stricten)
token::Ident(..) | token::NtIdent(..) |
token::OpenDelim(Delimiter::Parenthesis) | // tuple pattern
token::OpenDelim(Delimiter::Bracket) | // slice pattern
token::BinOp(token::And) | // reference
token::BinOp(token::Minus) | // negative literal
token::AndAnd | // double reference
token::Literal(_) | // literal
token::DotDot | // range pattern (future compat)
token::DotDotDot | // range pattern (future compat)
token::PathSep | // path
token::Lt | // path (UFCS constant)
token::BinOp(token::Shl) => true, // path (double UFCS)
// leading vert `|` or-pattern
token::BinOp(token::Or) => matches!(pat_kind, PatWithOr),
token::Interpolated(nt) => may_be_ident(nt),
_ => false,
},
NonterminalKind::Pat(pat_kind) => token.can_begin_pattern(pat_kind),
NonterminalKind::Lifetime => match &token.kind {
token::Lifetime(_) | token::NtLifetime(..) => true,
_ => false,
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,11 @@ impl<'a> Parser<'a> {

let mut lo = self.token.span;

if self.token.is_keyword(kw::Let) && self.look_ahead(1, |tok| tok.can_begin_pattern()) {
if self.token.is_keyword(kw::Let)
&& self.look_ahead(1, |tok| {
tok.can_begin_pattern(token::NtPatKind::PatParam { inferred: false })
})
{
self.bump();
self.dcx().emit_err(RemoveLet { span: lo });
lo = self.token.span;
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,10 @@ impl<'a> Parser<'a> {
if self.may_recover()
&& prev_token_before_parsing == token::PathSep
&& (style == PathStyle::Expr && self.token.can_begin_expr()
|| style == PathStyle::Pat && self.token.can_begin_pattern())
|| style == PathStyle::Pat
&& self.token.can_begin_pattern(token::NtPatKind::PatParam {
inferred: false,
}))
{
snapshot = Some(self.create_snapshot_for_diagnostic());
}
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/pattern/patterns-dont-match-nt-statement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@ check-pass

// Make sure that a `stmt` nonterminal does not eagerly match against
// a `pat`, since this will always cause a parse error...

macro_rules! m {
($pat:pat) => {};
($stmt:stmt) => {};
}

macro_rules! m2 {
($stmt:stmt) => {
m! { $stmt }
};
}

m2! { let x = 1 }

fn main() {}
Loading