From 68a74decb683929f59588e07636e44c40434d6bc Mon Sep 17 00:00:00 2001 From: Ryo Yoshida Date: Wed, 17 May 2023 17:45:44 +0900 Subject: [PATCH] Process `macro_use` prelude in semantic scope resolver --- crates/hir-def/src/find_path.rs | 2 +- crates/hir-def/src/nameres.rs | 8 ++++++-- crates/hir-def/src/resolver.rs | 5 ++++- crates/ide-completion/src/tests/flyimport.rs | 21 ++++++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/crates/hir-def/src/find_path.rs b/crates/hir-def/src/find_path.rs index b401762255e..1e299fecc9d 100644 --- a/crates/hir-def/src/find_path.rs +++ b/crates/hir-def/src/find_path.rs @@ -183,7 +183,7 @@ fn find_path_for_module( // - if the item is the crate root of a dependency crate, return the name from the extern prelude let root_def_map = crate_root.def_map(db); - for (name, &def_id) in root_def_map.extern_prelude() { + for (name, def_id) in root_def_map.extern_prelude() { if module_id == def_id { let name = scope_name.unwrap_or_else(|| name.clone()); diff --git a/crates/hir-def/src/nameres.rs b/crates/hir-def/src/nameres.rs index 39a56814ed8..176637f9d0d 100644 --- a/crates/hir-def/src/nameres.rs +++ b/crates/hir-def/src/nameres.rs @@ -355,8 +355,12 @@ pub(crate) fn prelude(&self) -> Option { self.prelude } - pub(crate) fn extern_prelude(&self) -> impl Iterator + '_ { - self.extern_prelude.iter() + pub(crate) fn extern_prelude(&self) -> impl Iterator + '_ { + self.extern_prelude.iter().map(|(name, def)| (name, *def)) + } + + pub(crate) fn macro_use_prelude(&self) -> impl Iterator + '_ { + self.macro_use_prelude.iter().map(|(name, def)| (name, *def)) } pub fn module_id(&self, local_id: LocalModuleId) -> ModuleId { diff --git a/crates/hir-def/src/resolver.rs b/crates/hir-def/src/resolver.rs index 4bec2b4dea4..afa3b33cc9f 100644 --- a/crates/hir-def/src/resolver.rs +++ b/crates/hir-def/src/resolver.rs @@ -461,7 +461,10 @@ pub fn names_in_scope( res.add(name, ScopeDef::ModuleDef(ModuleDefId::MacroId(mac))); }) }); - def_map.extern_prelude().for_each(|(name, &def)| { + def_map.macro_use_prelude().for_each(|(name, def)| { + res.add(name, ScopeDef::ModuleDef(def.into())); + }); + def_map.extern_prelude().for_each(|(name, def)| { res.add(name, ScopeDef::ModuleDef(ModuleDefId::ModuleId(def))); }); BUILTIN_SCOPE.iter().for_each(|(name, &def)| { diff --git a/crates/ide-completion/src/tests/flyimport.rs b/crates/ide-completion/src/tests/flyimport.rs index d727320b514..8c038c0fbaa 100644 --- a/crates/ide-completion/src/tests/flyimport.rs +++ b/crates/ide-completion/src/tests/flyimport.rs @@ -1265,3 +1265,24 @@ macro_rules! define_struct { "#]], ); } + +#[test] +fn macro_use_prelude_is_in_scope() { + check( + r#" +//- /main.rs crate:main deps:dep +#[macro_use] +extern crate dep; + +fn main() { + print$0 +} +//- /lib.rs crate:dep +#[macro_export] +macro_rules! println { + () => {} +} +"#, + expect![""], + ) +}