aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/nameres.rs
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-11-30 15:29:21 +0000
committerEdwin Cheng <[email protected]>2019-11-30 15:29:21 +0000
commitbb601e7eafa00e471a5306ac920f0be6c809aab0 (patch)
treed6713da23941b3e4daac0914d77221c357558db8 /crates/ra_hir_def/src/nameres.rs
parent8b278b1ab660df0728508e45e88ac769a2e03a58 (diff)
Add BuiltinShadowMode
Diffstat (limited to 'crates/ra_hir_def/src/nameres.rs')
-rw-r--r--crates/ra_hir_def/src/nameres.rs30
1 files changed, 27 insertions, 3 deletions
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<FxHashMap<Name, Resolution>> = Lazy::new(|| {
149 .collect() 149 .collect()
150}); 150});
151 151
152/// Shadow mode for builtin type
153/// Builtin type can be shadowed by same name mode
154#[derive(Debug, Copy, Clone, PartialEq, Eq)]
155pub enum BuiltinShadowMode {
156 // Prefer Module
157 Module,
158 // Prefer Other Types
159 Other,
160}
161
152/// Legacy macros can only be accessed through special methods like `get_legacy_macros`. 162/// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
153/// Other methods will only resolve values, types and module scoped macros only. 163/// Other methods will only resolve values, types and module scoped macros only.
154impl ModuleScope { 164impl ModuleScope {
@@ -178,8 +188,20 @@ impl ModuleScope {
178 } 188 }
179 189
180 /// Get a name from current module scope, legacy macros are not included 190 /// Get a name from current module scope, legacy macros are not included
181 pub fn get(&self, name: &Name) -> Option<&Resolution> { 191 pub fn get(&self, name: &Name, shadow: BuiltinShadowMode) -> Option<&Resolution> {
182 self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)) 192 match shadow {
193 BuiltinShadowMode::Module => self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)),
194 BuiltinShadowMode::Other => {
195 let item = self.items.get(name);
196 if let Some(res) = item {
197 if let Some(ModuleDefId::ModuleId(_)) = res.def.take_types() {
198 return BUILTIN_SCOPE.get(name).or(item);
199 }
200 }
201
202 item.or_else(|| BUILTIN_SCOPE.get(name))
203 }
204 }
183 } 205 }
184 206
185 pub fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a { 207 pub fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a {
@@ -250,8 +272,10 @@ impl CrateDefMap {
250 db: &impl DefDatabase, 272 db: &impl DefDatabase,
251 original_module: LocalModuleId, 273 original_module: LocalModuleId,
252 path: &Path, 274 path: &Path,
275 shadow: BuiltinShadowMode,
253 ) -> (PerNs, Option<usize>) { 276 ) -> (PerNs, Option<usize>) {
254 let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); 277 let res =
278 self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path, shadow);
255 (res.resolved_def, res.segment_index) 279 (res.resolved_def, res.segment_index)
256 } 280 }
257} 281}