Skip to content
Open
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
359 changes: 276 additions & 83 deletions library/proc_macro/src/quote.rs

Large diffs are not rendered by default.

94 changes: 86 additions & 8 deletions tests/ui/proc-macro/quote/auxiliary/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ use proc_macro::*;

#[proc_macro]
pub fn run_tests(_: TokenStream) -> TokenStream {
test_sep1();
test_sep2();
test_sep_group1();
test_sep_group2();
test_dollar_dollar1();
test_dollar_dollar2();
test_dollar_dollar3();
test_dollar_dollar4();

test_quote_impl();
test_substitution();
test_iter();
Expand Down Expand Up @@ -50,6 +59,75 @@ pub fn run_tests(_: TokenStream) -> TokenStream {
TokenStream::new()
}

fn test_sep1() {
let iter = ["a", "b"].into_iter();
let tokens = quote!($($iter) << *);

let expected = "\"a\" << \"b\"";
assert_eq!(expected, tokens.to_string());
}

fn test_sep2() {
let iter1 = ["a", "b"].into_iter();
let iter2 = [1, 2, 3];
let tokens = quote!($($iter1) ($($iter1) $($iter2),* A) *);

let expected = "\"a\" ($(\"b\") 1i32 , 2i32 , 3i32 A) \"b\"";
assert_eq!(expected, tokens.to_string());
}

fn test_sep_group1() {
let x = "X";
let iter = ["a", "b"].into_iter();
let tokens = quote!($($x) >> $($iter) << *);

let expected = "$(\"X\") >> \"a\" << \"b\"";
assert_eq!(expected, tokens.to_string());
}

fn test_sep_group2() {
let x = "X";
let iter = ["a", "b"].into_iter();
let tokens = quote!($($x) >> > $($iter) << *);

let expected = "$(\"X\") >> > \"a\" << \"b\"";
assert_eq!(expected, tokens.to_string());
}

fn test_dollar_dollar1() {
let iter = ["a", "b"].into_iter();
let tokens = quote!($$ x $($$ x $iter)*);

let expected = "$x $x \"a\" $x \"b\"";
assert_eq!(expected, tokens.to_string());
}

fn test_dollar_dollar2() {
let iter = ["a", "b", "c"].into_iter();
let tokens = quote!($($iter) $$ *);

let expected = "\"a\" $ \"b\" $ \"c\"";
assert_eq!(expected, tokens.to_string());
}

fn test_dollar_dollar3() {
let x = "X";
let iter = ["a", "b", "c"].into_iter();
let tokens = quote!($($x)$$($x),*);

let expected = "$(\"X\") $ (\"X\"),*";
assert_eq!(expected, tokens.to_string());
}

fn test_dollar_dollar4() {
let x = "X";
let iter = ["a", "b", "c"].into_iter();
let tokens = quote!($($x)$$y$($iter)*);

let expected = "$(\"X\") $y \"a\" \"b\" \"c\"";
assert_eq!(expected, tokens.to_string());
}

// Based on https://github.com/dtolnay/quote/blob/0245506323a3616daa2ee41c6ad0b871e4d78ae4/tests/test.rs
//
// FIXME(quote):
Expand Down Expand Up @@ -106,7 +184,7 @@ fn test_iter() {

assert_eq!("X, X, X, X,", quote!($($primes,)*).to_string());

assert_eq!("X, X, X, X", quote!($($primes),*).to_string());
assert_eq!("X , X , X , X", quote!($($primes),*).to_string());
}

fn test_array() {
Expand Down Expand Up @@ -320,7 +398,7 @@ fn test_fancy_repetition() {
$($foo: $bar),*
};

let expected = r#""a" : true, "b" : false"#;
let expected = r#""a" : true , "b" : false"#;
assert_eq!(expected, tokens.to_string());
}

Expand All @@ -333,7 +411,7 @@ fn test_nested_fancy_repetition() {
),*
};

let expected = "'a' 'b' 'c', 'x' 'y' 'z'";
let expected = "'a' 'b' 'c' , 'x' 'y' 'z'";
assert_eq!(expected, tokens.to_string());
}

Expand All @@ -345,7 +423,7 @@ fn test_duplicate_name_repetition() {
$($foo: $foo),*
};

let expected = r#""a" : "a", "b" : "b" "a" : "a", "b" : "b""#;
let expected = r#""a" : "a" , "b" : "b" "a" : "a" , "b" : "b""#;
assert_eq!(expected, tokens.to_string());
}

Expand All @@ -356,7 +434,7 @@ fn test_duplicate_name_repetition_no_copy() {
$($foo: $foo),*
};

let expected = r#""a" : "a", "b" : "b""#;
let expected = r#""a" : "a" , "b" : "b""#;
assert_eq!(expected, tokens.to_string());
}

Expand All @@ -369,7 +447,7 @@ fn test_btreeset_repetition() {
$($set: $set),*
};

let expected = r#""a" : "a", "b" : "b""#;
let expected = r#""a" : "a" , "b" : "b""#;
assert_eq!(expected, tokens.to_string());
}

Expand All @@ -378,7 +456,7 @@ fn test_variable_name_conflict() {
// fine, if a little confusing when debugging.
let _i = vec!['a', 'b'];
let tokens = quote! { $($_i),* };
let expected = "'a', 'b'";
let expected = "'a' , 'b'";
assert_eq!(expected, tokens.to_string());
}

Expand All @@ -390,7 +468,7 @@ fn test_nonrep_in_repetition() {
$($rep $rep : $nonrep $nonrep),*
};

let expected = r#""a" "a" : "c" "c", "b" "b" : "c" "c""#;
let expected = r#""a" "a" : "c" "c" , "b" "b" : "c" "c""#;
assert_eq!(expected, tokens.to_string());
}

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/proc-macro/quote/not-quotable-repetition-group-brace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(proc_macro_quote)]

extern crate proc_macro;

use proc_macro::quote;

fn main() {
let arr = [1, 2, 3];
quote! { ${$arr}* }; //~ ERROR the trait bound `[{integer}; 3]: ToTokens` is not satisfied [E0277]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0277]: the trait bound `[{integer}; 3]: ToTokens` is not satisfied
--> $DIR/not-quotable-repetition-group-brace.rs:9:5
|
LL | quote! { ${$arr}* };
| ^^^^^^^^^^^^^^^^^^^
| |
| the trait `ToTokens` is not implemented for `[{integer}; 3]`
| required by a bound introduced by this call
|
= help: the following other types implement trait `ToTokens`:
&T
&mut T
Box<T>
CString
Cow<'_, T>
Option<T>
Rc<T>
bool
and 24 others

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
10 changes: 10 additions & 0 deletions tests/ui/proc-macro/quote/not-quotable-repetition-group-bracket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(proc_macro_quote)]

extern crate proc_macro;

use proc_macro::quote;

fn main() {
let arr = [1, 2, 3];
quote! { $[$arr]* }; //~ ERROR the trait bound `[{integer}; 3]: ToTokens` is not satisfied [E0277]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0277]: the trait bound `[{integer}; 3]: ToTokens` is not satisfied
--> $DIR/not-quotable-repetition-group-bracket.rs:9:5
|
LL | quote! { $[$arr]* };
| ^^^^^^^^^^^^^^^^^^^
| |
| the trait `ToTokens` is not implemented for `[{integer}; 3]`
| required by a bound introduced by this call
|
= help: the following other types implement trait `ToTokens`:
&T
&mut T
Box<T>
CString
Cow<'_, T>
Option<T>
Rc<T>
bool
and 24 others

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
16 changes: 16 additions & 0 deletions tests/ui/proc-macro/quote/not-quotable-repetition-group-none.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(proc_macro_quote)]

extern crate proc_macro;

use proc_macro::quote;

macro_rules! do_quote {
($dollar:tt $content:expr) => {
proc_macro::quote!($dollar $content *) //~ ERROR the trait bound `[{integer}; 3]: ToTokens` is not satisfied [E0277]
};
}

fn main() {
let arr = [1, 2, 3];
do_quote!($ f!($arr));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0277]: the trait bound `[{integer}; 3]: ToTokens` is not satisfied
--> $DIR/not-quotable-repetition-group-none.rs:9:9
|
LL | proc_macro::quote!($dollar $content *)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ToTokens` is not implemented for `[{integer}; 3]`
...
LL | do_quote!($ f!($arr));
| ---------------------
| |
| required by a bound introduced by this call
| in this macro invocation
|
= help: the following other types implement trait `ToTokens`:
&T
&mut T
Box<T>
CString
Cow<'_, T>
Option<T>
Rc<T>
bool
and 24 others
= note: this error originates in the macro `proc_macro::quote` which comes from the expansion of the macro `do_quote` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(proc_macro_quote)]

extern crate proc_macro;

use proc_macro::quote;

fn main() {
let arr = [1, 2, 3];
quote! { $($arr) $$ x .. false [] * }; //~ ERROR the trait bound `[{integer}; 3]: ToTokens` is not satisfied [E0277]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0277]: the trait bound `[{integer}; 3]: ToTokens` is not satisfied
--> $DIR/not-quotable-repetition-invalid-separator.rs:9:5
|
LL | quote! { $($arr) $$ x .. false [] * };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| the trait `ToTokens` is not implemented for `[{integer}; 3]`
| required by a bound introduced by this call
|
= help: the following other types implement trait `ToTokens`:
&T
&mut T
Box<T>
CString
Cow<'_, T>
Option<T>
Rc<T>
bool
and 24 others

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Loading