-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix const items not being allowed to be called
r#move
or r#static
Because of an ambiguity with const closures, the parser needs to ensure that for a const item, the `const` keyword isn't followed by a `move` or `static` keyword, as that would indicate a const closure: ```rust fn main() { const move // ... } ``` This check did not take raw identifiers into account, therefore being unable to distinguish between `const move` and `const r#move`. The latter is obviously not a const closure, so it should be allowed as a const item. This fixes the check in the parser to only treat `const ...` as a const closure if it's followed by the *proper keyword*, and not a raw identifier. Additionally, this adds a large test that tests for all raw identifiers in all kinds of positions, including `const`, to prevent issues like this one from occurring again.
- Loading branch information
Showing
3 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
//@ check-pass | ||
// To make everything a keyword: | ||
//@ edition:2024 | ||
|
||
// Ensure that all (usable as identifier) keywords work as raw identifiers in all positions. | ||
// This was motivated by issue #137128, where `r#move`/`r#static`` did not work as `const` names | ||
// due to a parser check not acounting for raw identifiers. | ||
|
||
#![crate_type = "lib"] | ||
#![allow(dead_code, nonstandard_style)] | ||
|
||
macro_rules! with_keywords { | ||
($macro:ident) => { | ||
$macro!(r#break); | ||
$macro!(r#const); | ||
$macro!(r#continue); | ||
$macro!(r#else); | ||
$macro!(r#enum); | ||
$macro!(r#extern); | ||
$macro!(r#false); | ||
$macro!(r#fn); | ||
$macro!(r#for); | ||
$macro!(r#if); | ||
$macro!(r#impl); | ||
$macro!(r#in); | ||
$macro!(r#let); | ||
$macro!(r#loop); | ||
$macro!(r#match); | ||
$macro!(r#mod); | ||
$macro!(r#move); | ||
$macro!(r#mut); | ||
$macro!(r#pub); | ||
$macro!(r#ref); | ||
$macro!(r#return); | ||
$macro!(r#static); | ||
$macro!(r#struct); | ||
$macro!(r#trait); | ||
$macro!(r#true); | ||
$macro!(r#type); | ||
$macro!(r#unsafe); | ||
$macro!(r#use); | ||
$macro!(r#where); | ||
$macro!(r#while); | ||
$macro!(r#abstract); | ||
$macro!(r#become); | ||
$macro!(r#box); | ||
$macro!(r#do); | ||
$macro!(r#final); | ||
$macro!(r#macro); | ||
$macro!(r#override); | ||
$macro!(r#priv); | ||
$macro!(r#typeof); | ||
$macro!(r#unsized); | ||
$macro!(r#virtual); | ||
$macro!(r#yield); | ||
$macro!(r#async); | ||
$macro!(r#await); | ||
$macro!(r#dyn); | ||
$macro!(r#gen); | ||
$macro!(r#try); | ||
|
||
// Weak keywords: | ||
$macro!(auto); | ||
$macro!(builtin); | ||
$macro!(catch); | ||
$macro!(default); | ||
$macro!(macro_rules); | ||
$macro!(raw); | ||
$macro!(reuse); | ||
$macro!(contract_ensures); | ||
$macro!(contract_requires); | ||
$macro!(safe); | ||
$macro!(union); | ||
$macro!(yeet); | ||
}; | ||
} | ||
|
||
// NOTE: It is vital to only use a `tt` fragment to avoid confusing | ||
// the parser with nonterminals that can mask bugs. | ||
|
||
macro_rules! callback { | ||
($kw:tt) => { | ||
mod $kw { | ||
mod const_item { | ||
const $kw: () = (); | ||
} | ||
mod static_item { | ||
static $kw: () = (); | ||
} | ||
mod fn_item { | ||
fn $kw() {} | ||
} | ||
mod mod_and_use_item { | ||
mod $kw { | ||
use super::$kw; | ||
} | ||
} | ||
mod ty_alias_item { | ||
type $kw = (); | ||
} | ||
mod struct_item { | ||
struct $kw { $kw: () } | ||
} | ||
mod enum_item { | ||
enum $kw { $kw } | ||
} | ||
mod union_item { | ||
union $kw { $kw: () } | ||
} | ||
mod trait_item { | ||
trait $kw { | ||
fn $kw() {} | ||
} | ||
} | ||
mod generics_and_impl { | ||
struct A<$kw>($kw); | ||
enum B<$kw> { A($kw) } | ||
trait Tr<$kw> { | ||
type $kw; | ||
} | ||
|
||
impl<$kw> Tr<$kw> for A<$kw> { | ||
type $kw = (); | ||
} | ||
impl<$kw> B<$kw> {} | ||
} | ||
mod extern_crate { | ||
#[cfg(any())] | ||
extern crate $kw; | ||
} | ||
mod body { | ||
fn expr() { | ||
let $kw = 0; | ||
assert_eq!($kw, 0); | ||
} | ||
fn pat_const() { | ||
const $kw: u8 = 0; | ||
|
||
// Ensure that $kw actually matches the constant. | ||
#[forbid(unreachable_patterns)] | ||
match 1 { | ||
$kw => {} | ||
_ => {} | ||
} | ||
} | ||
fn pat_binding() { | ||
match 1 { | ||
$kw => {} | ||
_ => {} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
with_keywords!(callback); |