Skip to content

Commit

Permalink
proc_macro: Use ToTokens trait in quote macro
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Dec 23, 2024
1 parent 904d8f6 commit 0b32dec
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 8 deletions.
8 changes: 5 additions & 3 deletions library/proc_macro/src/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//! This quasiquoter uses macros 2.0 hygiene to reliably access
//! items from `proc_macro`, to build a `proc_macro::TokenStream`.
use crate::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use crate::{
Delimiter, Group, Ident, Literal, Punct, Spacing, Span, ToTokens, TokenStream, TokenTree,
};

macro_rules! quote_tt {
(($($t:tt)*)) => { Group::new(Delimiter::Parenthesis, quote!($($t)*)) };
Expand Down Expand Up @@ -50,7 +52,7 @@ macro_rules! quote {
() => { TokenStream::new() };
($($t:tt)*) => {
[
$(TokenStream::from(quote_ts!($t)),)*
$(ToTokens::into_token_stream(quote_ts!($t)),)*
].iter().cloned().collect::<TokenStream>()
};
}
Expand All @@ -73,7 +75,7 @@ pub fn quote(stream: TokenStream) -> TokenStream {
after_dollar = false;
match tree {
TokenTree::Ident(_) => {
return Some(quote!(Into::<crate::TokenStream>::into(
return Some(quote!(ToTokens::into_token_stream(
Clone::clone(&(@ tree))),));
}
TokenTree::Punct(ref tt) if tt.as_char() == '$' => {}
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/macros/auxiliary/issue-100199.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![feature(proc_macro_quote)]
#![feature(proc_macro_totokens)]

extern crate proc_macro;

use proc_macro::{quote, Ident, Span, TokenStream, TokenTree};
use proc_macro::{Ident, Span, TokenStream, TokenTree, quote};

#[proc_macro_attribute]
pub fn struct_with_bound(_: TokenStream, _: TokenStream) -> TokenStream {
Expand Down
1 change: 1 addition & 0 deletions tests/ui/macros/auxiliary/proc_macro_def.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(proc_macro_quote)]
#![feature(proc_macro_totokens)]

extern crate proc_macro;

Expand Down
1 change: 1 addition & 0 deletions tests/ui/proc-macro/auxiliary/cond_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(proc_macro_quote)]
#![feature(proc_macro_totokens)]

extern crate proc_macro;

Expand Down
1 change: 1 addition & 0 deletions tests/ui/proc-macro/auxiliary/double.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(proc_macro_quote)]
#![feature(proc_macro_totokens)]

extern crate proc_macro;
use proc_macro::*;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/proc-macro/auxiliary/generate-dollar-ident.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(proc_macro_quote)]
#![feature(proc_macro_totokens)]

extern crate proc_macro;
use proc_macro::*;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/proc-macro/auxiliary/hygiene_example_codegen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(proc_macro_quote)]
#![feature(proc_macro_totokens)]

extern crate proc_macro as proc_macro_renamed; // This does not break `quote!`

Expand Down
7 changes: 3 additions & 4 deletions tests/ui/proc-macro/auxiliary/mixed-site-span.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(proc_macro_quote)]
#![feature(proc_macro_totokens)]

extern crate proc_macro;
use proc_macro::*;
Expand All @@ -13,10 +14,8 @@ pub fn proc_macro_rules(input: TokenStream) -> TokenStream {
let local_use = id("local_use");
let mut single_quote = Punct::new('\'', Spacing::Joint);
single_quote.set_span(Span::mixed_site());
let label_use: TokenStream = [
TokenTree::from(single_quote),
id("label_use"),
].iter().cloned().collect();
let label_use: TokenStream =
[TokenTree::from(single_quote), id("label_use")].iter().cloned().collect();
quote!(
struct $item_def;
let $local_def = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(proc_macro_quote)]
#![feature(proc_macro_totokens)]

extern crate proc_macro;
use proc_macro::{TokenStream, quote};
Expand Down
1 change: 1 addition & 0 deletions tests/ui/proc-macro/auxiliary/resolved-located-at.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![feature(proc_macro_def_site)]
#![feature(proc_macro_diagnostic)]
#![feature(proc_macro_quote)]
#![feature(proc_macro_totokens)]

extern crate proc_macro;
use proc_macro::*;
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/proc-macro/quote-interpolation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//@ check-pass

#![feature(proc_macro_quote)]
#![feature(proc_macro_totokens)]
#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::*;

fn main() {
let x = Ident::new("foo", Span::call_site());
let _ = quote! {
let $x = 199;
};
}

0 comments on commit 0b32dec

Please sign in to comment.