Skip to content

Commit 622759d

Browse files
authored
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
2 parents 4eb9253 + 6deda6a commit 622759d

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

library/proc_macro/src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ impl Ident {
848848
/// Creates a new `Ident` with the given `string` as well as the specified
849849
/// `span`.
850850
/// The `string` argument must be a valid identifier permitted by the
851-
/// language, otherwise the function will panic.
851+
/// language (including keywords, e.g. `self` or `fn`). Otherwise, the function will panic.
852852
///
853853
/// Note that `span`, currently in rustc, configures the hygiene information
854854
/// for this identifier.
@@ -870,7 +870,10 @@ impl Ident {
870870
}
871871

872872
/// Same as `Ident::new`, but creates a raw identifier (`r#ident`).
873-
#[unstable(feature = "proc_macro_raw_ident", issue = "54723")]
873+
/// The `string` argument be a valid identifier permitted by the language
874+
/// (including keywords, e.g. `fn`). Keywords which are usable in path segments
875+
/// (e.g. `self`, `super`) are not supported, and will cause a panic.
876+
#[stable(feature = "proc_macro_raw_ident", since = "1.47.0")]
874877
pub fn new_raw(string: &str, span: Span) -> Ident {
875878
Ident(bridge::client::Ident::new(string, span.0, true))
876879
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
use proc_macro::{TokenStream, TokenTree, Ident, Punct, Spacing, Span};
8+
9+
#[proc_macro]
10+
pub fn make_struct(input: TokenStream) -> TokenStream {
11+
match input.into_iter().next().unwrap() {
12+
TokenTree::Ident(ident) => {
13+
vec![
14+
TokenTree::Ident(Ident::new("struct", Span::call_site())),
15+
TokenTree::Ident(Ident::new_raw(&ident.to_string(), Span::call_site())),
16+
TokenTree::Punct(Punct::new(';', Spacing::Alone))
17+
].into_iter().collect()
18+
}
19+
_ => panic!()
20+
}
21+
}
22+
23+
#[proc_macro]
24+
pub fn make_bad_struct(input: TokenStream) -> TokenStream {
25+
match input.into_iter().next().unwrap() {
26+
TokenTree::Ident(ident) => {
27+
vec![
28+
TokenTree::Ident(Ident::new_raw("struct", Span::call_site())),
29+
TokenTree::Ident(Ident::new(&ident.to_string(), Span::call_site())),
30+
TokenTree::Punct(Punct::new(';', Spacing::Alone))
31+
].into_iter().collect()
32+
}
33+
_ => panic!()
34+
}
35+
}

src/test/ui/proc-macro/raw-ident.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// aux-build:raw-ident.rs
2+
3+
#[macro_use] extern crate raw_ident;
4+
5+
fn main() {
6+
make_struct!(fn);
7+
make_struct!(Foo);
8+
make_struct!(await);
9+
10+
r#fn;
11+
r#Foo;
12+
Foo;
13+
r#await;
14+
15+
make_bad_struct!(S); //~ ERROR expected one of
16+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `S`
2+
--> $DIR/raw-ident.rs:15:5
3+
|
4+
LL | make_bad_struct!(S);
5+
| ^^^^^^^^^^^^^^^^^^^^ expected one of 8 possible tokens
6+
|
7+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)