diff options
| author | Yuki Okushi <huyuumi.dev@gmail.com> | 2020-08-04 09:27:06 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-04 09:27:06 +0900 |
| commit | 622759d129e7acad53cf4d54415254db7018568c (patch) | |
| tree | ce020c0fb3d5fd8fb72c2f09cdb4b50cc59ee082 | |
| parent | 4eb9253660d384cfdb73c166285bd4b3dab3f0d7 (diff) | |
| parent | 6deda6a6a05e6e6ace8fb015d610c6355efb0fd7 (diff) | |
| download | rust-622759d129e7acad53cf4d54415254db7018568c.tar.gz rust-622759d129e7acad53cf4d54415254db7018568c.zip | |
Rollup merge of #75084 - Aaron1011:stabilize/ident-new-raw, r=petrochenkov
Stabilize Ident::new_raw Tracking issue: #54723 This is a continuation of PR #59002
| -rw-r--r-- | library/proc_macro/src/lib.rs | 7 | ||||
| -rw-r--r-- | src/test/ui/proc-macro/auxiliary/raw-ident.rs | 35 | ||||
| -rw-r--r-- | src/test/ui/proc-macro/raw-ident.rs | 16 | ||||
| -rw-r--r-- | src/test/ui/proc-macro/raw-ident.stderr | 10 |
4 files changed, 66 insertions, 2 deletions
diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 42ba7f5c025..de3866d92fc 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -848,7 +848,7 @@ impl Ident { /// Creates a new `Ident` with the given `string` as well as the specified /// `span`. /// The `string` argument must be a valid identifier permitted by the - /// language, otherwise the function will panic. + /// language (including keywords, e.g. `self` or `fn`). Otherwise, the function will panic. /// /// Note that `span`, currently in rustc, configures the hygiene information /// for this identifier. @@ -870,7 +870,10 @@ impl Ident { } /// Same as `Ident::new`, but creates a raw identifier (`r#ident`). - #[unstable(feature = "proc_macro_raw_ident", issue = "54723")] + /// The `string` argument be a valid identifier permitted by the language + /// (including keywords, e.g. `fn`). Keywords which are usable in path segments + /// (e.g. `self`, `super`) are not supported, and will cause a panic. + #[stable(feature = "proc_macro_raw_ident", since = "1.47.0")] pub fn new_raw(string: &str, span: Span) -> Ident { Ident(bridge::client::Ident::new(string, span.0, true)) } diff --git a/src/test/ui/proc-macro/auxiliary/raw-ident.rs b/src/test/ui/proc-macro/auxiliary/raw-ident.rs new file mode 100644 index 00000000000..9daee21aa17 --- /dev/null +++ b/src/test/ui/proc-macro/auxiliary/raw-ident.rs @@ -0,0 +1,35 @@ +// force-host +// no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; +use proc_macro::{TokenStream, TokenTree, Ident, Punct, Spacing, Span}; + +#[proc_macro] +pub fn make_struct(input: TokenStream) -> TokenStream { + match input.into_iter().next().unwrap() { + TokenTree::Ident(ident) => { + vec![ + TokenTree::Ident(Ident::new("struct", Span::call_site())), + TokenTree::Ident(Ident::new_raw(&ident.to_string(), Span::call_site())), + TokenTree::Punct(Punct::new(';', Spacing::Alone)) + ].into_iter().collect() + } + _ => panic!() + } +} + +#[proc_macro] +pub fn make_bad_struct(input: TokenStream) -> TokenStream { + match input.into_iter().next().unwrap() { + TokenTree::Ident(ident) => { + vec![ + TokenTree::Ident(Ident::new_raw("struct", Span::call_site())), + TokenTree::Ident(Ident::new(&ident.to_string(), Span::call_site())), + TokenTree::Punct(Punct::new(';', Spacing::Alone)) + ].into_iter().collect() + } + _ => panic!() + } +} diff --git a/src/test/ui/proc-macro/raw-ident.rs b/src/test/ui/proc-macro/raw-ident.rs new file mode 100644 index 00000000000..03cb4571496 --- /dev/null +++ b/src/test/ui/proc-macro/raw-ident.rs @@ -0,0 +1,16 @@ +// aux-build:raw-ident.rs + +#[macro_use] extern crate raw_ident; + +fn main() { + make_struct!(fn); + make_struct!(Foo); + make_struct!(await); + + r#fn; + r#Foo; + Foo; + r#await; + + make_bad_struct!(S); //~ ERROR expected one of +} diff --git a/src/test/ui/proc-macro/raw-ident.stderr b/src/test/ui/proc-macro/raw-ident.stderr new file mode 100644 index 00000000000..e82a1226b5a --- /dev/null +++ b/src/test/ui/proc-macro/raw-ident.stderr @@ -0,0 +1,10 @@ +error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `S` + --> $DIR/raw-ident.rs:15:5 + | +LL | make_bad_struct!(S); + | ^^^^^^^^^^^^^^^^^^^^ expected one of 8 possible tokens + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + |
