From bb601e7eafa00e471a5306ac920f0be6c809aab0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 30 Nov 2019 23:29:21 +0800 Subject: Add BuiltinShadowMode --- crates/ra_hir_def/src/nameres.rs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def/src/nameres.rs') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index df42ea84a..9aaf7736b 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -149,6 +149,16 @@ static BUILTIN_SCOPE: Lazy> = Lazy::new(|| { .collect() }); +/// Shadow mode for builtin type +/// Builtin type can be shadowed by same name mode +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum BuiltinShadowMode { + // Prefer Module + Module, + // Prefer Other Types + Other, +} + /// Legacy macros can only be accessed through special methods like `get_legacy_macros`. /// Other methods will only resolve values, types and module scoped macros only. impl ModuleScope { @@ -178,8 +188,20 @@ impl ModuleScope { } /// Get a name from current module scope, legacy macros are not included - pub fn get(&self, name: &Name) -> Option<&Resolution> { - self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)) + pub fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&Resolution> { + match shadow { + BuiltinShadowMode::Module => self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)), + BuiltinShadowMode::Other => { + let item = self.items.get(name); + if let Some(res) = item { + if let Some(ModuleDefId::ModuleId(_)) = res.def.take_types() { + return BUILTIN_SCOPE.get(name).or(item); + } + } + + item.or_else(|| BUILTIN_SCOPE.get(name)) + } + } } pub fn traits<'a>(&'a self) -> impl Iterator + 'a { @@ -250,8 +272,10 @@ impl CrateDefMap { db: &impl DefDatabase, original_module: LocalModuleId, path: &Path, + shadow: BuiltinShadowMode, ) -> (PerNs, Option) { - let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); + let res = + self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path, shadow); (res.resolved_def, res.segment_index) } } -- cgit v1.2.3