From afacdd612df3222584b5a252872eb6135c12e28b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 17:17:16 +0300 Subject: [PATCH 01/10] internal: update expect --- Cargo.lock | 4 ++-- crates/cfg/Cargo.toml | 2 +- crates/hir_def/Cargo.toml | 2 +- crates/hir_expand/Cargo.toml | 2 +- crates/hir_ty/Cargo.toml | 2 +- crates/ide/Cargo.toml | 2 +- crates/ide_assists/Cargo.toml | 2 +- crates/ide_completion/Cargo.toml | 2 +- crates/ide_db/Cargo.toml | 2 +- crates/ide_diagnostics/Cargo.toml | 2 +- crates/ide_ssr/Cargo.toml | 2 +- crates/mbe/Cargo.toml | 2 +- crates/proc_macro_srv/Cargo.toml | 2 +- crates/project_model/Cargo.toml | 2 +- crates/rust-analyzer/Cargo.toml | 2 +- crates/syntax/Cargo.toml | 2 +- 16 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5aba6387f0..68c8536b2f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -345,9 +345,9 @@ dependencies = [ [[package]] name = "expect-test" -version = "1.1.0" +version = "1.2.0-pre.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2300477aab3a378f2ca00a4fbd4dc713654ab7ed790e4017493cb33656280633" +checksum = "3a2f1664bc69648747878bfe3430ad9b58bc8d9b50b3b1df9f3c081345e33197" dependencies = [ "dissimilar", "once_cell", diff --git a/crates/cfg/Cargo.toml b/crates/cfg/Cargo.toml index 132505fbf4..f86199c8c3 100644 --- a/crates/cfg/Cargo.toml +++ b/crates/cfg/Cargo.toml @@ -16,7 +16,7 @@ tt = { path = "../tt", version = "0.0.0" } [dev-dependencies] mbe = { path = "../mbe" } syntax = { path = "../syntax" } -expect-test = "1.1" +expect-test = "1.2.0-pre.1" oorandom = "11" # We depend on both individually instead of using `features = ["derive"]` to microoptimize the # build graph: if the feature was enabled, syn would be built early on in the graph if `smolstr` diff --git a/crates/hir_def/Cargo.toml b/crates/hir_def/Cargo.toml index f5e60a4f54..9573d5ac35 100644 --- a/crates/hir_def/Cargo.toml +++ b/crates/hir_def/Cargo.toml @@ -35,4 +35,4 @@ limit = { path = "../limit", version = "0.0.0" } [dev-dependencies] test_utils = { path = "../test_utils" } -expect-test = "1.1" +expect-test = "1.2.0-pre.1" diff --git a/crates/hir_expand/Cargo.toml b/crates/hir_expand/Cargo.toml index b39de7cfae..240193f9cf 100644 --- a/crates/hir_expand/Cargo.toml +++ b/crates/hir_expand/Cargo.toml @@ -25,4 +25,4 @@ mbe = { path = "../mbe", version = "0.0.0" } limit = { path = "../limit", version = "0.0.0" } [dev-dependencies] -expect-test = "1.1" +expect-test = "1.2.0-pre.1" diff --git a/crates/hir_ty/Cargo.toml b/crates/hir_ty/Cargo.toml index 7566bf687b..0f1e75958a 100644 --- a/crates/hir_ty/Cargo.toml +++ b/crates/hir_ty/Cargo.toml @@ -33,7 +33,7 @@ limit = { path = "../limit", version = "0.0.0" } [dev-dependencies] test_utils = { path = "../test_utils" } -expect-test = "1.1" +expect-test = "1.2.0-pre.1" tracing = "0.1" tracing-subscriber = { version = "0.2", default-features = false, features = [ "env-filter", diff --git a/crates/ide/Cargo.toml b/crates/ide/Cargo.toml index 804002a4b3..bccadacdb2 100644 --- a/crates/ide/Cargo.toml +++ b/crates/ide/Cargo.toml @@ -38,4 +38,4 @@ hir = { path = "../hir", version = "0.0.0" } [dev-dependencies] test_utils = { path = "../test_utils" } -expect-test = "1.1" +expect-test = "1.2.0-pre.1" diff --git a/crates/ide_assists/Cargo.toml b/crates/ide_assists/Cargo.toml index c34798d0ea..629aadb5b1 100644 --- a/crates/ide_assists/Cargo.toml +++ b/crates/ide_assists/Cargo.toml @@ -25,4 +25,4 @@ hir = { path = "../hir", version = "0.0.0" } [dev-dependencies] test_utils = { path = "../test_utils" } sourcegen = { path = "../sourcegen" } -expect-test = "1.1" +expect-test = "1.2.0-pre.1" diff --git a/crates/ide_completion/Cargo.toml b/crates/ide_completion/Cargo.toml index 0d4413978d..5b2f0b35f9 100644 --- a/crates/ide_completion/Cargo.toml +++ b/crates/ide_completion/Cargo.toml @@ -28,7 +28,7 @@ profile = { path = "../profile", version = "0.0.0" } hir = { path = "../hir", version = "0.0.0" } [dev-dependencies] -expect-test = "1.1" +expect-test = "1.2.0-pre.1" xshell = "0.1" test_utils = { path = "../test_utils" } diff --git a/crates/ide_db/Cargo.toml b/crates/ide_db/Cargo.toml index cdaff0ce39..e3c60edda1 100644 --- a/crates/ide_db/Cargo.toml +++ b/crates/ide_db/Cargo.toml @@ -31,4 +31,4 @@ limit = { path = "../limit", version = "0.0.0" } [dev-dependencies] test_utils = { path = "../test_utils" } -expect-test = "1.1" +expect-test = "1.2.0-pre.1" diff --git a/crates/ide_diagnostics/Cargo.toml b/crates/ide_diagnostics/Cargo.toml index 3389e67d9d..7e36b1a3bd 100644 --- a/crates/ide_diagnostics/Cargo.toml +++ b/crates/ide_diagnostics/Cargo.toml @@ -23,7 +23,7 @@ hir = { path = "../hir", version = "0.0.0" } ide_db = { path = "../ide_db", version = "0.0.0" } [dev-dependencies] -expect-test = "1.1" +expect-test = "1.2.0-pre.1" test_utils = { path = "../test_utils" } sourcegen = { path = "../sourcegen" } diff --git a/crates/ide_ssr/Cargo.toml b/crates/ide_ssr/Cargo.toml index 28aacd349c..275f923f2b 100644 --- a/crates/ide_ssr/Cargo.toml +++ b/crates/ide_ssr/Cargo.toml @@ -21,4 +21,4 @@ hir = { path = "../hir", version = "0.0.0" } [dev-dependencies] test_utils = { path = "../test_utils" } -expect-test = "1.1" +expect-test = "1.2.0-pre.1" diff --git a/crates/mbe/Cargo.toml b/crates/mbe/Cargo.toml index 22bda57669..5378516e70 100644 --- a/crates/mbe/Cargo.toml +++ b/crates/mbe/Cargo.toml @@ -12,7 +12,7 @@ doctest = false cov-mark = "2.0.0-pre.1" rustc-hash = "1.1.0" smallvec = "1.2.0" -expect-test = "1.1" +expect-test = "1.2.0-pre.1" tracing = "0.1" syntax = { path = "../syntax", version = "0.0.0" } diff --git a/crates/proc_macro_srv/Cargo.toml b/crates/proc_macro_srv/Cargo.toml index d0e6bd9990..6048c5e6cc 100644 --- a/crates/proc_macro_srv/Cargo.toml +++ b/crates/proc_macro_srv/Cargo.toml @@ -25,7 +25,7 @@ paths = { path = "../paths", version = "0.0.0" } proc_macro_api = { path = "../proc_macro_api", version = "0.0.0" } [dev-dependencies] -expect-test = "1.1.0" +expect-test = "1.2.0-pre.1" # used as proc macro test targets proc_macro_test = { path = "../proc_macro_test" } diff --git a/crates/project_model/Cargo.toml b/crates/project_model/Cargo.toml index cea2c16a63..949325fc82 100644 --- a/crates/project_model/Cargo.toml +++ b/crates/project_model/Cargo.toml @@ -16,7 +16,7 @@ semver = "1" serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.48" anyhow = "1.0.26" -expect-test = "1" +expect-test = "1.2.0-pre.1" la-arena = { version = "0.2.0", path = "../../lib/arena" } cfg = { path = "../cfg", version = "0.0.0" } diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 29a3fbb67e..8275f6e3d5 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -67,7 +67,7 @@ winapi = "0.3.8" jemallocator = { version = "0.4.1", package = "tikv-jemallocator", optional = true } [dev-dependencies] -expect-test = "1.1" +expect-test = "1.2.0-pre.1" xshell = "0.1" test_utils = { path = "../test_utils" } diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml index fba5db9a93..a34522435a 100644 --- a/crates/syntax/Cargo.toml +++ b/crates/syntax/Cargo.toml @@ -26,7 +26,7 @@ profile = { path = "../profile", version = "0.0.0" } [dev-dependencies] rayon = "1" -expect-test = "1.1" +expect-test = "1.2.0-pre.1" proc-macro2 = "1.0.8" quote = "1.0.2" ungrammar = "=1.14.8" From cb1b6a273f6b317482541afbccb9c130c2e44313 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 17:17:37 +0300 Subject: [PATCH 02/10] internal: don't un-indent macro tests manually --- crates/hir_def/src/macro_expansion_tests.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index 96957471c8..b14f484e76 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -28,7 +28,7 @@ use crate::{ db::DefDatabase, nameres::ModuleSource, resolver::HasResolver, test_db::TestDB, AsMacroCall, }; -fn check(ra_fixture: &str, expect: Expect) { +fn check(ra_fixture: &str, mut expect: Expect) { let db = TestDB::with_files(ra_fixture); let krate = db.crate_graph().iter().next().unwrap(); let def_map = db.crate_def_map(krate); @@ -75,6 +75,7 @@ fn check(ra_fixture: &str, expect: Expect) { expanded_text.replace_range(range, &expn_text) } + expect.indent(false); expect.assert_eq(&expanded_text); } From 036c0ff8c7c51cac4481d8f70280405d9a170ddc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 17:27:38 +0300 Subject: [PATCH 03/10] move some tests --- crates/hir_def/src/macro_expansion_tests.rs | 4 +- .../hir_def/src/macro_expansion_tests/mbe.rs | 63 +++++++++++++++++++ crates/mbe/src/tests/expand.rs | 42 ------------- 3 files changed, 66 insertions(+), 43 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index b14f484e76..d4520939b6 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -102,9 +102,11 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { let curr_kind = token.kind(); let space = match (prev_kind, curr_kind) { _ if prev_kind.is_trivia() || curr_kind.is_trivia() => "", + (T!['{'], T!['}']) => "", (T![=], _) | (_, T![=]) => " ", (_, T!['{']) => " ", - (T![;] | T!['}'], _) => "\n", + (T![;] | T!['{'] | T!['}'], _) => "\n", + (_, T!['}']) => "\n", (IDENT | LIFETIME_IDENT, IDENT | LIFETIME_IDENT) => " ", (IDENT, _) if curr_kind.is_keyword() => " ", (_, IDENT) if prev_kind.is_keyword() => " ", diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index c57e9cd838..8851028845 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -159,3 +159,66 @@ struct Bar; "##]], ); } + +#[test] +fn test_match_group_pattern_with_multiple_defs() { + check( + r#" +macro_rules! m { + ($ ($ i:ident),*) => ( impl Bar { $ ( fn $ i {} )*} ); +} +m! { foo, bar } +"#, + expect![[r#" +macro_rules! m { + ($ ($ i:ident),*) => ( impl Bar { $ ( fn $ i {} )*} ); +} +impl Bar { +fn foo {} +fn bar {} +} +"#]], + ); +} + +#[test] +fn test_match_group_pattern_with_multiple_statement() { + check( + r#" +macro_rules! m { + ($ ($ i:ident),*) => ( fn baz { $ ( $ i (); )*} ); +} +m! { foo, bar } +"#, + expect![[r#" +macro_rules! m { + ($ ($ i:ident),*) => ( fn baz { $ ( $ i (); )*} ); +} +fn baz { +foo(); +bar(); +} +"#]], + ) +} + +#[test] +fn test_match_group_pattern_with_multiple_statement_without_semi() { + check( + r#" +macro_rules! m { + ($ ($ i:ident),*) => ( fn baz { $ ( $i() );*} ); +} +m! { foo, bar } +"#, + expect![[r#" +macro_rules! m { + ($ ($ i:ident),*) => ( fn baz { $ ( $i() );*} ); +} +fn baz { +foo(); +bar() +} +"#]], + ) +} diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 7becaa6658..d329784023 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -209,48 +209,6 @@ fn test_expr_order() { ); } -#[test] -fn test_match_group_pattern_with_multiple_defs() { - parse_macro( - r#" - macro_rules! foo { - ($ ($ i:ident),*) => ( struct Bar { $ ( - fn $ i {} - )*} ); - } -"#, - ) - .assert_expand_items("foo! { foo, bar }", "struct Bar {fn foo {} fn bar {}}"); -} - -#[test] -fn test_match_group_pattern_with_multiple_statement() { - parse_macro( - r#" - macro_rules! foo { - ($ ($ i:ident),*) => ( fn baz { $ ( - $ i (); - )*} ); - } -"#, - ) - .assert_expand_items("foo! { foo, bar }", "fn baz {foo () ; bar () ;}"); -} - -#[test] -fn test_match_group_pattern_with_multiple_statement_without_semi() { - parse_macro( - r#" - macro_rules! foo { - ($ ($ i:ident),*) => ( fn baz { $ ( - $i() - );*} ); - } -"#, - ) - .assert_expand_items("foo! { foo, bar }", "fn baz {foo () ;bar ()}"); -} - #[test] fn test_match_group_empty_fixed_token() { parse_macro( From a0d9e78ec34e614b9d5dcab44e579abc55c75cac Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 17:31:04 +0300 Subject: [PATCH 04/10] align tests to code style --- .../hir_def/src/macro_expansion_tests/mbe.rs | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index 8851028845..36c1858a04 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -52,9 +52,9 @@ fn match_by_first_token_literally() { check( r#" macro_rules! m { - ($ i:ident) => ( mod $ i {} ); - (= $ i:ident) => ( fn $ i() {} ); - (+ $ i:ident) => ( struct $ i; ) + ($i:ident) => ( mod $i {} ); + (= $i:ident) => ( fn $i() {} ); + (+ $i:ident) => ( struct $i; ) } m! { foo } m! { = bar } @@ -62,9 +62,9 @@ m! { + Baz } "#, expect![[r#" macro_rules! m { - ($ i:ident) => ( mod $ i {} ); - (= $ i:ident) => ( fn $ i() {} ); - (+ $ i:ident) => ( struct $ i; ) + ($i:ident) => ( mod $i {} ); + (= $i:ident) => ( fn $i() {} ); + (+ $i:ident) => ( struct $i; ) } mod foo {} fn bar() {} @@ -78,9 +78,9 @@ fn match_by_last_token_literally() { check( r#" macro_rules! m { - ($ i:ident) => ( mod $ i {} ); - ($ i:ident =) => ( fn $ i() {} ); - ($ i:ident +) => ( struct $ i; ) + ($i:ident) => ( mod $i {} ); + ($i:ident =) => ( fn $i() {} ); + ($i:ident +) => ( struct $i; ) } m! { foo } m! { bar = } @@ -88,9 +88,9 @@ m! { Baz + } "#, expect![[r#" macro_rules! m { - ($ i:ident) => ( mod $ i {} ); - ($ i:ident =) => ( fn $ i() {} ); - ($ i:ident +) => ( struct $ i; ) + ($i:ident) => ( mod $i {} ); + ($i:ident =) => ( fn $i() {} ); + ($i:ident +) => ( struct $i; ) } mod foo {} fn bar() {} @@ -104,9 +104,9 @@ fn match_by_ident() { check( r#" macro_rules! m { - ($ i:ident) => ( mod $ i {} ); - (spam $ i:ident) => ( fn $ i() {} ); - (eggs $ i:ident) => ( struct $ i; ) + ($i:ident) => ( mod $i {} ); + (spam $i:ident) => ( fn $i() {} ); + (eggs $i:ident) => ( struct $i; ) } m! { foo } m! { spam bar } @@ -114,9 +114,9 @@ m! { eggs Baz } "#, expect![[r#" macro_rules! m { - ($ i:ident) => ( mod $ i {} ); - (spam $ i:ident) => ( fn $ i() {} ); - (eggs $ i:ident) => ( struct $ i; ) + ($i:ident) => ( mod $i {} ); + (spam $i:ident) => ( fn $i() {} ); + (eggs $i:ident) => ( struct $i; ) } mod foo {} fn bar() {} @@ -130,9 +130,9 @@ fn match_by_separator_token() { check( r#" macro_rules! m { - ($ ($ i:ident),*) => ($ ( mod $ i {} )*); - ($ ($ i:ident)#*) => ($ ( fn $ i() {} )*); - ($ i:ident ,# $ j:ident) => ( struct $ i; struct $ j; ) + ($($i:ident),*) => ($(mod $i {} )*); + ($($i:ident)#*) => ($(fn $i() {} )*); + ($i:ident ,# $ j:ident) => ( struct $i; struct $ j; ) } m! { foo, bar } @@ -143,9 +143,9 @@ m! { Foo,# Bar } "#, expect![[r##" macro_rules! m { - ($ ($ i:ident),*) => ($ ( mod $ i {} )*); - ($ ($ i:ident)#*) => ($ ( fn $ i() {} )*); - ($ i:ident ,# $ j:ident) => ( struct $ i; struct $ j; ) + ($($i:ident),*) => ($(mod $i {} )*); + ($($i:ident)#*) => ($(fn $i() {} )*); + ($i:ident ,# $ j:ident) => ( struct $i; struct $ j; ) } mod foo {} @@ -165,13 +165,13 @@ fn test_match_group_pattern_with_multiple_defs() { check( r#" macro_rules! m { - ($ ($ i:ident),*) => ( impl Bar { $ ( fn $ i {} )*} ); + ($($i:ident),*) => ( impl Bar { $(fn $i {})* } ); } m! { foo, bar } "#, expect![[r#" macro_rules! m { - ($ ($ i:ident),*) => ( impl Bar { $ ( fn $ i {} )*} ); + ($($i:ident),*) => ( impl Bar { $(fn $i {})* } ); } impl Bar { fn foo {} @@ -186,13 +186,13 @@ fn test_match_group_pattern_with_multiple_statement() { check( r#" macro_rules! m { - ($ ($ i:ident),*) => ( fn baz { $ ( $ i (); )*} ); + ($($i:ident),*) => ( fn baz { $($i ();)* } ); } m! { foo, bar } "#, expect![[r#" macro_rules! m { - ($ ($ i:ident),*) => ( fn baz { $ ( $ i (); )*} ); + ($($i:ident),*) => ( fn baz { $($i ();)* } ); } fn baz { foo(); @@ -207,13 +207,13 @@ fn test_match_group_pattern_with_multiple_statement_without_semi() { check( r#" macro_rules! m { - ($ ($ i:ident),*) => ( fn baz { $ ( $i() );*} ); + ($($i:ident),*) => ( fn baz { $($i() );* } ); } m! { foo, bar } "#, expect![[r#" macro_rules! m { - ($ ($ i:ident),*) => ( fn baz { $ ( $i() );*} ); + ($($i:ident),*) => ( fn baz { $($i() );* } ); } fn baz { foo(); From 6407e2e789e45c64727a996da825d23f1516cb27 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 17:36:41 +0300 Subject: [PATCH 05/10] indent macro output when pprinting --- crates/hir_def/src/macro_expansion_tests.rs | 10 ++++++++++ crates/hir_def/src/macro_expansion_tests/mbe.rs | 12 ++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index d4520939b6..ac0a2d27b3 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -98,6 +98,7 @@ fn reindent(indent: IndentLevel, pp: String) -> String { fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { let mut res = String::new(); let mut prev_kind = SyntaxKind::EOF; + let mut indent_level = 0; for token in iter::successors(expn.first_token(), |t| t.next_token()) { let curr_kind = token.kind(); let space = match (prev_kind, curr_kind) { @@ -113,7 +114,16 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { _ => "", }; + match prev_kind { + T!['{'] => indent_level += 1, + T!['}'] => indent_level -= 1, + _ => (), + } + res.push_str(space); + if space == "\n" && curr_kind != T!['}'] { + res.push_str(&" ".repeat(indent_level)); + } prev_kind = curr_kind; format_to!(res, "{}", token) } diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index 36c1858a04..f5d00b6666 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -174,8 +174,8 @@ macro_rules! m { ($($i:ident),*) => ( impl Bar { $(fn $i {})* } ); } impl Bar { -fn foo {} -fn bar {} + fn foo {} + fn bar {} } "#]], ); @@ -195,8 +195,8 @@ macro_rules! m { ($($i:ident),*) => ( fn baz { $($i ();)* } ); } fn baz { -foo(); -bar(); + foo(); + bar(); } "#]], ) @@ -216,8 +216,8 @@ macro_rules! m { ($($i:ident),*) => ( fn baz { $($i() );* } ); } fn baz { -foo(); -bar() + foo(); + bar() } "#]], ) From aac23f78328a6747a6e090af9a3cbe13b621def9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 17:43:07 +0300 Subject: [PATCH 06/10] move tests --- .../hir_def/src/macro_expansion_tests/mbe.rs | 39 +++++++++++++++++++ crates/mbe/src/tests/expand.rs | 27 ------------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index f5d00b6666..f1e979503e 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -222,3 +222,42 @@ fn baz { "#]], ) } + +#[test] +fn test_match_group_empty_fixed_token() { + check( + r#" +macro_rules! m { + ($($i:ident)* #abc) => ( fn baz { $($i ();)* } ); +} +m!{#abc} +"#, + expect![[r##" +macro_rules! m { + ($($i:ident)* #abc) => ( fn baz { $($i ();)* } ); +} +fn baz {} +"##]], + ) +} + +#[test] +fn test_match_group_in_subtree() { + check( + r#" +macro_rules! m { + (fn $name:ident { $($i:ident)* } ) => ( fn $name() { $($i ();)* } ); +} +m! {fn baz { a b } } +"#, + expect![[r#" +macro_rules! m { + (fn $name:ident { $($i:ident)* } ) => ( fn $name() { $($i ();)* } ); +} +fn baz() { + a(); + b(); +} +"#]], + ) +} diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index d329784023..3655cb5a63 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -209,33 +209,6 @@ fn test_expr_order() { ); } -#[test] -fn test_match_group_empty_fixed_token() { - parse_macro( - r#" - macro_rules! foo { - ($ ($ i:ident)* #abc) => ( fn baz { $ ( - $ i (); - )*} ); - } -"#, - ) - .assert_expand_items("foo! {#abc}", "fn baz {}"); -} - -#[test] -fn test_match_group_in_subtree() { - parse_macro( - r#" - macro_rules! foo { - (fn $name:ident {$($i:ident)*} ) => ( fn $name() { $ ( - $ i (); - )*} ); - }"#, - ) - .assert_expand_items("foo! {fn baz {a b} }", "fn baz () {a () ; b () ;}"); -} - #[test] fn test_match_group_with_multichar_sep() { parse_macro( From c41b7bbe698a504b6c66cce744c2a8c0181c9b85 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 17:58:17 +0300 Subject: [PATCH 07/10] internal: allow macro tests to inspect parse tree --- crates/hir_def/src/macro_expansion_tests.rs | 7 +++ .../hir_def/src/macro_expansion_tests/mbe.rs | 46 +++++++++++++++++++ crates/mbe/src/tests/expand.rs | 42 ----------------- 3 files changed, 53 insertions(+), 42 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index ac0a2d27b3..0a9ab93157 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -69,6 +69,13 @@ fn check(ra_fixture: &str, mut expect: Expect) { let indent = IndentLevel::from_node(call.syntax()); let pp = reindent(indent, pp); format_to!(expn_text, "{}", pp); + if call.to_string().contains("// +tree") { + let tree = format!("{:#?}", parse.syntax_node()) + .split_inclusive("\n") + .map(|line| format!("// {}", line)) + .collect::(); + format_to!(expn_text, "\n{}", tree) + } } let range = call.syntax().text_range(); let range: Range = range.into(); diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index f1e979503e..7c9a19613b 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -261,3 +261,49 @@ fn baz() { "#]], ) } + +#[test] +fn test_expr_order() { + check( + r#" +macro_rules! m { + ($ i:expr) => { fn bar() { $ i * 3; } } +} +// +tree +m! { 1 + 2 } +"#, + expect![[r#" +macro_rules! m { + ($ i:expr) => { fn bar() { $ i * 3; } } +} +fn bar() { + 1+2*3; +} +// MACRO_ITEMS@0..15 +// FN@0..15 +// FN_KW@0..2 "fn" +// NAME@2..5 +// IDENT@2..5 "bar" +// PARAM_LIST@5..7 +// L_PAREN@5..6 "(" +// R_PAREN@6..7 ")" +// BLOCK_EXPR@7..15 +// STMT_LIST@7..15 +// L_CURLY@7..8 "{" +// EXPR_STMT@8..14 +// BIN_EXPR@8..13 +// BIN_EXPR@8..11 +// LITERAL@8..9 +// INT_NUMBER@8..9 "1" +// PLUS@9..10 "+" +// LITERAL@10..11 +// INT_NUMBER@10..11 "2" +// STAR@11..12 "*" +// LITERAL@12..13 +// INT_NUMBER@12..13 "3" +// SEMICOLON@13..14 ";" +// R_CURLY@14..15 "}" + +"#]], + ) +} diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 3655cb5a63..85a51588eb 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -166,48 +166,6 @@ SUBTREE $ ); } -#[test] -fn test_expr_order() { - let expanded = parse_macro( - r#" - macro_rules! foo { - ($ i:expr) => { - fn bar() { $ i * 2; } - } - } -"#, - ) - .expand_items("foo! { 1 + 1}"); - - let dump = format!("{:#?}", expanded); - assert_eq_text!( - r#"MACRO_ITEMS@0..15 - FN@0..15 - FN_KW@0..2 "fn" - NAME@2..5 - IDENT@2..5 "bar" - PARAM_LIST@5..7 - L_PAREN@5..6 "(" - R_PAREN@6..7 ")" - BLOCK_EXPR@7..15 - STMT_LIST@7..15 - L_CURLY@7..8 "{" - EXPR_STMT@8..14 - BIN_EXPR@8..13 - BIN_EXPR@8..11 - LITERAL@8..9 - INT_NUMBER@8..9 "1" - PLUS@9..10 "+" - LITERAL@10..11 - INT_NUMBER@10..11 "1" - STAR@11..12 "*" - LITERAL@12..13 - INT_NUMBER@12..13 "2" - SEMICOLON@13..14 ";" - R_CURLY@14..15 "}""#, - dump.trim() - ); -} #[test] fn test_match_group_with_multichar_sep() { From c2e425dd00e72f31334cdadc52a02d6b6ede9526 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 18:06:24 +0300 Subject: [PATCH 08/10] internal: make sure macro test expand to valid syntax --- crates/hir_def/src/macro_expansion_tests.rs | 5 ++++ .../hir_def/src/macro_expansion_tests/mbe.rs | 28 +++++++++---------- .../src/macro_expansion_tests/mbe/matching.rs | 12 ++++---- .../macro_expansion_tests/mbe/meta_syntax.rs | 6 ++-- crates/syntax/src/lib.rs | 7 ++--- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index 0a9ab93157..d96af5c407 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -65,6 +65,11 @@ fn check(ra_fixture: &str, mut expect: Expect) { format_to!(expn_text, "/* error: {} */", err); } if let Some((parse, _token_map)) = exp.value { + assert!( + parse.errors().is_empty(), + "parse errors in expansion: \n{:#?}", + parse.errors() + ); let pp = pretty_print_macro_expansion(parse.syntax_node()); let indent = IndentLevel::from_node(call.syntax()); let pp = reindent(indent, pp); diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index 7c9a19613b..06ef8f3572 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -165,17 +165,17 @@ fn test_match_group_pattern_with_multiple_defs() { check( r#" macro_rules! m { - ($($i:ident),*) => ( impl Bar { $(fn $i {})* } ); + ($($i:ident),*) => ( impl Bar { $(fn $i() {})* } ); } m! { foo, bar } "#, expect![[r#" macro_rules! m { - ($($i:ident),*) => ( impl Bar { $(fn $i {})* } ); + ($($i:ident),*) => ( impl Bar { $(fn $i() {})* } ); } impl Bar { - fn foo {} - fn bar {} + fn foo() {} + fn bar() {} } "#]], ); @@ -186,15 +186,15 @@ fn test_match_group_pattern_with_multiple_statement() { check( r#" macro_rules! m { - ($($i:ident),*) => ( fn baz { $($i ();)* } ); + ($($i:ident),*) => ( fn baz() { $($i ();)* } ); } m! { foo, bar } "#, expect![[r#" macro_rules! m { - ($($i:ident),*) => ( fn baz { $($i ();)* } ); + ($($i:ident),*) => ( fn baz() { $($i ();)* } ); } -fn baz { +fn baz() { foo(); bar(); } @@ -207,15 +207,15 @@ fn test_match_group_pattern_with_multiple_statement_without_semi() { check( r#" macro_rules! m { - ($($i:ident),*) => ( fn baz { $($i() );* } ); + ($($i:ident),*) => ( fn baz() { $($i() );* } ); } m! { foo, bar } "#, expect![[r#" macro_rules! m { - ($($i:ident),*) => ( fn baz { $($i() );* } ); + ($($i:ident),*) => ( fn baz() { $($i() );* } ); } -fn baz { +fn baz() { foo(); bar() } @@ -228,15 +228,15 @@ fn test_match_group_empty_fixed_token() { check( r#" macro_rules! m { - ($($i:ident)* #abc) => ( fn baz { $($i ();)* } ); + ($($i:ident)* #abc) => ( fn baz() { $($i ();)* } ); } m!{#abc} "#, expect![[r##" macro_rules! m { - ($($i:ident)* #abc) => ( fn baz { $($i ();)* } ); + ($($i:ident)* #abc) => ( fn baz() { $($i ();)* } ); } -fn baz {} +fn baz() {} "##]], ) } @@ -248,7 +248,7 @@ fn test_match_group_in_subtree() { macro_rules! m { (fn $name:ident { $($i:ident)* } ) => ( fn $name() { $($i ();)* } ); } -m! {fn baz { a b } } +m! { fn baz { a b } } "#, expect![[r#" macro_rules! m { diff --git a/crates/hir_def/src/macro_expansion_tests/mbe/matching.rs b/crates/hir_def/src/macro_expansion_tests/mbe/matching.rs index 11317cfa9d..5bdabfc598 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe/matching.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe/matching.rs @@ -8,18 +8,18 @@ use crate::macro_expansion_tests::check; fn unary_minus_is_a_literal() { check( r#" -macro_rules! m { ($x:literal) => (literal!()); ($x:tt) => (not_a_literal!()); } +macro_rules! m { ($x:literal) => (literal!();); ($x:tt) => (not_a_literal!();); } m!(92); m!(-92); m!(-9.2); m!(--92); "#, expect![[r#" -macro_rules! m { ($x:literal) => (literal!()); ($x:tt) => (not_a_literal!()); } -literal!() -literal!() -literal!() -/* error: leftover tokens */not_a_literal!() +macro_rules! m { ($x:literal) => (literal!();); ($x:tt) => (not_a_literal!();); } +literal!(); +literal!(); +literal!(); +/* error: leftover tokens */not_a_literal!(); "#]], ) } diff --git a/crates/hir_def/src/macro_expansion_tests/mbe/meta_syntax.rs b/crates/hir_def/src/macro_expansion_tests/mbe/meta_syntax.rs index 4249c2507e..d434c88244 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe/meta_syntax.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe/meta_syntax.rs @@ -16,7 +16,7 @@ macro_rules! m { ($($i:ident)*) => ($_); ($($true:ident)*) => ($true); ($($false:ident)*) => ($false); - ($) => ($); + ($) => (m!($);); } m!($); "#, @@ -29,9 +29,9 @@ macro_rules! m { ($($i:ident)*) => ($_); ($($true:ident)*) => ($true); ($($false:ident)*) => ($false); - ($) => ($); + ($) => (m!($);); } -$ +m!($); "#]], ) } diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 132d300674..24a099cb3c 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -89,6 +89,9 @@ impl Parse { pub fn syntax_node(&self) -> SyntaxNode { SyntaxNode::new_root(self.green.clone()) } + pub fn errors(&self) -> &[SyntaxError] { + &*self.errors + } } impl Parse { @@ -100,10 +103,6 @@ impl Parse { T::cast(self.syntax_node()).unwrap() } - pub fn errors(&self) -> &[SyntaxError] { - &*self.errors - } - pub fn ok(self) -> Result>> { if self.errors.is_empty() { Ok(self.tree()) From a060b9a4b234a3b98c9852781f2035889869f3ba Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 18:15:05 +0300 Subject: [PATCH 09/10] internal: move some macro tests --- crates/hir_def/src/macro_expansion_tests.rs | 8 +- .../hir_def/src/macro_expansion_tests/mbe.rs | 39 ++++++++ crates/mbe/src/tests/expand.rs | 96 ------------------- 3 files changed, 45 insertions(+), 98 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index d96af5c407..60c9fd7d0a 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -123,6 +123,9 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { (IDENT | LIFETIME_IDENT, IDENT | LIFETIME_IDENT) => " ", (IDENT, _) if curr_kind.is_keyword() => " ", (_, IDENT) if prev_kind.is_keyword() => " ", + (T![>], IDENT) => " ", + (T![>], _) if curr_kind.is_keyword() => " ", + (T![->], _) | (_, T![->]) => " ", _ => "", }; @@ -133,8 +136,9 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { } res.push_str(space); - if space == "\n" && curr_kind != T!['}'] { - res.push_str(&" ".repeat(indent_level)); + if space == "\n" { + let level = if curr_kind == T!['}'] { indent_level - 1 } else { indent_level }; + res.push_str(&" ".repeat(level)); } prev_kind = curr_kind; format_to!(res, "{}", token) diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index 06ef8f3572..c7442bade6 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -9,6 +9,45 @@ use expect_test::expect; use crate::macro_expansion_tests::check; +#[test] +fn mbe_smoke_test() { + check( + r#" +macro_rules! impl_froms { + ($e:ident: $($v:ident),*) => { + $( + impl From<$v> for $e { + fn from(it: $v) -> $e { $e::$v(it) } + } + )* + } +} +impl_froms!(TokenTree: Leaf, Subtree); +"#, + expect![[r#" +macro_rules! impl_froms { + ($e:ident: $($v:ident),*) => { + $( + impl From<$v> for $e { + fn from(it: $v) -> $e { $e::$v(it) } + } + )* + } +} +impl From for TokenTree { + fn from(it:Leaf) -> TokenTree { + TokenTree::Leaf(it) + } +} +impl From for TokenTree { + fn from(it:Subtree) -> TokenTree { + TokenTree::Subtree(it) + } +} +"#]], + ); +} + #[test] fn expansion_does_not_parse_as_expression() { check( diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 85a51588eb..d0eb49455b 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -71,102 +71,6 @@ macro_rules! foobar { assert_eq!(get_text(tt::TokenId(13), T!['{']), "{"); } -#[test] -fn test_convert_tt() { - parse_macro(r#" -macro_rules! impl_froms { - ($e:ident: $($v:ident),*) => { - $( - impl From<$v> for $e { - fn from(it: $v) -> $e { - $e::$v(it) - } - } - )* - } -} -"#) - .assert_expand_tt( - "impl_froms!(TokenTree: Leaf, Subtree);", - "impl From for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree ::Leaf (it)}} \ - impl From for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree ::Subtree (it)}}" - ); -} - -#[test] -fn test_convert_tt2() { - parse_macro( - r#" -macro_rules! impl_froms { - ($e:ident: $($v:ident),*) => { - $( - impl From<$v> for $e { - fn from(it: $v) -> $e { - $e::$v(it) - } - } - )* - } -} -"#, - ) - .assert_expand( - "impl_froms!(TokenTree: Leaf, Subtree);", - r#" -SUBTREE $ - IDENT impl 20 - IDENT From 21 - PUNCH < [joint] 22 - IDENT Leaf 53 - PUNCH > [alone] 25 - IDENT for 26 - IDENT TokenTree 51 - SUBTREE {} 29 - IDENT fn 30 - IDENT from 31 - SUBTREE () 32 - IDENT it 33 - PUNCH : [alone] 34 - IDENT Leaf 53 - PUNCH - [joint] 37 - PUNCH > [alone] 38 - IDENT TokenTree 51 - SUBTREE {} 41 - IDENT TokenTree 51 - PUNCH : [joint] 44 - PUNCH : [joint] 45 - IDENT Leaf 53 - SUBTREE () 48 - IDENT it 49 - IDENT impl 20 - IDENT From 21 - PUNCH < [joint] 22 - IDENT Subtree 55 - PUNCH > [alone] 25 - IDENT for 26 - IDENT TokenTree 51 - SUBTREE {} 29 - IDENT fn 30 - IDENT from 31 - SUBTREE () 32 - IDENT it 33 - PUNCH : [alone] 34 - IDENT Subtree 55 - PUNCH - [joint] 37 - PUNCH > [alone] 38 - IDENT TokenTree 51 - SUBTREE {} 41 - IDENT TokenTree 51 - PUNCH : [joint] 44 - PUNCH : [joint] 45 - IDENT Subtree 55 - SUBTREE () 48 - IDENT it 49 -"#, - ); -} - - #[test] fn test_match_group_with_multichar_sep() { parse_macro( From 3e8ef943c64ad4dd7d0550065cc0ef01d02bc40b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 18:18:56 +0300 Subject: [PATCH 10/10] internal: move some tests --- crates/hir_def/src/macro_expansion_tests.rs | 1 + .../hir_def/src/macro_expansion_tests/mbe.rs | 37 +++++++++++++++++++ crates/mbe/src/tests/expand.rs | 22 ----------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index 60c9fd7d0a..c66c75c143 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -126,6 +126,7 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { (T![>], IDENT) => " ", (T![>], _) if curr_kind.is_keyword() => " ", (T![->], _) | (_, T![->]) => " ", + (T![&&], _) | (_, T![&&]) => " ", _ => "", }; diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index c7442bade6..384c70028d 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -346,3 +346,40 @@ fn bar() { "#]], ) } + +#[test] +fn test_match_group_with_multichar_sep() { + check( + r#" +macro_rules! m { + (fn $name:ident { $($i:literal)* }) => ( fn $name() -> bool { $($i)&&* } ); +} +m! (fn baz { true false } ); +"#, + expect![[r#" +macro_rules! m { + (fn $name:ident { $($i:literal)* }) => ( fn $name() -> bool { $($i)&&* } ); +} +fn baz() -> bool { + true && false +} +"#]], + ); + + check( + r#" +macro_rules! m { + (fn $name:ident { $($i:literal)&&* }) => ( fn $name() -> bool { $($i)&&* } ); +} +m! (fn baz { true && false } ); +"#, + expect![[r#" +macro_rules! m { + (fn $name:ident { $($i:literal)&&* }) => ( fn $name() -> bool { $($i)&&* } ); +} +fn baz() -> bool { + true && false +} +"#]], + ); +} diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index d0eb49455b..c08788cda1 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -71,28 +71,6 @@ macro_rules! foobar { assert_eq!(get_text(tt::TokenId(13), T!['{']), "{"); } -#[test] -fn test_match_group_with_multichar_sep() { - parse_macro( - r#" - macro_rules! foo { - (fn $name:ident {$($i:literal)*} ) => ( fn $name() -> bool { $($i)&&*} ); - }"#, - ) - .assert_expand_items("foo! (fn baz {true true} );", "fn baz () -> bool {true &&true}"); -} - -#[test] -fn test_match_group_with_multichar_sep2() { - parse_macro( - r#" - macro_rules! foo { - (fn $name:ident {$($i:literal)&&*} ) => ( fn $name() -> bool { $($i)&&*} ); - }"#, - ) - .assert_expand_items("foo! (fn baz {true && true} );", "fn baz () -> bool {true &&true}"); -} - #[test] fn test_match_group_zero_match() { parse_macro(