Skip to content
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

Stabilize Ident::new_raw #75084

Merged
merged 1 commit into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 5 additions & 2 deletions library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
}
Expand Down
35 changes: 35 additions & 0 deletions src/test/ui/proc-macro/auxiliary/raw-ident.rs
Original file line number Diff line number Diff line change
@@ -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!()
}
}
16 changes: 16 additions & 0 deletions src/test/ui/proc-macro/raw-ident.rs
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions src/test/ui/proc-macro/raw-ident.stderr
Original file line number Diff line number Diff line change
@@ -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