Unnamed repository; edit this file 'description' to name the repository.
move tests
| -rw-r--r-- | crates/hir_def/src/macro_expansion_tests/mbe.rs | 18 | ||||
| -rw-r--r-- | crates/hir_def/src/macro_expansion_tests/mbe/regression.rs | 151 | ||||
| -rw-r--r-- | crates/mbe/src/tests/expand.rs | 80 |
3 files changed, 169 insertions, 80 deletions
diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index 956a6466cc..1da0110fe2 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -1428,3 +1428,21 @@ macro_rules! foo { "#]], ); } + +#[test] +fn expr_interpolation() { + check( + r#" +macro_rules! m { ($expr:expr) => { map($expr) } } +fn f() { + let _ = m!(x + foo); +} +"#, + expect![[r#" +macro_rules! m { ($expr:expr) => { map($expr) } } +fn f() { + let _ = map(x+foo); +} +"#]], + ) +} diff --git a/crates/hir_def/src/macro_expansion_tests/mbe/regression.rs b/crates/hir_def/src/macro_expansion_tests/mbe/regression.rs index 5f650d458a..5e23ca88fa 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe/regression.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe/regression.rs @@ -728,3 +728,154 @@ impl <> Data for & 'amut G where G: Data {} "##]], ); } + +#[test] +fn test_issue_2520() { + check( + r#" +macro_rules! my_macro { + { + ( $( + $( [] $sname:ident : $stype:ty )? + $( [$expr:expr] $nname:ident : $ntype:ty )? + ),* ) + } => {ok!( + Test { + $( + $( $sname, )? + )* + } + );}; +} + +my_macro! { + ([] p1: u32, [|_| S0K0] s: S0K0, [] k0: i32) +} + "#, + expect![[r#" +macro_rules! my_macro { + { + ( $( + $( [] $sname:ident : $stype:ty )? + $( [$expr:expr] $nname:ident : $ntype:ty )? + ),* ) + } => {ok!( + Test { + $( + $( $sname, )? + )* + } + );}; +} + +ok!(Test { + p1, k0, +} +); + "#]], + ); +} + +#[test] +fn test_repeat_bad_var() { + // FIXME: the second rule of the macro should be removed and an error about + // `$( $c )+` raised + check( + r#" +macro_rules! foo { + ($( $b:ident )+) => { ok!($( $c )+); }; + ($( $b:ident )+) => { ok!($( $b )+); } +} + +foo!(b0 b1); +"#, + expect![[r#" +macro_rules! foo { + ($( $b:ident )+) => { ok!($( $c )+); }; + ($( $b:ident )+) => { ok!($( $b )+); } +} + +ok!(b0 b1); +"#]], + ); +} + +#[test] +fn test_issue_3861() { + // This is should (and does) produce a parse error. It used to infinite loop + // instead. + check( + r#" +macro_rules! rgb_color { + ($p:expr, $t:ty) => { + pub fn new() { + let _ = 0 as $t << $p; + } + }; +} +// +tree +errors +rgb_color!(8 + 8, u32); +"#, + expect![[r#" +macro_rules! rgb_color { + ($p:expr, $t:ty) => { + pub fn new() { + let _ = 0 as $t << $p; + } + }; +} +/* parse error: expected type */ +/* parse error: expected R_ANGLE */ +/* parse error: expected COMMA */ +/* parse error: expected R_ANGLE */ +/* parse error: expected SEMICOLON */ +pub fn new() { + let _ = 0as u32<<8+8; +} +// [email protected] "pub" +// [email protected] "fn" +// [email protected] "new" +// [email protected] "(" +// [email protected] ")" +// [email protected] "{" +// [email protected] "let" +// [email protected] "_" +// [email protected] "=" +// [email protected] "0" +// [email protected] "as" +// [email protected] "u32" +// [email protected] "<" +// [email protected] "<" +// [email protected] "8" +// [email protected] "+" +// [email protected] "8" +// [email protected] ";" +// [email protected] "}" + +"#]], + ); +} diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 6c2e91668e..ffb24709f2 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -98,86 +98,6 @@ fn test_attr_to_token_tree() { ); } -#[test] -fn expr_interpolation() { - let expanded = parse_macro( - r#" - macro_rules! id { - ($expr:expr) => { - map($expr) - } - } - "#, - ) - .expand_expr("id!(x + foo);"); - - assert_eq!(expanded.to_string(), "map(x+foo)"); -} - -#[test] -fn test_issue_2520() { - let macro_fixture = parse_macro( - r#" - macro_rules! my_macro { - { - ( $( - $( [] $sname:ident : $stype:ty )? - $( [$expr:expr] $nname:ident : $ntype:ty )? - ),* ) - } => { - Test { - $( - $( $sname, )? - )* - } - }; - } - "#, - ); - - macro_fixture.assert_expand_items( - r#"my_macro ! { - ([] p1 : u32 , [|_| S0K0] s : S0K0 , [] k0 : i32) - }"#, - "Test {p1 , k0 ,}", - ); -} - -#[test] -fn test_issue_3861() { - let macro_fixture = parse_macro( - r#" - macro_rules! rgb_color { - ($p:expr, $t: ty) => { - pub fn new() { - let _ = 0 as $t << $p; - } - }; - } - "#, - ); - - macro_fixture.expand_items(r#"rgb_color!(8 + 8, u32);"#); -} - -#[test] -fn test_repeat_bad_var() { - // FIXME: the second rule of the macro should be removed and an error about - // `$( $c )+` raised - parse_macro( - r#" - macro_rules! foo { - ($( $b:ident )+) => { - $( $c )+ - }; - ($( $b:ident )+) => { - $( $b )+ - } - } - "#, - ) - .assert_expand_items("foo!(b0 b1);", "b0 b1"); -} #[test] fn test_no_space_after_semi_colon() { |