Skip to content

Commit c586fe4

Browse files
committed
macro_rules: Add more tests for using tt in addition to ident
Generally, `tt` and `ident` should behave identically, modulo the latter accepting only a subset of token trees.
1 parent 205fb75 commit c586fe4

19 files changed

+159
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// check-pass
21
// edition:2018
32
// aux-build:anon-params-edition-hygiene.rs
43

@@ -8,6 +7,8 @@
87
#[macro_use]
98
extern crate anon_params_edition_hygiene;
109

11-
generate_trait_2015!(u8);
10+
generate_trait_2015_ident!(u8);
11+
// FIXME: Edition hygiene doesn't work correctly with `tt`s in this case.
12+
generate_trait_2015_tt!(u8); //~ ERROR expected one of `:`, `@`, or `|`, found `)`
1213

1314
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: expected one of `:`, `@`, or `|`, found `)`
2+
--> $DIR/anon-params-edition-hygiene.rs:12:1
3+
|
4+
LL | generate_trait_2015_tt!(u8);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected one of `:`, `@`, or `|`
6+
|
7+
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
8+
= note: this error originates in the macro `generate_trait_2015_tt` (in Nightly builds, run with -Z macro-backtrace for more info)
9+
help: if this is a `self` type, give it a parameter name
10+
|
11+
LL | generate_trait_2015_tt!(self: u8);
12+
| +++++
13+
help: if this is a parameter name, give it a type
14+
|
15+
LL | generate_trait_2015_tt!(u8: TypeName);
16+
| ++++++++++
17+
help: if this is a type, explicitly ignore the parameter name
18+
|
19+
LL | generate_trait_2015_tt!(_: u8);
20+
| ++
21+
22+
error: aborting due to 1 previous error
23+

tests/ui/anon-params/auxiliary/anon-params-edition-hygiene.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
// edition:2015
22

33
#[macro_export]
4-
macro_rules! generate_trait_2015 {
4+
macro_rules! generate_trait_2015_ident {
55
($Type: ident) => {
6-
trait Trait {
6+
trait Trait1 {
7+
fn method($Type) {}
8+
}
9+
};
10+
}
11+
12+
#[macro_export]
13+
macro_rules! generate_trait_2015_tt {
14+
($Type: tt) => {
15+
trait Trait2 {
716
fn method($Type) {}
817
}
918
};

tests/ui/editions/auxiliary/edition-kw-macro-2015.rs

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ macro_rules! consumes_async_raw {
2626
macro_rules! passes_ident {
2727
($i: ident) => ($i)
2828
}
29+
30+
#[macro_export]
31+
macro_rules! passes_tt {
32+
($i: tt) => ($i)
33+
}

tests/ui/editions/auxiliary/edition-kw-macro-2018.rs

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ macro_rules! consumes_async_raw {
2626
macro_rules! passes_ident {
2727
($i: ident) => ($i)
2828
}
29+
30+
#[macro_export]
31+
macro_rules! passes_tt {
32+
($i: tt) => ($i)
33+
}

tests/ui/editions/edition-keywords-2015-2015-parsing.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn check_async() {
1919

2020
if passes_ident!(async) == 1 {} // OK
2121
if passes_ident!(r#async) == 1 {} // OK
22+
if passes_tt!(async) == 1 {} // OK
23+
if passes_tt!(r#async) == 1 {} // OK
2224
module::async(); // OK
2325
module::r#async(); // OK
2426
}

tests/ui/editions/edition-keywords-2015-2015.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub fn check_async() {
2020

2121
if passes_ident!(async) == 1 {} // OK
2222
if passes_ident!(r#async) == 1 {} // OK
23+
if passes_tt!(async) == 1 {} // OK
24+
if passes_tt!(r#async) == 1 {} // OK
2325
one_async::async(); // OK
2426
one_async::r#async(); // OK
2527
two_async::async(); // OK

tests/ui/editions/edition-keywords-2015-2018-parsing.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn check_async() {
1919

2020
if passes_ident!(async) == 1 {} // OK
2121
if passes_ident!(r#async) == 1 {} // OK
22+
if passes_tt!(async) == 1 {} // OK
23+
if passes_tt!(r#async) == 1 {} // OK
2224
module::async(); // OK
2325
module::r#async(); // OK
2426
}

tests/ui/editions/edition-keywords-2015-2018.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub fn check_async() {
2020

2121
if passes_ident!(async) == 1 {} // OK
2222
if passes_ident!(r#async) == 1 {} // OK
23+
if passes_tt!(async) == 1 {} // OK
24+
if passes_tt!(r#async) == 1 {} // OK
2325
// one_async::async(); // ERROR, unresolved name
2426
// one_async::r#async(); // ERROR, unresolved name
2527
two_async::async(); // OK

tests/ui/editions/edition-keywords-2018-2015-parsing.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ pub fn check_async() {
2121
r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
2222
r#async = consumes_async_raw!(r#async); // OK
2323

24-
if passes_ident!(async) == 1 {}
24+
if passes_ident!(async) == 1 {} // FIXME: Edition hygiene bug, async here is 2018 and reserved
2525
if passes_ident!(r#async) == 1 {} // OK
26+
if passes_tt!(async) == 1 {} //~ ERROR macro expansion ends with an incomplete expression
27+
if passes_tt!(r#async) == 1 {} // OK
2628
module::async(); //~ ERROR expected identifier, found keyword `async`
2729
module::r#async(); // OK
2830

tests/ui/editions/edition-keywords-2018-2015-parsing.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | let mut r#async = 1;
1010
| ++
1111

1212
error: expected identifier, found keyword `async`
13-
--> $DIR/edition-keywords-2018-2015-parsing.rs:26:13
13+
--> $DIR/edition-keywords-2018-2015-parsing.rs:28:13
1414
|
1515
LL | module::async();
1616
| ^^^^^ expected identifier, found keyword
@@ -52,17 +52,23 @@ LL | ($i: ident) => ($i)
5252
|
5353
::: $DIR/edition-keywords-2018-2015-parsing.rs:24:8
5454
|
55-
LL | if passes_ident!(async) == 1 {}
55+
LL | if passes_ident!(async) == 1 {} // FIXME: Edition hygiene bug, async here is 2018 and reserved
5656
| -------------------- in this macro invocation
5757

58+
error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
59+
--> $DIR/edition-keywords-2018-2015-parsing.rs:26:24
60+
|
61+
LL | if passes_tt!(async) == 1 {}
62+
| ^ expected one of `move`, `|`, or `||`
63+
5864
error[E0308]: mismatched types
59-
--> $DIR/edition-keywords-2018-2015-parsing.rs:29:33
65+
--> $DIR/edition-keywords-2018-2015-parsing.rs:31:33
6066
|
6167
LL | let _recovery_witness: () = 0;
6268
| -- ^ expected `()`, found integer
6369
| |
6470
| expected due to this
6571

66-
error: aborting due to 6 previous errors
72+
error: aborting due to 7 previous errors
6773

6874
For more information about this error, try `rustc --explain E0308`.

tests/ui/editions/edition-keywords-2018-2015.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub fn check_async() {
1818

1919
// if passes_ident!(async) == 1 {} // ERROR, reserved
2020
if passes_ident!(r#async) == 1 {} // OK
21+
// if passes_tt!(async) == 1 {} // ERROR, reserved
22+
if passes_tt!(r#async) == 1 {} // OK
2123
// one_async::async(); // ERROR, reserved
2224
one_async::r#async(); // OK
2325
// two_async::async(); // ERROR, reserved

tests/ui/editions/edition-keywords-2018-2018-parsing.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ mod module {
1212
pub fn r#async() {}
1313
}
1414

15+
macro_rules! local_passes_ident {
16+
($i: ident) => ($i) //~ ERROR macro expansion ends with an incomplete expression
17+
}
18+
macro_rules! local_passes_tt {
19+
($i: tt) => ($i) //~ ERROR macro expansion ends with an incomplete expression
20+
}
21+
1522
pub fn check_async() {
1623
let mut async = 1; //~ ERROR expected identifier, found keyword `async`
1724
let mut r#async = 1; // OK
@@ -21,8 +28,14 @@ pub fn check_async() {
2128
r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
2229
r#async = consumes_async_raw!(r#async); // OK
2330

24-
if passes_ident!(async) == 1 {}
31+
if passes_ident!(async) == 1 {} // FIXME: Edition hygiene bug, async here is 2018 and reserved
2532
if passes_ident!(r#async) == 1 {} // OK
33+
if passes_tt!(async) == 1 {} //~ ERROR macro expansion ends with an incomplete expression
34+
if passes_tt!(r#async) == 1 {} // OK
35+
if local_passes_ident!(async) == 1 {} // Error reported above in the macro
36+
if local_passes_ident!(r#async) == 1 {} // OK
37+
if local_passes_tt!(async) == 1 {} // Error reported above in the macro
38+
if local_passes_tt!(r#async) == 1 {} // OK
2639
module::async(); //~ ERROR expected identifier, found keyword `async`
2740
module::r#async(); // OK
2841

tests/ui/editions/edition-keywords-2018-2018-parsing.stderr

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: expected identifier, found keyword `async`
2-
--> $DIR/edition-keywords-2018-2018-parsing.rs:16:13
2+
--> $DIR/edition-keywords-2018-2018-parsing.rs:23:13
33
|
44
LL | let mut async = 1;
55
| ^^^^^ expected identifier, found keyword
@@ -10,7 +10,7 @@ LL | let mut r#async = 1;
1010
| ++
1111

1212
error: expected identifier, found keyword `async`
13-
--> $DIR/edition-keywords-2018-2018-parsing.rs:26:13
13+
--> $DIR/edition-keywords-2018-2018-parsing.rs:39:13
1414
|
1515
LL | module::async();
1616
| ^^^^^ expected identifier, found keyword
@@ -21,7 +21,7 @@ LL | module::r#async();
2121
| ++
2222

2323
error: no rules expected the token `r#async`
24-
--> $DIR/edition-keywords-2018-2018-parsing.rs:20:31
24+
--> $DIR/edition-keywords-2018-2018-parsing.rs:27:31
2525
|
2626
LL | r#async = consumes_async!(r#async);
2727
| ^^^^^^^ no rules expected this token in macro call
@@ -33,7 +33,7 @@ LL | (async) => (1)
3333
| ^^^^^
3434

3535
error: no rules expected the token `async`
36-
--> $DIR/edition-keywords-2018-2018-parsing.rs:21:35
36+
--> $DIR/edition-keywords-2018-2018-parsing.rs:28:35
3737
|
3838
LL | r#async = consumes_async_raw!(async);
3939
| ^^^^^ no rules expected this token in macro call
@@ -50,19 +50,37 @@ error: macro expansion ends with an incomplete expression: expected one of `move
5050
LL | ($i: ident) => ($i)
5151
| ^ expected one of `move`, `|`, or `||`
5252
|
53-
::: $DIR/edition-keywords-2018-2018-parsing.rs:24:8
53+
::: $DIR/edition-keywords-2018-2018-parsing.rs:31:8
5454
|
55-
LL | if passes_ident!(async) == 1 {}
55+
LL | if passes_ident!(async) == 1 {} // FIXME: Edition hygiene bug, async here is 2018 and reserved
5656
| -------------------- in this macro invocation
5757

58+
error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
59+
--> $DIR/edition-keywords-2018-2018-parsing.rs:33:24
60+
|
61+
LL | if passes_tt!(async) == 1 {}
62+
| ^ expected one of `move`, `|`, or `||`
63+
64+
error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
65+
--> $DIR/edition-keywords-2018-2018-parsing.rs:16:23
66+
|
67+
LL | ($i: ident) => ($i)
68+
| ^ expected one of `move`, `|`, or `||`
69+
70+
error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
71+
--> $DIR/edition-keywords-2018-2018-parsing.rs:19:20
72+
|
73+
LL | ($i: tt) => ($i)
74+
| ^ expected one of `move`, `|`, or `||`
75+
5876
error[E0308]: mismatched types
59-
--> $DIR/edition-keywords-2018-2018-parsing.rs:29:33
77+
--> $DIR/edition-keywords-2018-2018-parsing.rs:42:33
6078
|
6179
LL | let _recovery_witness: () = 0;
6280
| -- ^ expected `()`, found integer
6381
| |
6482
| expected due to this
6583

66-
error: aborting due to 6 previous errors
84+
error: aborting due to 9 previous errors
6785

6886
For more information about this error, try `rustc --explain E0308`.

tests/ui/editions/edition-keywords-2018-2018.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub fn check_async() {
1818

1919
// if passes_ident!(async) == 1 {} // ERROR, reserved
2020
if passes_ident!(r#async) == 1 {} // OK
21+
// if passes_tt!(async) == 1 {} // ERROR, reserved
22+
if passes_tt!(r#async) == 1 {} // OK
2123
// one_async::async(); // ERROR, reserved
2224
// one_async::r#async(); // ERROR, unresolved name
2325
// two_async::async(); // ERROR, reserved

tests/ui/lint/wide_pointer_comparisons.rs

+9
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ fn main() {
107107
//~^ WARN ambiguous wide pointer comparison
108108
}
109109

110+
{
111+
macro_rules! cmp {
112+
($a:tt, $b:tt) => { $a == $b }
113+
//~^ WARN ambiguous wide pointer comparison
114+
}
115+
116+
cmp!(a, b);
117+
}
118+
110119
{
111120
macro_rules! cmp {
112121
($a:ident, $b:ident) => { $a == $b }

tests/ui/lint/wide_pointer_comparisons.stderr

+14-3
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,18 @@ LL | std::ptr::eq(*a, *b)
421421
| ~~~~~~~~~~~~~ ~ +
422422

423423
warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
424-
--> $DIR/wide_pointer_comparisons.rs:112:39
424+
--> $DIR/wide_pointer_comparisons.rs:112:33
425+
|
426+
LL | ($a:tt, $b:tt) => { $a == $b }
427+
| ^^^^^^^^
428+
|
429+
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
430+
|
431+
LL | ($a:tt, $b:tt) => { std::ptr::addr_eq($a, $b) }
432+
| ++++++++++++++++++ ~ +
433+
434+
warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
435+
--> $DIR/wide_pointer_comparisons.rs:121:39
425436
|
426437
LL | ($a:ident, $b:ident) => { $a == $b }
427438
| ^^^^^^^^
@@ -436,7 +447,7 @@ LL | ($a:ident, $b:ident) => { std::ptr::addr_eq($a, $b) }
436447
| ++++++++++++++++++ ~ +
437448

438449
warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
439-
--> $DIR/wide_pointer_comparisons.rs:122:37
450+
--> $DIR/wide_pointer_comparisons.rs:131:37
440451
|
441452
LL | ($a:expr, $b:expr) => { $a == $b }
442453
| ^^
@@ -448,5 +459,5 @@ LL | cmp!(&a, &b);
448459
= help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
449460
= note: this warning originates in the macro `cmp` (in Nightly builds, run with -Z macro-backtrace for more info)
450461

451-
warning: 37 warnings emitted
462+
warning: 38 warnings emitted
452463

tests/ui/methods/method-on-ambiguous-numeric-type.rs

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
macro_rules! local_mac {
66
($ident:ident) => { let $ident = 42; }
77
}
8+
macro_rules! local_mac_tt {
9+
($tt:tt) => { let $tt = 42; }
10+
}
811

912
fn main() {
1013
let x = 2.0.neg();
@@ -23,6 +26,10 @@ fn main() {
2326
local_mac!(local_bar);
2427
local_bar.pow(2);
2528
//~^ ERROR can't call method `pow` on ambiguous numeric type `{integer}`
29+
30+
local_mac_tt!(local_bar_tt);
31+
local_bar_tt.pow(2);
32+
//~^ ERROR can't call method `pow` on ambiguous numeric type `{integer}`
2633
}
2734

2835
fn qux() {

0 commit comments

Comments
 (0)