From e04c0f42227b2c72aa8ea40d6aea873669428267 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 4 Mar 2020 02:41:33 +0800 Subject: [PATCH 1/2] Fix panic on eager expansion --- crates/ra_hir_expand/src/eager.rs | 20 ++++++++++++++++++-- crates/ra_hir_ty/src/tests/macros.rs | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/crates/ra_hir_expand/src/eager.rs b/crates/ra_hir_expand/src/eager.rs index 7fcdfab5ae..1dba1179a6 100644 --- a/crates/ra_hir_expand/src/eager.rs +++ b/crates/ra_hir_expand/src/eager.rs @@ -37,9 +37,25 @@ pub fn expand_eager_macro( ) -> Option { let args = macro_call.value.token_tree()?; let parsed_args = mbe::ast_to_token_tree(&args)?.0; - let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, FragmentKind::Expr).ok()?.0; - let result = eager_macro_recur(db, macro_call.with_value(parsed_args.syntax_node()), resolver)?; + // Note: + // When `lazy_expand` is called, its *parent* file must be already exists. + // Here we store an eager macro id for the argument expaned here + // for that purpose. + let arg_id: MacroCallId = db + .intern_eager_expansion({ + EagerCallLoc { + def, + fragment: FragmentKind::Expr, + subtree: Arc::new(parsed_args.clone()), + file_id: macro_call.file_id, + } + }) + .into(); + + let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, FragmentKind::Expr).ok()?.0; + let result = + eager_macro_recur(db, InFile::new(arg_id.as_file(), parsed_args.syntax_node()), resolver)?; let subtree = to_subtree(&result)?; if let MacroDefKind::BuiltInEager(eager) = def.kind { diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs index 55386c0303..5d0efa0f40 100644 --- a/crates/ra_hir_ty/src/tests/macros.rs +++ b/crates/ra_hir_ty/src/tests/macros.rs @@ -438,6 +438,27 @@ fn main() { ); } +#[test] +fn infer_builtin_macros_concat_with_lazy() { + assert_snapshot!( + infer(r#" +macro_rules! hello {() => {"hello"}} + +#[rustc_builtin_macro] +macro_rules! concat {() => {}} + +fn main() { + let x = concat!(hello!(), concat!("world", "!")); +} +"#), + @r###" + ![0; 13) '"helloworld!"': &str + [104; 161) '{ ...")); }': () + [114; 115) 'x': &str + "### + ); +} + #[test] fn infer_derive_clone_simple() { let (db, pos) = TestDB::with_position( From 5ea83fee010a26e86e962cfb8270fd2a565277b3 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 4 Mar 2020 02:57:54 +0800 Subject: [PATCH 2/2] Fixed a typo in comment --- crates/ra_hir_expand/src/eager.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_hir_expand/src/eager.rs b/crates/ra_hir_expand/src/eager.rs index 1dba1179a6..f95f37ede1 100644 --- a/crates/ra_hir_expand/src/eager.rs +++ b/crates/ra_hir_expand/src/eager.rs @@ -40,7 +40,7 @@ pub fn expand_eager_macro( // Note: // When `lazy_expand` is called, its *parent* file must be already exists. - // Here we store an eager macro id for the argument expaned here + // Here we store an eager macro id for the argument expanded subtree here // for that purpose. let arg_id: MacroCallId = db .intern_eager_expansion({