-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #85359 - lrh2000:reserved-prefixes, r=nikomatsakis
Reserve prefixed identifiers and literals (RFC 3101) This PR denies any identifiers immediately followed by one of three tokens `"`, `'` or `#`, which is stricter than the requirements of RFC 3101 but may be necessary according to the discussion at [Zulip]. [Zulip]: https://rust-lang.zulipchat.com/#narrow/stream/268952-edition-2021/topic/reserved.20prefixes/near/238470099 The tracking issue #84599 says we'll add a feature gate named `reserved_prefixes`, but I don't think I can do this because it is impossible for the lexer to know whether a feature is enabled or not. I guess determining the behavior by the edition information should be enough. Fixes #84599
- Loading branch information
Showing
16 changed files
with
518 additions
and
9 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
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
25 changes: 25 additions & 0 deletions
25
src/test/ui/rust-2021/auxiliary/reserved-prefixes-macro-2018.rs
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,25 @@ | ||
// force-host | ||
// edition:2018 | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
use std::str::FromStr; | ||
|
||
#[proc_macro] | ||
pub fn number_of_tokens_in_a_prefixed_integer_literal(_: TokenStream) -> TokenStream { | ||
TokenStream::from_str("hey#123").unwrap().into_iter().count().to_string().parse().unwrap() | ||
} | ||
|
||
#[proc_macro] | ||
pub fn number_of_tokens_in_a_prefixed_char_literal(_: TokenStream) -> TokenStream { | ||
TokenStream::from_str("hey#'a'").unwrap().into_iter().count().to_string().parse().unwrap() | ||
} | ||
|
||
#[proc_macro] | ||
pub fn number_of_tokens_in_a_prefixed_string_literal(_: TokenStream) -> TokenStream { | ||
TokenStream::from_str("hey#\"abc\"").unwrap().into_iter().count().to_string().parse().unwrap() | ||
} |
25 changes: 25 additions & 0 deletions
25
src/test/ui/rust-2021/auxiliary/reserved-prefixes-macro-2021.rs
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,25 @@ | ||
// force-host | ||
// edition:2021 | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
use std::str::FromStr; | ||
|
||
#[proc_macro] | ||
pub fn number_of_tokens_in_a_prefixed_integer_literal(_: TokenStream) -> TokenStream { | ||
TokenStream::from_str("hey#123").unwrap().into_iter().count().to_string().parse().unwrap() | ||
} | ||
|
||
#[proc_macro] | ||
pub fn number_of_tokens_in_a_prefixed_char_literal(_: TokenStream) -> TokenStream { | ||
TokenStream::from_str("hey#'a'").unwrap().into_iter().count().to_string().parse().unwrap() | ||
} | ||
|
||
#[proc_macro] | ||
pub fn number_of_tokens_in_a_prefixed_string_literal(_: TokenStream) -> TokenStream { | ||
TokenStream::from_str("hey#\"abc\"").unwrap().into_iter().count().to_string().parse().unwrap() | ||
} |
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,38 @@ | ||
// check-pass | ||
// run-rustfix | ||
// compile-flags: -Z unstable-options --edition 2018 | ||
|
||
#![warn(reserved_prefix)] | ||
|
||
macro_rules! m2 { | ||
($a:tt $b:tt) => {}; | ||
} | ||
|
||
macro_rules! m3 { | ||
($a:tt $b:tt $c:tt) => {}; | ||
} | ||
|
||
fn main() { | ||
m2!(z "hey"); | ||
//~^ WARNING prefix `z` is unknown [reserved_prefix] | ||
//~| WARNING hard error in Rust 2021 | ||
m2!(prefix "hey"); | ||
//~^ WARNING prefix `prefix` is unknown [reserved_prefix] | ||
//~| WARNING hard error in Rust 2021 | ||
m3!(hey #123); | ||
//~^ WARNING prefix `hey` is unknown [reserved_prefix] | ||
//~| WARNING hard error in Rust 2021 | ||
m3!(hey #hey); | ||
//~^ WARNING prefix `hey` is unknown [reserved_prefix] | ||
//~| WARNING hard error in Rust 2021 | ||
} | ||
|
||
macro_rules! quote { | ||
(# name = # kind # value) => {}; | ||
} | ||
|
||
quote! { | ||
#name = #kind #value | ||
//~^ WARNING prefix `kind` is unknown [reserved_prefix] | ||
//~| WARNING hard error in Rust 2021 | ||
} |
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,38 @@ | ||
// check-pass | ||
// run-rustfix | ||
// compile-flags: -Z unstable-options --edition 2018 | ||
|
||
#![warn(reserved_prefix)] | ||
|
||
macro_rules! m2 { | ||
($a:tt $b:tt) => {}; | ||
} | ||
|
||
macro_rules! m3 { | ||
($a:tt $b:tt $c:tt) => {}; | ||
} | ||
|
||
fn main() { | ||
m2!(z"hey"); | ||
//~^ WARNING prefix `z` is unknown [reserved_prefix] | ||
//~| WARNING hard error in Rust 2021 | ||
m2!(prefix"hey"); | ||
//~^ WARNING prefix `prefix` is unknown [reserved_prefix] | ||
//~| WARNING hard error in Rust 2021 | ||
m3!(hey#123); | ||
//~^ WARNING prefix `hey` is unknown [reserved_prefix] | ||
//~| WARNING hard error in Rust 2021 | ||
m3!(hey#hey); | ||
//~^ WARNING prefix `hey` is unknown [reserved_prefix] | ||
//~| WARNING hard error in Rust 2021 | ||
} | ||
|
||
macro_rules! quote { | ||
(# name = # kind # value) => {}; | ||
} | ||
|
||
quote! { | ||
#name = #kind#value | ||
//~^ WARNING prefix `kind` is unknown [reserved_prefix] | ||
//~| WARNING hard error in Rust 2021 | ||
} |
Oops, something went wrong.