Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/macro_expansion_tests/mbe.rs')
| -rw-r--r-- | crates/hir-def/src/macro_expansion_tests/mbe.rs | 160 |
1 files changed, 102 insertions, 58 deletions
diff --git a/crates/hir-def/src/macro_expansion_tests/mbe.rs b/crates/hir-def/src/macro_expansion_tests/mbe.rs index d090621324..9bf2a50d57 100644 --- a/crates/hir-def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir-def/src/macro_expansion_tests/mbe.rs @@ -15,7 +15,6 @@ use crate::macro_expansion_tests::check; fn token_mapping_smoke_test() { check( r#" -// +tokenids macro_rules! f { ( struct $ident:ident ) => { struct $ident { @@ -24,26 +23,22 @@ macro_rules! f { }; } -// +tokenids +// +spans+syntaxctxt f!(struct MyTraitMap2); "#, - expect![[r##" -// call ids will be shifted by Shift(30) -// +tokenids -macro_rules! f {#0 - (#1 struct#2 $#3ident#4:#5ident#6 )#1 =#7>#8 {#9 - struct#10 $#11ident#12 {#13 - map#14:#15 :#16:#17std#18:#19:#20collections#21:#22:#23HashSet#24<#25(#26)#26>#27,#28 - }#13 - }#9;#29 -}#0 - -// // +tokenids -// f!(struct#1 MyTraitMap2#2); -struct#10 MyTraitMap2#32 {#13 - map#14:#15 ::std#18::collections#21::HashSet#24<#25(#26)#26>#27,#28 -}#13 -"##]], + expect![[r#" +macro_rules! f { + ( struct $ident:ident ) => { + struct $ident { + map: ::std::collections::HashSet<()>, + } + }; +} + +struct#FileId(0):[email protected]\2# MyTraitMap2#FileId(0):[email protected]\0# {#FileId(0):[email protected]\2# + map#FileId(0):[email protected]\2#:#FileId(0):[email protected]\2# #FileId(0):[email protected]\2#::#FileId(0):[email protected]\2#std#FileId(0):[email protected]\2#::#FileId(0):[email protected]\2#collections#FileId(0):[email protected]\2#::#FileId(0):[email protected]\2#HashSet#FileId(0):[email protected]\2#<#FileId(0):[email protected]\2#(#FileId(0):[email protected]\2#)#FileId(0):[email protected]\2#>#FileId(0):[email protected]\2#,#FileId(0):[email protected]\2# +}#FileId(0):[email protected]\2# +"#]], ); } @@ -53,49 +48,42 @@ fn token_mapping_floats() { // (and related issues) check( r#" -// +tokenids +// +spans+syntaxctxt macro_rules! f { ($($tt:tt)*) => { $($tt)* }; } -// +tokenids +// +spans+syntaxctxt f! { fn main() { 1; 1.0; + ((1,),).0.0; let x = 1; } } "#, - expect![[r##" -// call ids will be shifted by Shift(18) -// +tokenids -macro_rules! f {#0 - (#1$#2(#3$#4tt#5:#6tt#7)#3*#8)#1 =#9>#10 {#11 - $#12(#13$#14tt#15)#13*#16 - }#11;#17 -}#0 - -// // +tokenids -// f! { -// fn#1 main#2() { -// 1#5;#6 -// 1.0#7;#8 -// let#9 x#10 =#11 1#12;#13 -// } -// } -fn#19 main#20(#21)#21 {#22 - 1#23;#24 - 1.0#25;#26 - let#27 x#28 =#29 1#30;#31 -}#22 + expect![[r#" +// +spans+syntaxctxt +macro_rules! f { + ($($tt:tt)*) => { + $($tt)* + }; +} +fn#FileId(0):[email protected]\0# main#FileId(0):[email protected]\0#(#FileId(0):[email protected]\0#)#FileId(0):[email protected]\0# {#FileId(0):[email protected]\0# + 1#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0# + 1.0#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0# + (#FileId(0):[email protected]\0#(#FileId(0):[email protected]\0#1#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# )#FileId(0):[email protected]\0#,#FileId(0):[email protected]\0# )#FileId(0):[email protected]\0#.#FileId(0):[email protected]\0#0#FileId(0):[email protected]\0#.#FileId(0):[email protected]\0#0#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0# + let#FileId(0):[email protected]\0# x#FileId(0):[email protected]\0# =#FileId(0):[email protected]\0# 1#FileId(0):[email protected]\0#;#FileId(0):[email protected]\0# +}#FileId(0):[email protected]\0# -"##]], + +"#]], ); } @@ -105,53 +93,86 @@ fn eager_expands_with_unresolved_within() { r#" #[rustc_builtin_macro] #[macro_export] -macro_rules! format_args {} +macro_rules! concat {} +macro_rules! identity { + ($tt:tt) => { + $tt + } +} fn main(foo: ()) { - format_args!("{} {} {}", format_args!("{}", 0), foo, identity!(10), "bar") + concat!("hello", identity!("world"), unresolved!(), identity!("!")); } "#, expect![[r##" #[rustc_builtin_macro] #[macro_export] -macro_rules! format_args {} +macro_rules! concat {} +macro_rules! identity { + ($tt:tt) => { + $tt + } +} fn main(foo: ()) { - builtin #format_args ("{} {} {}", format_args!("{}", 0), foo, identity!(10), "bar") + /* error: unresolved macro unresolved */"helloworld!"; } "##]], ); } #[test] -fn token_mapping_eager() { +fn concat_spans() { check( r#" #[rustc_builtin_macro] #[macro_export] -macro_rules! format_args {} - +macro_rules! concat {} macro_rules! identity { - ($expr:expr) => { $expr }; + ($tt:tt) => { + $tt + } } fn main(foo: ()) { - format_args/*+tokenids*/!("{} {} {}", format_args!("{}", 0), foo, identity!(10), "bar") + #[rustc_builtin_macro] + #[macro_export] + macro_rules! concat {} + macro_rules! identity { + ($tt:tt) => { + $tt + } + } + + fn main(foo: ()) { + concat/*+spans+syntaxctxt*/!("hello", concat!("w", identity!("o")), identity!("rld"), unresolved!(), identity!("!")); + } } "#, expect![[r##" #[rustc_builtin_macro] #[macro_export] -macro_rules! format_args {} - +macro_rules! concat {} macro_rules! identity { - ($expr:expr) => { $expr }; + ($tt:tt) => { + $tt + } } fn main(foo: ()) { - // format_args/*+tokenids*/!("{} {} {}"#1,#2 format_args#3!#4("{}"#6,#7 0#8),#9 foo#10,#11 identity#12!#13(10#15),#16 "bar"#17) -builtin#4294967295 ##4294967295format_args#4294967295 (#0"{} {} {}"#1,#2 format_args#3!#4(#5"{}"#6,#7 0#8)#5,#9 foo#10,#11 identity#12!#13(#1410#15)#14,#16 "bar"#17)#0 + #[rustc_builtin_macro] + #[macro_export] + macro_rules! concat {} + macro_rules! identity { + ($tt:tt) => { + $tt + } + } + + fn main(foo: ()) { + /* error: unresolved macro unresolved */"helloworld!"#FileId(0):[email protected]\6#; + } } "##]], @@ -159,6 +180,29 @@ builtin#4294967295 ##4294967295format_args#4294967295 (#0"{} {} {}"#1,#2 format_ } #[test] +fn token_mapping_across_files() { + check( + r#" +//- /lib.rs +#[macro_use] +mod foo; + +mk_struct/*+spans+syntaxctxt*/!(Foo with u32); +//- /foo.rs +macro_rules! mk_struct { + ($foo:ident with $ty:ty) => { struct $foo($ty); } +} +"#, + expect![[r#" +#[macro_use] +mod foo; + +struct#FileId(1):[email protected]\2# Foo#FileId(0):[email protected]\0#(#FileId(1):[email protected]\2#u32#FileId(0):[email protected]\0#)#FileId(1):[email protected]\2#;#FileId(1):[email protected]\2# +"#]], + ); +} + +#[test] fn float_field_access_macro_input() { check( r#" |