diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-30 14:13:06 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-30 14:13:06 +0100 |
commit | 3cf841e1bc2bf354c180dc80fb394c0e0a843965 (patch) | |
tree | f01cf9c3589ae920c50696ce244c98b2454834b3 /crates/ra_hir/src/nameres.rs | |
parent | 6b88735fe6cd3b259816c7c90a2675ee057c9e4c (diff) | |
parent | b6a854e161cc122e6d9ae12084b6a1d1f4d0f241 (diff) |
Merge #1352
1352: Builtins r=matklad a=matklad
closes #1340
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/nameres.rs')
-rw-r--r-- | crates/ra_hir/src/nameres.rs | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 42dcac332..aa26345b2 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs | |||
@@ -62,9 +62,10 @@ use ra_db::{FileId, Edition}; | |||
62 | use test_utils::tested_by; | 62 | use test_utils::tested_by; |
63 | use ra_syntax::ast; | 63 | use ra_syntax::ast; |
64 | use ra_prof::profile; | 64 | use ra_prof::profile; |
65 | use once_cell::sync::Lazy; | ||
65 | 66 | ||
66 | use crate::{ | 67 | use crate::{ |
67 | ModuleDef, Name, Crate, Module, MacroDef, | 68 | ModuleDef, Name, Crate, Module, MacroDef, AsName, BuiltinType, |
68 | DefDatabase, Path, PathKind, HirFileId, Trait, | 69 | DefDatabase, Path, PathKind, HirFileId, Trait, |
69 | ids::MacroDefId, | 70 | ids::MacroDefId, |
70 | diagnostics::DiagnosticSink, | 71 | diagnostics::DiagnosticSink, |
@@ -140,12 +141,22 @@ pub struct ModuleScope { | |||
140 | macros: FxHashMap<Name, MacroDef>, | 141 | macros: FxHashMap<Name, MacroDef>, |
141 | } | 142 | } |
142 | 143 | ||
144 | static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| { | ||
145 | BuiltinType::ALL | ||
146 | .iter() | ||
147 | .map(|&(known_name, ty)| { | ||
148 | (known_name.as_name(), Resolution { def: PerNs::types(ty.into()), import: None }) | ||
149 | }) | ||
150 | .collect() | ||
151 | }); | ||
152 | |||
143 | impl ModuleScope { | 153 | impl ModuleScope { |
144 | pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a { | 154 | pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a { |
145 | self.items.iter() | 155 | //FIXME: shadowing |
156 | self.items.iter().chain(BUILTIN_SCOPE.iter()) | ||
146 | } | 157 | } |
147 | pub fn get(&self, name: &Name) -> Option<&Resolution> { | 158 | pub fn get(&self, name: &Name) -> Option<&Resolution> { |
148 | self.items.get(name) | 159 | self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)) |
149 | } | 160 | } |
150 | pub fn traits<'a>(&'a self) -> impl Iterator<Item = Trait> + 'a { | 161 | pub fn traits<'a>(&'a self) -> impl Iterator<Item = Trait> + 'a { |
151 | self.items.values().filter_map(|r| match r.def.take_types() { | 162 | self.items.values().filter_map(|r| match r.def.take_types() { |
@@ -154,7 +165,7 @@ impl ModuleScope { | |||
154 | }) | 165 | }) |
155 | } | 166 | } |
156 | fn get_item_or_macro(&self, name: &Name) -> Option<ItemOrMacro> { | 167 | fn get_item_or_macro(&self, name: &Name) -> Option<ItemOrMacro> { |
157 | match (self.items.get(name), self.macros.get(name)) { | 168 | match (self.get(name), self.macros.get(name)) { |
158 | (Some(item), _) if !item.def.is_none() => Some(Either::Left(item.def)), | 169 | (Some(item), _) if !item.def.is_none() => Some(Either::Left(item.def)), |
159 | (_, Some(macro_)) => Some(Either::Right(*macro_)), | 170 | (_, Some(macro_)) => Some(Either::Right(*macro_)), |
160 | _ => None, | 171 | _ => None, |