Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/macro_expansion_tests/mod.rs')
| -rw-r--r-- | crates/hir-def/src/macro_expansion_tests/mod.rs | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/crates/hir-def/src/macro_expansion_tests/mod.rs b/crates/hir-def/src/macro_expansion_tests/mod.rs index f129358946..5b9ffdf37b 100644 --- a/crates/hir-def/src/macro_expansion_tests/mod.rs +++ b/crates/hir-def/src/macro_expansion_tests/mod.rs @@ -16,14 +16,16 @@ mod proc_macros; use std::{iter, ops::Range, sync}; +use base_db::SourceDatabase; use expect_test::Expect; use hir_expand::{ db::ExpandDatabase, proc_macro::{ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind}, span_map::SpanMapRef, - InFile, MacroFileId, MacroFileIdExt, + InFile, MacroCallKind, MacroFileId, MacroFileIdExt, }; use intern::Symbol; +use itertools::Itertools; use span::{Edition, Span}; use stdx::{format_to, format_to_acc}; use syntax::{ @@ -45,6 +47,36 @@ use crate::{ }; #[track_caller] +fn check_errors(ra_fixture: &str, expect: Expect) { + let db = TestDB::with_files(ra_fixture); + let krate = db.fetch_test_crate(); + let def_map = db.crate_def_map(krate); + let errors = def_map + .modules() + .flat_map(|module| module.1.scope.all_macro_calls()) + .filter_map(|macro_call| { + let errors = db.parse_macro_expansion_error(macro_call)?; + let errors = errors.err.as_ref()?.render_to_string(&db); + let macro_loc = db.lookup_intern_macro_call(macro_call); + let ast_id = match macro_loc.kind { + MacroCallKind::FnLike { ast_id, .. } => ast_id.map(|it| it.erase()), + MacroCallKind::Derive { ast_id, .. } => ast_id.map(|it| it.erase()), + MacroCallKind::Attr { ast_id, .. } => ast_id.map(|it| it.erase()), + }; + let ast = db + .parse(ast_id.file_id.file_id().expect("macros inside macros are not supported")) + .syntax_node(); + let ast_id_map = db.ast_id_map(ast_id.file_id); + let node = ast_id_map.get_erased(ast_id.value).to_node(&ast); + Some((node.text_range(), errors)) + }) + .sorted_unstable_by_key(|(range, _)| range.start()) + .format_with("\n", |(range, err), format| format(&format_args!("{range:?}: {err}"))) + .to_string(); + expect.assert_eq(&errors); +} + +#[track_caller] fn check(ra_fixture: &str, mut expect: Expect) { let extra_proc_macros = vec![( r#" @@ -245,7 +277,9 @@ fn pretty_print_macro_expansion( let mut res = String::new(); let mut prev_kind = EOF; let mut indent_level = 0; - for token in iter::successors(expn.first_token(), |t| t.next_token()) { + for token in iter::successors(expn.first_token(), |t| t.next_token()) + .take_while(|token| token.text_range().start() < expn.text_range().end()) + { let curr_kind = token.kind(); let space = match (prev_kind, curr_kind) { _ if prev_kind.is_trivia() || curr_kind.is_trivia() => "", |