aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-21 09:21:46 +0000
committerAleksey Kladov <[email protected]>2019-11-21 10:25:03 +0000
commit0102fb41337ac0442e689d410bb424d215e9a7bd (patch)
treec77bf501427adc77c0fc92fe75fd9f314087b120 /crates
parent612a72fc4ea4376920f2a7da7b3c334227c1716c (diff)
Decouple Resolver
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs18
-rw-r--r--crates/ra_hir/src/expr.rs4
-rw-r--r--crates/ra_hir/src/from_id.rs65
-rw-r--r--crates/ra_hir/src/resolve.rs117
-rw-r--r--crates/ra_hir/src/source_binder.rs21
-rw-r--r--crates/ra_hir/src/ty/infer.rs23
-rw-r--r--crates/ra_hir/src/ty/infer/path.rs4
-rw-r--r--crates/ra_hir/src/ty/lower.rs31
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs4
-rw-r--r--crates/ra_hir_def/src/traits.rs9
10 files changed, 188 insertions, 108 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index a132d128b..550ab1a98 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -12,8 +12,8 @@ use hir_def::{
12 builtin_type::BuiltinType, 12 builtin_type::BuiltinType,
13 traits::TraitData, 13 traits::TraitData,
14 type_ref::{Mutability, TypeRef}, 14 type_ref::{Mutability, TypeRef},
15 AssocItemId, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, 15 ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup,
16 LocalStructFieldId, Lookup, ModuleId, UnionId, 16 ModuleId, UnionId,
17}; 17};
18use hir_expand::{ 18use hir_expand::{
19 diagnostics::DiagnosticSink, 19 diagnostics::DiagnosticSink,
@@ -842,9 +842,10 @@ impl Trait {
842 _ => None, 842 _ => None,
843 }) 843 })
844 .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path) { 844 .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path) {
845 Some(TypeNs::Trait(t)) => Some(t), 845 Some(TypeNs::TraitId(t)) => Some(t),
846 _ => None, 846 _ => None,
847 }) 847 })
848 .map(Trait::from)
848 .collect() 849 .collect()
849 } 850 }
850 851
@@ -871,14 +872,9 @@ impl Trait {
871 872
872 pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> { 873 pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> {
873 let trait_data = self.trait_data(db); 874 let trait_data = self.trait_data(db);
874 trait_data 875 let res =
875 .items 876 trait_data.associated_types().map(TypeAlias::from).find(|t| &t.name(db) == name)?;
876 .iter() 877 Some(res)
877 .filter_map(|item| match item {
878 AssocItemId::TypeAliasId(t) => Some(TypeAlias::from(*t)),
879 _ => None,
880 })
881 .find(|t| &t.name(db) == name)
882 } 878 }
883 879
884 pub fn associated_type_by_name_including_super_traits( 880 pub fn associated_type_by_name_including_super_traits(
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 869879bdf..8c4c63fda 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -46,7 +46,7 @@ pub(crate) fn resolver_for_scope(
46 let scopes = owner.expr_scopes(db); 46 let scopes = owner.expr_scopes(db);
47 let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>(); 47 let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
48 for scope in scope_chain.into_iter().rev() { 48 for scope in scope_chain.into_iter().rev() {
49 r = r.push_expr_scope(owner, Arc::clone(&scopes), scope); 49 r = r.push_expr_scope(owner.into(), Arc::clone(&scopes), scope);
50 } 50 }
51 r 51 r
52} 52}
@@ -152,7 +152,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
152 _ => return, 152 _ => return,
153 }; 153 };
154 154
155 let std_result_ctor = TypeCtor::Adt(Adt::Enum(std_result_enum)); 155 let std_result_ctor = TypeCtor::Adt(Adt::Enum(std_result_enum.into()));
156 let params = match &mismatch.expected { 156 let params = match &mismatch.expected {
157 Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &std_result_ctor => parameters, 157 Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &std_result_ctor => parameters,
158 _ => return, 158 _ => return,
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs
index b7692d407..7042422cc 100644
--- a/crates/ra_hir/src/from_id.rs
+++ b/crates/ra_hir/src/from_id.rs
@@ -3,9 +3,14 @@
3//! It's unclear if we need this long-term, but it's definitelly useful while we 3//! It's unclear if we need this long-term, but it's definitelly useful while we
4//! are splitting the hir. 4//! are splitting the hir.
5 5
6use hir_def::{AdtId, AssocItemId, DefWithBodyId, EnumVariantId, GenericDefId, ModuleDefId}; 6use hir_def::{
7 AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ModuleDefId, StructId,
8 TypeAliasId, UnionId,
9};
7 10
8use crate::{Adt, AssocItem, DefWithBody, EnumVariant, GenericDef, ModuleDef}; 11use crate::{
12 ty::TypableDef, Adt, AssocItem, DefWithBody, EnumVariant, GenericDef, ModuleDef, TypeAlias,
13};
9 14
10macro_rules! from_id { 15macro_rules! from_id {
11 ($(($id:path, $ty:path)),*) => {$( 16 ($(($id:path, $ty:path)),*) => {$(
@@ -83,6 +88,16 @@ impl From<DefWithBody> for DefWithBodyId {
83 } 88 }
84} 89}
85 90
91impl From<DefWithBodyId> for DefWithBody {
92 fn from(def: DefWithBodyId) -> Self {
93 match def {
94 DefWithBodyId::FunctionId(it) => DefWithBody::Function(it.into()),
95 DefWithBodyId::StaticId(it) => DefWithBody::Static(it.into()),
96 DefWithBodyId::ConstId(it) => DefWithBody::Const(it.into()),
97 }
98 }
99}
100
86impl From<AssocItemId> for AssocItem { 101impl From<AssocItemId> for AssocItem {
87 fn from(def: AssocItemId) -> Self { 102 fn from(def: AssocItemId) -> Self {
88 match def { 103 match def {
@@ -122,3 +137,49 @@ impl From<GenericDefId> for GenericDef {
122 } 137 }
123 } 138 }
124} 139}
140
141impl From<AdtId> for TypableDef {
142 fn from(id: AdtId) -> Self {
143 Adt::from(id).into()
144 }
145}
146
147impl From<StructId> for TypableDef {
148 fn from(id: StructId) -> Self {
149 AdtId::StructId(id).into()
150 }
151}
152
153impl From<UnionId> for TypableDef {
154 fn from(id: UnionId) -> Self {
155 AdtId::UnionId(id).into()
156 }
157}
158
159impl From<EnumId> for TypableDef {
160 fn from(id: EnumId) -> Self {
161 AdtId::EnumId(id).into()
162 }
163}
164
165impl From<EnumVariantId> for TypableDef {
166 fn from(id: EnumVariantId) -> Self {
167 EnumVariant::from(id).into()
168 }
169}
170
171impl From<TypeAliasId> for TypableDef {
172 fn from(id: TypeAliasId) -> Self {
173 TypeAlias::from(id).into()
174 }
175}
176
177impl From<Adt> for GenericDefId {
178 fn from(id: Adt) -> Self {
179 match id {
180 Adt::Struct(it) => it.id.into(),
181 Adt::Union(it) => it.id.into(),
182 Adt::Enum(it) => it.id.into(),
183 }
184 }
185}
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index eca8e0596..f4165babd 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -3,19 +3,21 @@ use std::sync::Arc;
3 3
4use hir_def::{ 4use hir_def::{
5 builtin_type::BuiltinType, 5 builtin_type::BuiltinType,
6 db::DefDatabase2,
7 generics::GenericParams,
6 nameres::CrateDefMap, 8 nameres::CrateDefMap,
7 path::{Path, PathKind}, 9 path::{Path, PathKind},
8 AdtId, CrateModuleId, ModuleDefId, 10 AdtId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ImplId, ModuleDefId,
11 StructId, TraitId, TypeAliasId,
9}; 12};
10use hir_expand::name::{self, Name}; 13use hir_expand::name::{self, Name};
11use rustc_hash::FxHashSet; 14use rustc_hash::FxHashSet;
12 15
13use crate::{ 16use crate::{
14 code_model::Crate, 17 code_model::Crate,
15 db::{DefDatabase, HirDatabase}, 18 db::DefDatabase,
16 expr::{ExprScopes, PatId, ScopeId}, 19 expr::{ExprScopes, PatId, ScopeId},
17 generics::{GenericParams, HasGenericParams}, 20 Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, ImplBlock, Local,
18 Adt, Const, Container, DefWithBody, Enum, EnumVariant, Function, GenericDef, ImplBlock, Local,
19 MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias, 21 MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias,
20}; 22};
21 23
@@ -33,7 +35,7 @@ pub(crate) struct ModuleItemMap {
33 35
34#[derive(Debug, Clone)] 36#[derive(Debug, Clone)]
35pub(crate) struct ExprScope { 37pub(crate) struct ExprScope {
36 owner: DefWithBody, 38 owner: DefWithBodyId,
37 expr_scopes: Arc<ExprScopes>, 39 expr_scopes: Arc<ExprScopes>,
38 scope_id: ScopeId, 40 scope_id: ScopeId,
39} 41}
@@ -43,28 +45,28 @@ pub(crate) enum Scope {
43 /// All the items and imported names of a module 45 /// All the items and imported names of a module
44 ModuleScope(ModuleItemMap), 46 ModuleScope(ModuleItemMap),
45 /// Brings the generic parameters of an item into scope 47 /// Brings the generic parameters of an item into scope
46 GenericParams { def: GenericDef, params: Arc<GenericParams> }, 48 GenericParams { def: GenericDefId, params: Arc<GenericParams> },
47 /// Brings `Self` in `impl` block into scope 49 /// Brings `Self` in `impl` block into scope
48 ImplBlockScope(ImplBlock), 50 ImplBlockScope(ImplId),
49 /// Brings `Self` in enum, struct and union definitions into scope 51 /// Brings `Self` in enum, struct and union definitions into scope
50 AdtScope(Adt), 52 AdtScope(AdtId),
51 /// Local bindings 53 /// Local bindings
52 ExprScope(ExprScope), 54 ExprScope(ExprScope),
53} 55}
54 56
55#[derive(Debug, Clone, PartialEq, Eq, Hash)] 57#[derive(Debug, Clone, PartialEq, Eq, Hash)]
56pub(crate) enum TypeNs { 58pub(crate) enum TypeNs {
57 SelfType(ImplBlock), 59 SelfType(ImplId),
58 GenericParam(u32), 60 GenericParam(u32),
59 Adt(Adt), 61 AdtId(AdtId),
60 AdtSelfType(Adt), 62 AdtSelfType(AdtId),
61 EnumVariant(EnumVariant), 63 EnumVariantId(EnumVariantId),
62 TypeAlias(TypeAlias), 64 TypeAliasId(TypeAliasId),
63 BuiltinType(BuiltinType), 65 BuiltinType(BuiltinType),
64 Trait(Trait), 66 TraitId(TraitId),
65 // Module belong to type ns, but the resolver is used when all module paths 67 // Module belong to type ns, but the resolver is used when all module paths
66 // are fully resolved. 68 // are fully resolved.
67 // Module(Module) 69 // ModuleId(ModuleId)
68} 70}
69 71
70#[derive(Debug, Clone, PartialEq, Eq, Hash)] 72#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -85,10 +87,14 @@ pub(crate) enum ValueNs {
85 87
86impl Resolver { 88impl Resolver {
87 /// Resolve known trait from std, like `std::futures::Future` 89 /// Resolve known trait from std, like `std::futures::Future`
88 pub(crate) fn resolve_known_trait(&self, db: &impl HirDatabase, path: &Path) -> Option<Trait> { 90 pub(crate) fn resolve_known_trait(
91 &self,
92 db: &impl DefDatabase2,
93 path: &Path,
94 ) -> Option<TraitId> {
89 let res = self.resolve_module_path(db, path).take_types()?; 95 let res = self.resolve_module_path(db, path).take_types()?;
90 match res { 96 match res {
91 ModuleDefId::TraitId(it) => Some(it.into()), 97 ModuleDefId::TraitId(it) => Some(it),
92 _ => None, 98 _ => None,
93 } 99 }
94 } 100 }
@@ -96,27 +102,27 @@ impl Resolver {
96 /// Resolve known struct from std, like `std::boxed::Box` 102 /// Resolve known struct from std, like `std::boxed::Box`
97 pub(crate) fn resolve_known_struct( 103 pub(crate) fn resolve_known_struct(
98 &self, 104 &self,
99 db: &impl HirDatabase, 105 db: &impl DefDatabase2,
100 path: &Path, 106 path: &Path,
101 ) -> Option<Struct> { 107 ) -> Option<StructId> {
102 let res = self.resolve_module_path(db, path).take_types()?; 108 let res = self.resolve_module_path(db, path).take_types()?;
103 match res { 109 match res {
104 ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it.into()), 110 ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it),
105 _ => None, 111 _ => None,
106 } 112 }
107 } 113 }
108 114
109 /// Resolve known enum from std, like `std::result::Result` 115 /// Resolve known enum from std, like `std::result::Result`
110 pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> Option<Enum> { 116 pub(crate) fn resolve_known_enum(&self, db: &impl DefDatabase2, path: &Path) -> Option<EnumId> {
111 let res = self.resolve_module_path(db, path).take_types()?; 117 let res = self.resolve_module_path(db, path).take_types()?;
112 match res { 118 match res {
113 ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it.into()), 119 ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it),
114 _ => None, 120 _ => None,
115 } 121 }
116 } 122 }
117 123
118 /// pub only for source-binder 124 /// pub only for source-binder
119 pub(crate) fn resolve_module_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs { 125 pub(crate) fn resolve_module_path(&self, db: &impl DefDatabase2, path: &Path) -> PerNs {
120 let (item_map, module) = match self.module() { 126 let (item_map, module) = match self.module() {
121 Some(it) => it, 127 Some(it) => it,
122 None => return PerNs::none(), 128 None => return PerNs::none(),
@@ -130,7 +136,7 @@ impl Resolver {
130 136
131 pub(crate) fn resolve_path_in_type_ns( 137 pub(crate) fn resolve_path_in_type_ns(
132 &self, 138 &self,
133 db: &impl HirDatabase, 139 db: &impl DefDatabase2,
134 path: &Path, 140 path: &Path,
135 ) -> Option<(TypeNs, Option<usize>)> { 141 ) -> Option<(TypeNs, Option<usize>)> {
136 if path.is_type_relative() { 142 if path.is_type_relative() {
@@ -164,13 +170,13 @@ impl Resolver {
164 Scope::ModuleScope(m) => { 170 Scope::ModuleScope(m) => {
165 let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path); 171 let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path);
166 let res = match module_def.take_types()? { 172 let res = match module_def.take_types()? {
167 ModuleDefId::AdtId(it) => TypeNs::Adt(it.into()), 173 ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
168 ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariant(it.into()), 174 ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it),
169 175
170 ModuleDefId::TypeAliasId(it) => TypeNs::TypeAlias(it.into()), 176 ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
171 ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it), 177 ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
172 178
173 ModuleDefId::TraitId(it) => TypeNs::Trait(it.into()), 179 ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
174 180
175 ModuleDefId::FunctionId(_) 181 ModuleDefId::FunctionId(_)
176 | ModuleDefId::ConstId(_) 182 | ModuleDefId::ConstId(_)
@@ -186,7 +192,7 @@ impl Resolver {
186 192
187 pub(crate) fn resolve_path_in_type_ns_fully( 193 pub(crate) fn resolve_path_in_type_ns_fully(
188 &self, 194 &self,
189 db: &impl HirDatabase, 195 db: &impl DefDatabase2,
190 path: &Path, 196 path: &Path,
191 ) -> Option<TypeNs> { 197 ) -> Option<TypeNs> {
192 let (res, unresolved) = self.resolve_path_in_type_ns(db, path)?; 198 let (res, unresolved) = self.resolve_path_in_type_ns(db, path)?;
@@ -198,7 +204,7 @@ impl Resolver {
198 204
199 pub(crate) fn resolve_path_in_value_ns<'p>( 205 pub(crate) fn resolve_path_in_value_ns<'p>(
200 &self, 206 &self,
201 db: &impl HirDatabase, 207 db: &impl DefDatabase2,
202 path: &'p Path, 208 path: &'p Path,
203 ) -> Option<ResolveValueResult> { 209 ) -> Option<ResolveValueResult> {
204 if path.is_type_relative() { 210 if path.is_type_relative() {
@@ -278,9 +284,9 @@ impl Resolver {
278 } 284 }
279 Some(idx) => { 285 Some(idx) => {
280 let ty = match module_def.take_types()? { 286 let ty = match module_def.take_types()? {
281 ModuleDefId::AdtId(it) => TypeNs::Adt(it.into()), 287 ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
282 ModuleDefId::TraitId(it) => TypeNs::Trait(it.into()), 288 ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
283 ModuleDefId::TypeAliasId(it) => TypeNs::TypeAlias(it.into()), 289 ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
284 ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it), 290 ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
285 291
286 ModuleDefId::ModuleId(_) 292 ModuleDefId::ModuleId(_)
@@ -300,7 +306,7 @@ impl Resolver {
300 306
301 pub(crate) fn resolve_path_in_value_ns_fully( 307 pub(crate) fn resolve_path_in_value_ns_fully(
302 &self, 308 &self,
303 db: &impl HirDatabase, 309 db: &impl DefDatabase2,
304 path: &Path, 310 path: &Path,
305 ) -> Option<ValueNs> { 311 ) -> Option<ValueNs> {
306 match self.resolve_path_in_value_ns(db, path)? { 312 match self.resolve_path_in_value_ns(db, path)? {
@@ -311,7 +317,7 @@ impl Resolver {
311 317
312 pub(crate) fn resolve_path_as_macro( 318 pub(crate) fn resolve_path_as_macro(
313 &self, 319 &self,
314 db: &impl DefDatabase, 320 db: &impl DefDatabase2,
315 path: &Path, 321 path: &Path,
316 ) -> Option<MacroDef> { 322 ) -> Option<MacroDef> {
317 let (item_map, module) = self.module()?; 323 let (item_map, module) = self.module()?;
@@ -320,7 +326,7 @@ impl Resolver {
320 326
321 pub(crate) fn process_all_names( 327 pub(crate) fn process_all_names(
322 &self, 328 &self,
323 db: &impl HirDatabase, 329 db: &impl DefDatabase2,
324 f: &mut dyn FnMut(Name, ScopeDef), 330 f: &mut dyn FnMut(Name, ScopeDef),
325 ) { 331 ) {
326 for scope in self.scopes.iter().rev() { 332 for scope in self.scopes.iter().rev() {
@@ -328,16 +334,15 @@ impl Resolver {
328 } 334 }
329 } 335 }
330 336
331 pub(crate) fn traits_in_scope(&self, db: &impl HirDatabase) -> FxHashSet<Trait> { 337 pub(crate) fn traits_in_scope(&self, db: &impl DefDatabase2) -> FxHashSet<TraitId> {
332 let mut traits = FxHashSet::default(); 338 let mut traits = FxHashSet::default();
333 for scope in &self.scopes { 339 for scope in &self.scopes {
334 if let Scope::ModuleScope(m) = scope { 340 if let Scope::ModuleScope(m) = scope {
335 if let Some(prelude) = m.crate_def_map.prelude() { 341 if let Some(prelude) = m.crate_def_map.prelude() {
336 let prelude_def_map = db.crate_def_map(prelude.krate); 342 let prelude_def_map = db.crate_def_map(prelude.krate);
337 traits 343 traits.extend(prelude_def_map[prelude.module_id].scope.traits());
338 .extend(prelude_def_map[prelude.module_id].scope.traits().map(Trait::from));
339 } 344 }
340 traits.extend(m.crate_def_map[m.module_id].scope.traits().map(Trait::from)); 345 traits.extend(m.crate_def_map[m.module_id].scope.traits());
341 } 346 }
342 } 347 }
343 traits 348 traits
@@ -367,7 +372,7 @@ impl Resolver {
367 .flat_map(|params| params.where_predicates.iter()) 372 .flat_map(|params| params.where_predicates.iter())
368 } 373 }
369 374
370 pub(crate) fn generic_def(&self) -> Option<crate::generics::GenericDef> { 375 pub(crate) fn generic_def(&self) -> Option<GenericDefId> {
371 self.scopes.iter().find_map(|scope| match scope { 376 self.scopes.iter().find_map(|scope| match scope {
372 Scope::GenericParams { def, .. } => Some(*def), 377 Scope::GenericParams { def, .. } => Some(*def),
373 _ => None, 378 _ => None,
@@ -383,10 +388,10 @@ impl Resolver {
383 388
384 pub(crate) fn push_generic_params_scope( 389 pub(crate) fn push_generic_params_scope(
385 self, 390 self,
386 db: &impl DefDatabase, 391 db: &impl DefDatabase2,
387 def: GenericDef, 392 def: GenericDefId,
388 ) -> Resolver { 393 ) -> Resolver {
389 let params = def.generic_params(db); 394 let params = db.generic_params(def);
390 if params.params.is_empty() { 395 if params.params.is_empty() {
391 self 396 self
392 } else { 397 } else {
@@ -394,7 +399,7 @@ impl Resolver {
394 } 399 }
395 } 400 }
396 401
397 pub(crate) fn push_impl_block_scope(self, impl_block: ImplBlock) -> Resolver { 402 pub(crate) fn push_impl_block_scope(self, impl_block: ImplId) -> Resolver {
398 self.push_scope(Scope::ImplBlockScope(impl_block)) 403 self.push_scope(Scope::ImplBlockScope(impl_block))
399 } 404 }
400 405
@@ -408,7 +413,7 @@ impl Resolver {
408 413
409 pub(crate) fn push_expr_scope( 414 pub(crate) fn push_expr_scope(
410 self, 415 self,
411 owner: DefWithBody, 416 owner: DefWithBodyId,
412 expr_scopes: Arc<ExprScopes>, 417 expr_scopes: Arc<ExprScopes>,
413 scope_id: ScopeId, 418 scope_id: ScopeId,
414 ) -> Resolver { 419 ) -> Resolver {
@@ -440,7 +445,7 @@ impl From<PerNs> for ScopeDef {
440} 445}
441 446
442impl Scope { 447impl Scope {
443 fn process_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) { 448 fn process_names(&self, db: &impl DefDatabase2, f: &mut dyn FnMut(Name, ScopeDef)) {
444 match self { 449 match self {
445 Scope::ModuleScope(m) => { 450 Scope::ModuleScope(m) => {
446 // FIXME: should we provide `self` here? 451 // FIXME: should we provide `self` here?
@@ -472,14 +477,14 @@ impl Scope {
472 } 477 }
473 } 478 }
474 Scope::ImplBlockScope(i) => { 479 Scope::ImplBlockScope(i) => {
475 f(name::SELF_TYPE, ScopeDef::ImplSelfType(*i)); 480 f(name::SELF_TYPE, ScopeDef::ImplSelfType((*i).into()));
476 } 481 }
477 Scope::AdtScope(i) => { 482 Scope::AdtScope(i) => {
478 f(name::SELF_TYPE, ScopeDef::AdtSelfType(*i)); 483 f(name::SELF_TYPE, ScopeDef::AdtSelfType((*i).into()));
479 } 484 }
480 Scope::ExprScope(scope) => { 485 Scope::ExprScope(scope) => {
481 scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| { 486 scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| {
482 let local = Local { parent: scope.owner, pat_id: e.pat() }; 487 let local = Local { parent: scope.owner.into(), pat_id: e.pat() };
483 f(e.name().clone(), ScopeDef::Local(local)); 488 f(e.name().clone(), ScopeDef::Local(local));
484 }); 489 });
485 } 490 }
@@ -501,7 +506,7 @@ impl HasResolver for Module {
501 506
502impl HasResolver for Trait { 507impl HasResolver for Trait {
503 fn resolver(self, db: &impl DefDatabase) -> Resolver { 508 fn resolver(self, db: &impl DefDatabase) -> Resolver {
504 self.module(db).resolver(db).push_generic_params_scope(db, self.into()) 509 self.module(db).resolver(db).push_generic_params_scope(db, self.id.into())
505 } 510 }
506} 511}
507 512
@@ -511,7 +516,7 @@ impl<T: Into<Adt>> HasResolver for T {
511 def.module(db) 516 def.module(db)
512 .resolver(db) 517 .resolver(db)
513 .push_generic_params_scope(db, def.into()) 518 .push_generic_params_scope(db, def.into())
514 .push_scope(Scope::AdtScope(def)) 519 .push_scope(Scope::AdtScope(def.into()))
515 } 520 }
516} 521}
517 522
@@ -520,7 +525,7 @@ impl HasResolver for Function {
520 self.container(db) 525 self.container(db)
521 .map(|c| c.resolver(db)) 526 .map(|c| c.resolver(db))
522 .unwrap_or_else(|| self.module(db).resolver(db)) 527 .unwrap_or_else(|| self.module(db).resolver(db))
523 .push_generic_params_scope(db, self.into()) 528 .push_generic_params_scope(db, self.id.into())
524 } 529 }
525} 530}
526 531
@@ -551,7 +556,7 @@ impl HasResolver for TypeAlias {
551 self.container(db) 556 self.container(db)
552 .map(|ib| ib.resolver(db)) 557 .map(|ib| ib.resolver(db))
553 .unwrap_or_else(|| self.module(db).resolver(db)) 558 .unwrap_or_else(|| self.module(db).resolver(db))
554 .push_generic_params_scope(db, self.into()) 559 .push_generic_params_scope(db, self.id.into())
555 } 560 }
556} 561}
557 562
@@ -582,7 +587,7 @@ impl HasResolver for ImplBlock {
582 fn resolver(self, db: &impl DefDatabase) -> Resolver { 587 fn resolver(self, db: &impl DefDatabase) -> Resolver {
583 self.module(db) 588 self.module(db)
584 .resolver(db) 589 .resolver(db)
585 .push_generic_params_scope(db, self.into()) 590 .push_generic_params_scope(db, self.id.into())
586 .push_impl_block_scope(self) 591 .push_impl_block_scope(self.id)
587 } 592 }
588} 593}
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 727310f06..5abb8d693 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -25,8 +25,9 @@ use crate::{
25 ids::LocationCtx, 25 ids::LocationCtx,
26 resolve::{HasResolver, ScopeDef, TypeNs, ValueNs}, 26 resolve::{HasResolver, ScopeDef, TypeNs, ValueNs},
27 ty::method_resolution::{self, implements_trait}, 27 ty::method_resolution::{self, implements_trait},
28 AssocItem, Const, DefWithBody, Either, Enum, FromSource, Function, GenericParam, HasBody, 28 Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
29 HirFileId, Local, MacroDef, Module, Name, Path, Resolver, Static, Struct, Ty, 29 GenericParam, HasBody, HirFileId, Local, MacroDef, Module, Name, Path, Resolver, Static,
30 Struct, Trait, Ty, TypeAlias,
30}; 31};
31 32
32fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> { 33fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> {
@@ -240,16 +241,18 @@ impl SourceAnalyzer {
240 path: &crate::Path, 241 path: &crate::Path,
241 ) -> Option<PathResolution> { 242 ) -> Option<PathResolution> {
242 let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty { 243 let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty {
243 TypeNs::SelfType(it) => PathResolution::SelfType(it), 244 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
244 TypeNs::GenericParam(idx) => PathResolution::GenericParam(GenericParam { 245 TypeNs::GenericParam(idx) => PathResolution::GenericParam(GenericParam {
245 parent: self.resolver.generic_def().unwrap(), 246 parent: self.resolver.generic_def().unwrap().into(),
246 idx, 247 idx,
247 }), 248 }),
248 TypeNs::AdtSelfType(it) | TypeNs::Adt(it) => PathResolution::Def(it.into()), 249 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
249 TypeNs::EnumVariant(it) => PathResolution::Def(it.into()), 250 PathResolution::Def(Adt::from(it).into())
250 TypeNs::TypeAlias(it) => PathResolution::Def(it.into()), 251 }
252 TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
253 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
251 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), 254 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
252 TypeNs::Trait(it) => PathResolution::Def(it.into()), 255 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
253 }); 256 });
254 let values = self.resolver.resolve_path_in_value_ns_fully(db, &path).and_then(|val| { 257 let values = self.resolver.resolve_path_in_value_ns_fully(db, &path).and_then(|val| {
255 let res = match val { 258 let res = match val {
@@ -392,7 +395,7 @@ impl SourceAnalyzer {
392 let std_future_path = known::std_future_future(); 395 let std_future_path = known::std_future_future();
393 396
394 let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) { 397 let std_future_trait = match self.resolver.resolve_known_trait(db, &std_future_path) {
395 Some(it) => it, 398 Some(it) => it.into(),
396 _ => return false, 399 _ => return false,
397 }; 400 };
398 401
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index 7f9e81d64..684d66946 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -24,6 +24,7 @@ use rustc_hash::FxHashMap;
24use hir_def::{ 24use hir_def::{
25 path::known, 25 path::known,
26 type_ref::{Mutability, TypeRef}, 26 type_ref::{Mutability, TypeRef},
27 AdtId,
27}; 28};
28use hir_expand::{diagnostics::DiagnosticSink, name}; 29use hir_expand::{diagnostics::DiagnosticSink, name};
29use ra_arena::map::ArenaMap; 30use ra_arena::map::ArenaMap;
@@ -43,7 +44,7 @@ use crate::{
43 resolve::{HasResolver, Resolver, TypeNs}, 44 resolve::{HasResolver, Resolver, TypeNs},
44 ty::infer::diagnostics::InferenceDiagnostic, 45 ty::infer::diagnostics::InferenceDiagnostic,
45 Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path, 46 Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, HasBody, IntTy, Path,
46 StructField, VariantDef, 47 StructField, Trait, VariantDef,
47}; 48};
48 49
49macro_rules! ty_app { 50macro_rules! ty_app {
@@ -518,17 +519,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
518 // FIXME: this should resolve assoc items as well, see this example: 519 // FIXME: this should resolve assoc items as well, see this example:
519 // https://play.rust-lang.org/?gist=087992e9e22495446c01c0d4e2d69521 520 // https://play.rust-lang.org/?gist=087992e9e22495446c01c0d4e2d69521
520 match resolver.resolve_path_in_type_ns_fully(self.db, &path) { 521 match resolver.resolve_path_in_type_ns_fully(self.db, &path) {
521 Some(TypeNs::Adt(Adt::Struct(it))) => it.into(), 522 Some(TypeNs::AdtId(AdtId::StructId(it))) => it.into(),
522 Some(TypeNs::Adt(Adt::Union(it))) => it.into(), 523 Some(TypeNs::AdtId(AdtId::UnionId(it))) => it.into(),
523 Some(TypeNs::AdtSelfType(adt)) => adt.into(), 524 Some(TypeNs::AdtSelfType(adt)) => adt.into(),
524 Some(TypeNs::EnumVariant(it)) => it.into(), 525 Some(TypeNs::EnumVariantId(it)) => it.into(),
525 Some(TypeNs::TypeAlias(it)) => it.into(), 526 Some(TypeNs::TypeAliasId(it)) => it.into(),
526 527
527 Some(TypeNs::SelfType(_)) | 528 Some(TypeNs::SelfType(_)) |
528 Some(TypeNs::GenericParam(_)) | 529 Some(TypeNs::GenericParam(_)) |
529 Some(TypeNs::BuiltinType(_)) | 530 Some(TypeNs::BuiltinType(_)) |
530 Some(TypeNs::Trait(_)) | 531 Some(TypeNs::TraitId(_)) |
531 Some(TypeNs::Adt(Adt::Enum(_))) | 532 Some(TypeNs::AdtId(AdtId::EnumId(_))) |
532 None => { 533 None => {
533 return (Ty::Unknown, None) 534 return (Ty::Unknown, None)
534 } 535 }
@@ -576,26 +577,26 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
576 577
577 fn resolve_into_iter_item(&self) -> Option<TypeAlias> { 578 fn resolve_into_iter_item(&self) -> Option<TypeAlias> {
578 let path = known::std_iter_into_iterator(); 579 let path = known::std_iter_into_iterator();
579 let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; 580 let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
580 trait_.associated_type_by_name(self.db, &name::ITEM_TYPE) 581 trait_.associated_type_by_name(self.db, &name::ITEM_TYPE)
581 } 582 }
582 583
583 fn resolve_ops_try_ok(&self) -> Option<TypeAlias> { 584 fn resolve_ops_try_ok(&self) -> Option<TypeAlias> {
584 let path = known::std_ops_try(); 585 let path = known::std_ops_try();
585 let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; 586 let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
586 trait_.associated_type_by_name(self.db, &name::OK_TYPE) 587 trait_.associated_type_by_name(self.db, &name::OK_TYPE)
587 } 588 }
588 589
589 fn resolve_future_future_output(&self) -> Option<TypeAlias> { 590 fn resolve_future_future_output(&self) -> Option<TypeAlias> {
590 let path = known::std_future_future(); 591 let path = known::std_future_future();
591 let trait_ = self.resolver.resolve_known_trait(self.db, &path)?; 592 let trait_: Trait = self.resolver.resolve_known_trait(self.db, &path)?.into();
592 trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE) 593 trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE)
593 } 594 }
594 595
595 fn resolve_boxed_box(&self) -> Option<Adt> { 596 fn resolve_boxed_box(&self) -> Option<Adt> {
596 let path = known::std_boxed_box(); 597 let path = known::std_boxed_box();
597 let struct_ = self.resolver.resolve_known_struct(self.db, &path)?; 598 let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
598 Some(Adt::Struct(struct_)) 599 Some(Adt::Struct(struct_.into()))
599 } 600 }
600} 601}
601 602
diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs
index 31ca675aa..f36a27929 100644
--- a/crates/ra_hir/src/ty/infer/path.rs
+++ b/crates/ra_hir/src/ty/infer/path.rs
@@ -94,13 +94,13 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
94 let is_before_last = remaining_segments.len() == 1; 94 let is_before_last = remaining_segments.len() == 1;
95 95
96 match (def, is_before_last) { 96 match (def, is_before_last) {
97 (TypeNs::Trait(trait_), true) => { 97 (TypeNs::TraitId(trait_), true) => {
98 let segment = 98 let segment =
99 remaining_segments.last().expect("there should be at least one segment here"); 99 remaining_segments.last().expect("there should be at least one segment here");
100 let trait_ref = TraitRef::from_resolved_path( 100 let trait_ref = TraitRef::from_resolved_path(
101 self.db, 101 self.db,
102 &self.resolver, 102 &self.resolver,
103 trait_, 103 trait_.into(),
104 resolved_segment, 104 resolved_segment,
105 None, 105 None,
106 ); 106 );
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index 397ee7d5f..d4fbddac0 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -28,8 +28,8 @@ use crate::{
28 Adt, 28 Adt,
29 }, 29 },
30 util::make_mut_slice, 30 util::make_mut_slice,
31 Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, Trait, 31 Const, Enum, EnumVariant, Function, ImplBlock, ModuleDef, Path, Static, Struct, StructField,
32 TypeAlias, Union, VariantDef, 32 Trait, TypeAlias, Union, VariantDef,
33}; 33};
34 34
35// FIXME: this is only really used in `type_for_def`, which contains a bunch of 35// FIXME: this is only really used in `type_for_def`, which contains a bunch of
@@ -156,9 +156,14 @@ impl Ty {
156 remaining_segments: &[PathSegment], 156 remaining_segments: &[PathSegment],
157 ) -> Ty { 157 ) -> Ty {
158 let ty = match resolution { 158 let ty = match resolution {
159 TypeNs::Trait(trait_) => { 159 TypeNs::TraitId(trait_) => {
160 let trait_ref = 160 let trait_ref = TraitRef::from_resolved_path(
161 TraitRef::from_resolved_path(db, resolver, trait_, resolved_segment, None); 161 db,
162 resolver,
163 trait_.into(),
164 resolved_segment,
165 None,
166 );
162 return if remaining_segments.len() == 1 { 167 return if remaining_segments.len() == 1 {
163 let segment = &remaining_segments[0]; 168 let segment = &remaining_segments[0];
164 match trait_ref 169 match trait_ref
@@ -189,18 +194,18 @@ impl Ty {
189 let name = resolved_segment.name.clone(); 194 let name = resolved_segment.name.clone();
190 Ty::Param { idx, name } 195 Ty::Param { idx, name }
191 } 196 }
192 TypeNs::SelfType(impl_block) => impl_block.target_ty(db), 197 TypeNs::SelfType(impl_block) => ImplBlock::from(impl_block).target_ty(db),
193 TypeNs::AdtSelfType(adt) => adt.ty(db), 198 TypeNs::AdtSelfType(adt) => Adt::from(adt).ty(db),
194 199
195 TypeNs::Adt(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()), 200 TypeNs::AdtId(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()),
196 TypeNs::BuiltinType(it) => { 201 TypeNs::BuiltinType(it) => {
197 Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()) 202 Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into())
198 } 203 }
199 TypeNs::TypeAlias(it) => { 204 TypeNs::TypeAliasId(it) => {
200 Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()) 205 Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into())
201 } 206 }
202 // FIXME: report error 207 // FIXME: report error
203 TypeNs::EnumVariant(_) => return Ty::Unknown, 208 TypeNs::EnumVariantId(_) => return Ty::Unknown,
204 }; 209 };
205 210
206 Ty::from_type_relative_path(db, resolver, ty, remaining_segments) 211 Ty::from_type_relative_path(db, resolver, ty, remaining_segments)
@@ -247,7 +252,7 @@ impl Ty {
247 Some(def) => def, 252 Some(def) => def,
248 None => return Ty::Unknown, // this can't actually happen 253 None => return Ty::Unknown, // this can't actually happen
249 }; 254 };
250 let predicates = db.generic_predicates_for_param(def, param_idx); 255 let predicates = db.generic_predicates_for_param(def.into(), param_idx);
251 let traits_from_env = predicates.iter().filter_map(|pred| match pred { 256 let traits_from_env = predicates.iter().filter_map(|pred| match pred {
252 GenericPredicate::Implemented(tr) if tr.self_ty() == &self_ty => Some(tr.trait_), 257 GenericPredicate::Implemented(tr) if tr.self_ty() == &self_ty => Some(tr.trait_),
253 _ => None, 258 _ => None,
@@ -391,11 +396,11 @@ impl TraitRef {
391 explicit_self_ty: Option<Ty>, 396 explicit_self_ty: Option<Ty>,
392 ) -> Option<Self> { 397 ) -> Option<Self> {
393 let resolved = match resolver.resolve_path_in_type_ns_fully(db, &path)? { 398 let resolved = match resolver.resolve_path_in_type_ns_fully(db, &path)? {
394 TypeNs::Trait(tr) => tr, 399 TypeNs::TraitId(tr) => tr,
395 _ => return None, 400 _ => return None,
396 }; 401 };
397 let segment = path.segments.last().expect("path should have at least one segment"); 402 let segment = path.segments.last().expect("path should have at least one segment");
398 Some(TraitRef::from_resolved_path(db, resolver, resolved, segment, explicit_self_ty)) 403 Some(TraitRef::from_resolved_path(db, resolver, resolved.into(), segment, explicit_self_ty))
399 } 404 }
400 405
401 pub(super) fn from_resolved_path( 406 pub(super) fn from_resolved_path(
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index f377fca48..26dd06171 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -224,7 +224,9 @@ fn iterate_trait_method_candidates<T>(
224 .trait_predicates_for_self_ty(&ty.value) 224 .trait_predicates_for_self_ty(&ty.value)
225 .map(|tr| tr.trait_) 225 .map(|tr| tr.trait_)
226 .flat_map(|t| t.all_super_traits(db)); 226 .flat_map(|t| t.all_super_traits(db));
227 let traits = inherent_trait.chain(traits_from_env).chain(resolver.traits_in_scope(db)); 227 let traits = inherent_trait
228 .chain(traits_from_env)
229 .chain(resolver.traits_in_scope(db).into_iter().map(Trait::from));
228 'traits: for t in traits { 230 'traits: for t in traits {
229 let data = t.trait_data(db); 231 let data = t.trait_data(db);
230 232
diff --git a/crates/ra_hir_def/src/traits.rs b/crates/ra_hir_def/src/traits.rs
index 877d73d66..6c2d5b2a9 100644
--- a/crates/ra_hir_def/src/traits.rs
+++ b/crates/ra_hir_def/src/traits.rs
@@ -11,7 +11,7 @@ use ra_syntax::ast::{self, NameOwner};
11 11
12use crate::{ 12use crate::{
13 db::DefDatabase2, AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionLoc, Intern, TraitId, 13 db::DefDatabase2, AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionLoc, Intern, TraitId,
14 TypeAliasLoc, 14 TypeAliasId, TypeAliasLoc,
15}; 15};
16 16
17#[derive(Debug, Clone, PartialEq, Eq)] 17#[derive(Debug, Clone, PartialEq, Eq)]
@@ -56,4 +56,11 @@ impl TraitData {
56 }; 56 };
57 Arc::new(TraitData { name, items, auto }) 57 Arc::new(TraitData { name, items, auto })
58 } 58 }
59
60 pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
61 self.items.iter().filter_map(|item| match item {
62 AssocItemId::TypeAliasId(t) => Some(*t),
63 _ => None,
64 })
65 }
59} 66}