aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/lower.rs
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-12-23 14:35:31 +0000
committerSeivan Heidari <[email protected]>2019-12-23 14:35:31 +0000
commitb21d9337d9200e2cfdc90b386591c72c302dc03e (patch)
treef81f5c08f821115cee26fa4d3ceaae88c7807fd5 /crates/ra_hir_ty/src/lower.rs
parent18a0937585b836ec5ed054b9ae48e0156ab6d9ef (diff)
parentce07a2daa9e53aa86a769f8641b14c2878444fbc (diff)
Merge branch 'master' into feature/themes
Diffstat (limited to 'crates/ra_hir_ty/src/lower.rs')
-rw-r--r--crates/ra_hir_ty/src/lower.rs174
1 files changed, 98 insertions, 76 deletions
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs
index 091c60f4f..af3db2e1d 100644
--- a/crates/ra_hir_ty/src/lower.rs
+++ b/crates/ra_hir_ty/src/lower.rs
@@ -11,10 +11,10 @@ use std::sync::Arc;
11use hir_def::{ 11use hir_def::{
12 builtin_type::BuiltinType, 12 builtin_type::BuiltinType,
13 generics::WherePredicate, 13 generics::WherePredicate,
14 path::{GenericArg, Path, PathKind, PathSegment}, 14 path::{GenericArg, Path, PathSegment, PathSegments},
15 resolver::{HasResolver, Resolver, TypeNs}, 15 resolver::{HasResolver, Resolver, TypeNs},
16 type_ref::{TypeBound, TypeRef}, 16 type_ref::{TypeBound, TypeRef},
17 AdtId, AstItemDef, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, 17 AdtId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId,
18 LocalStructFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, UnionId, VariantId, 18 LocalStructFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, UnionId, VariantId,
19}; 19};
20use ra_arena::map::ArenaMap; 20use ra_arena::map::ArenaMap;
@@ -24,11 +24,11 @@ use crate::{
24 db::HirDatabase, 24 db::HirDatabase,
25 primitive::{FloatTy, IntTy}, 25 primitive::{FloatTy, IntTy},
26 utils::{ 26 utils::{
27 all_super_traits, associated_type_by_name_including_super_traits, make_mut_slice, 27 all_super_traits, associated_type_by_name_including_super_traits, generics, make_mut_slice,
28 variant_data, 28 variant_data,
29 }, 29 },
30 FnSig, GenericPredicate, ImplTy, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, 30 FnSig, GenericPredicate, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef,
31 TraitRef, Ty, TypeCtor, TypeWalk, 31 Ty, TypeCtor, TypeWalk,
32}; 32};
33 33
34impl Ty { 34impl Ty {
@@ -101,17 +101,19 @@ impl Ty {
101 TypeRef::Path(path) => path, 101 TypeRef::Path(path) => path,
102 _ => return None, 102 _ => return None,
103 }; 103 };
104 if let PathKind::Type(_) = &path.kind { 104 if path.type_anchor().is_some() {
105 return None; 105 return None;
106 } 106 }
107 if path.segments.len() > 1 { 107 if path.segments().len() > 1 {
108 return None; 108 return None;
109 } 109 }
110 let resolution = match resolver.resolve_path_in_type_ns(db, path) { 110 let resolution = match resolver.resolve_path_in_type_ns(db, path.mod_path()) {
111 Some((it, None)) => it, 111 Some((it, None)) => it,
112 _ => return None, 112 _ => return None,
113 }; 113 };
114 if let TypeNs::GenericParam(idx) = resolution { 114 if let TypeNs::GenericParam(param_id) = resolution {
115 let generics = generics(db, resolver.generic_def().expect("generics in scope"));
116 let idx = generics.param_idx(param_id);
115 Some(idx) 117 Some(idx)
116 } else { 118 } else {
117 None 119 None
@@ -122,11 +124,11 @@ impl Ty {
122 db: &impl HirDatabase, 124 db: &impl HirDatabase,
123 resolver: &Resolver, 125 resolver: &Resolver,
124 ty: Ty, 126 ty: Ty,
125 remaining_segments: &[PathSegment], 127 remaining_segments: PathSegments<'_>,
126 ) -> Ty { 128 ) -> Ty {
127 if remaining_segments.len() == 1 { 129 if remaining_segments.len() == 1 {
128 // resolve unselected assoc types 130 // resolve unselected assoc types
129 let segment = &remaining_segments[0]; 131 let segment = remaining_segments.first().unwrap();
130 Ty::select_associated_type(db, resolver, ty, segment) 132 Ty::select_associated_type(db, resolver, ty, segment)
131 } else if remaining_segments.len() > 1 { 133 } else if remaining_segments.len() > 1 {
132 // FIXME report error (ambiguous associated type) 134 // FIXME report error (ambiguous associated type)
@@ -140,15 +142,15 @@ impl Ty {
140 db: &impl HirDatabase, 142 db: &impl HirDatabase,
141 resolver: &Resolver, 143 resolver: &Resolver,
142 resolution: TypeNs, 144 resolution: TypeNs,
143 resolved_segment: &PathSegment, 145 resolved_segment: PathSegment<'_>,
144 remaining_segments: &[PathSegment], 146 remaining_segments: PathSegments<'_>,
145 ) -> Ty { 147 ) -> Ty {
146 let ty = match resolution { 148 let ty = match resolution {
147 TypeNs::TraitId(trait_) => { 149 TypeNs::TraitId(trait_) => {
148 let trait_ref = 150 let trait_ref =
149 TraitRef::from_resolved_path(db, resolver, trait_, resolved_segment, None); 151 TraitRef::from_resolved_path(db, resolver, trait_, resolved_segment, None);
150 return if remaining_segments.len() == 1 { 152 return if remaining_segments.len() == 1 {
151 let segment = &remaining_segments[0]; 153 let segment = remaining_segments.first().unwrap();
152 let associated_ty = associated_type_by_name_including_super_traits( 154 let associated_ty = associated_type_by_name_including_super_traits(
153 db, 155 db,
154 trait_ref.trait_, 156 trait_ref.trait_,
@@ -174,12 +176,14 @@ impl Ty {
174 Ty::Dyn(Arc::new([GenericPredicate::Implemented(trait_ref)])) 176 Ty::Dyn(Arc::new([GenericPredicate::Implemented(trait_ref)]))
175 }; 177 };
176 } 178 }
177 TypeNs::GenericParam(idx) => { 179 TypeNs::GenericParam(param_id) => {
180 let generics = generics(db, resolver.generic_def().expect("generics in scope"));
181 let idx = generics.param_idx(param_id);
178 // FIXME: maybe return name in resolution? 182 // FIXME: maybe return name in resolution?
179 let name = resolved_segment.name.clone(); 183 let name = generics.param_name(param_id);
180 Ty::Param { idx, name } 184 Ty::Param { idx, name }
181 } 185 }
182 TypeNs::SelfType(impl_id) => db.impl_ty(impl_id).self_type().clone(), 186 TypeNs::SelfType(impl_id) => db.impl_self_ty(impl_id).clone(),
183 TypeNs::AdtSelfType(adt) => db.ty(adt.into()), 187 TypeNs::AdtSelfType(adt) => db.ty(adt.into()),
184 188
185 TypeNs::AdtId(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()), 189 TypeNs::AdtId(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()),
@@ -198,21 +202,21 @@ impl Ty {
198 202
199 pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Ty { 203 pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Ty {
200 // Resolve the path (in type namespace) 204 // Resolve the path (in type namespace)
201 if let PathKind::Type(type_ref) = &path.kind { 205 if let Some(type_ref) = path.type_anchor() {
202 let ty = Ty::from_hir(db, resolver, &type_ref); 206 let ty = Ty::from_hir(db, resolver, &type_ref);
203 let remaining_segments = &path.segments[..]; 207 return Ty::from_type_relative_path(db, resolver, ty, path.segments());
204 return Ty::from_type_relative_path(db, resolver, ty, remaining_segments);
205 } 208 }
206 let (resolution, remaining_index) = match resolver.resolve_path_in_type_ns(db, path) { 209 let (resolution, remaining_index) =
207 Some(it) => it, 210 match resolver.resolve_path_in_type_ns(db, path.mod_path()) {
208 None => return Ty::Unknown, 211 Some(it) => it,
209 }; 212 None => return Ty::Unknown,
213 };
210 let (resolved_segment, remaining_segments) = match remaining_index { 214 let (resolved_segment, remaining_segments) = match remaining_index {
211 None => ( 215 None => (
212 path.segments.last().expect("resolved path has at least one element"), 216 path.segments().last().expect("resolved path has at least one element"),
213 &[] as &[PathSegment], 217 PathSegments::EMPTY,
214 ), 218 ),
215 Some(i) => (&path.segments[i - 1], &path.segments[i..]), 219 Some(i) => (path.segments().get(i - 1).unwrap(), path.segments().skip(i)),
216 }; 220 };
217 Ty::from_partly_resolved_hir_path( 221 Ty::from_partly_resolved_hir_path(
218 db, 222 db,
@@ -227,7 +231,7 @@ impl Ty {
227 db: &impl HirDatabase, 231 db: &impl HirDatabase,
228 resolver: &Resolver, 232 resolver: &Resolver,
229 self_ty: Ty, 233 self_ty: Ty,
230 segment: &PathSegment, 234 segment: PathSegment<'_>,
231 ) -> Ty { 235 ) -> Ty {
232 let param_idx = match self_ty { 236 let param_idx = match self_ty {
233 Ty::Param { idx, .. } => idx, 237 Ty::Param { idx, .. } => idx,
@@ -257,7 +261,7 @@ impl Ty {
257 fn from_hir_path_inner( 261 fn from_hir_path_inner(
258 db: &impl HirDatabase, 262 db: &impl HirDatabase,
259 resolver: &Resolver, 263 resolver: &Resolver,
260 segment: &PathSegment, 264 segment: PathSegment<'_>,
261 typable: TyDefId, 265 typable: TyDefId,
262 ) -> Ty { 266 ) -> Ty {
263 let generic_def = match typable { 267 let generic_def = match typable {
@@ -280,7 +284,7 @@ impl Ty {
280 // special-case enum variants 284 // special-case enum variants
281 resolved: ValueTyDefId, 285 resolved: ValueTyDefId,
282 ) -> Substs { 286 ) -> Substs {
283 let last = path.segments.last().expect("path should have at least one segment"); 287 let last = path.segments().last().expect("path should have at least one segment");
284 let (segment, generic_def) = match resolved { 288 let (segment, generic_def) = match resolved {
285 ValueTyDefId::FunctionId(it) => (last, Some(it.into())), 289 ValueTyDefId::FunctionId(it) => (last, Some(it.into())),
286 ValueTyDefId::StructId(it) => (last, Some(it.into())), 290 ValueTyDefId::StructId(it) => (last, Some(it.into())),
@@ -292,13 +296,11 @@ impl Ty {
292 // referring to the variant. So `Option::<T>::None` and 296 // referring to the variant. So `Option::<T>::None` and
293 // `Option::None::<T>` are both allowed (though the former is 297 // `Option::None::<T>` are both allowed (though the former is
294 // preferred). See also `def_ids_for_path_segments` in rustc. 298 // preferred). See also `def_ids_for_path_segments` in rustc.
295 let len = path.segments.len(); 299 let len = path.segments().len();
296 let segment = if len >= 2 && path.segments[len - 2].args_and_bindings.is_some() { 300 let penultimate = if len >= 2 { path.segments().get(len - 2) } else { None };
297 // Option::<T>::None 301 let segment = match penultimate {
298 &path.segments[len - 2] 302 Some(segment) if segment.args_and_bindings.is_some() => segment,
299 } else { 303 _ => last,
300 // Option::None::<T>
301 last
302 }; 304 };
303 (segment, Some(var.parent.into())) 305 (segment, Some(var.parent.into()))
304 } 306 }
@@ -310,16 +312,15 @@ impl Ty {
310pub(super) fn substs_from_path_segment( 312pub(super) fn substs_from_path_segment(
311 db: &impl HirDatabase, 313 db: &impl HirDatabase,
312 resolver: &Resolver, 314 resolver: &Resolver,
313 segment: &PathSegment, 315 segment: PathSegment<'_>,
314 def_generic: Option<GenericDefId>, 316 def_generic: Option<GenericDefId>,
315 add_self_param: bool, 317 add_self_param: bool,
316) -> Substs { 318) -> Substs {
317 let mut substs = Vec::new(); 319 let mut substs = Vec::new();
318 let def_generics = def_generic.map(|def| db.generic_params(def.into())); 320 let def_generics = def_generic.map(|def| generics(db, def.into()));
319 321
320 let (parent_param_count, param_count) = 322 let (total_len, parent_len, child_len) = def_generics.map_or((0, 0, 0), |g| g.len_split());
321 def_generics.map_or((0, 0), |g| (g.count_parent_params(), g.params.len())); 323 substs.extend(iter::repeat(Ty::Unknown).take(parent_len));
322 substs.extend(iter::repeat(Ty::Unknown).take(parent_param_count));
323 if add_self_param { 324 if add_self_param {
324 // FIXME this add_self_param argument is kind of a hack: Traits have the 325 // FIXME this add_self_param argument is kind of a hack: Traits have the
325 // Self type as an implicit first type parameter, but it can't be 326 // Self type as an implicit first type parameter, but it can't be
@@ -330,8 +331,8 @@ pub(super) fn substs_from_path_segment(
330 if let Some(generic_args) = &segment.args_and_bindings { 331 if let Some(generic_args) = &segment.args_and_bindings {
331 // if args are provided, it should be all of them, but we can't rely on that 332 // if args are provided, it should be all of them, but we can't rely on that
332 let self_param_correction = if add_self_param { 1 } else { 0 }; 333 let self_param_correction = if add_self_param { 1 } else { 0 };
333 let param_count = param_count - self_param_correction; 334 let child_len = child_len + self_param_correction;
334 for arg in generic_args.args.iter().take(param_count) { 335 for arg in generic_args.args.iter().take(child_len) {
335 match arg { 336 match arg {
336 GenericArg::Type(type_ref) => { 337 GenericArg::Type(type_ref) => {
337 let ty = Ty::from_hir(db, resolver, type_ref); 338 let ty = Ty::from_hir(db, resolver, type_ref);
@@ -342,10 +343,10 @@ pub(super) fn substs_from_path_segment(
342 } 343 }
343 // add placeholders for args that were not provided 344 // add placeholders for args that were not provided
344 let supplied_params = substs.len(); 345 let supplied_params = substs.len();
345 for _ in supplied_params..parent_param_count + param_count { 346 for _ in supplied_params..total_len {
346 substs.push(Ty::Unknown); 347 substs.push(Ty::Unknown);
347 } 348 }
348 assert_eq!(substs.len(), parent_param_count + param_count); 349 assert_eq!(substs.len(), total_len);
349 350
350 // handle defaults 351 // handle defaults
351 if let Some(def_generic) = def_generic { 352 if let Some(def_generic) = def_generic {
@@ -369,11 +370,11 @@ impl TraitRef {
369 path: &Path, 370 path: &Path,
370 explicit_self_ty: Option<Ty>, 371 explicit_self_ty: Option<Ty>,
371 ) -> Option<Self> { 372 ) -> Option<Self> {
372 let resolved = match resolver.resolve_path_in_type_ns_fully(db, &path)? { 373 let resolved = match resolver.resolve_path_in_type_ns_fully(db, path.mod_path())? {
373 TypeNs::TraitId(tr) => tr, 374 TypeNs::TraitId(tr) => tr,
374 _ => return None, 375 _ => return None,
375 }; 376 };
376 let segment = path.segments.last().expect("path should have at least one segment"); 377 let segment = path.segments().last().expect("path should have at least one segment");
377 Some(TraitRef::from_resolved_path(db, resolver, resolved.into(), segment, explicit_self_ty)) 378 Some(TraitRef::from_resolved_path(db, resolver, resolved.into(), segment, explicit_self_ty))
378 } 379 }
379 380
@@ -381,7 +382,7 @@ impl TraitRef {
381 db: &impl HirDatabase, 382 db: &impl HirDatabase,
382 resolver: &Resolver, 383 resolver: &Resolver,
383 resolved: TraitId, 384 resolved: TraitId,
384 segment: &PathSegment, 385 segment: PathSegment<'_>,
385 explicit_self_ty: Option<Ty>, 386 explicit_self_ty: Option<Ty>,
386 ) -> Self { 387 ) -> Self {
387 let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved); 388 let mut substs = TraitRef::substs_from_path(db, resolver, segment, resolved);
@@ -407,7 +408,7 @@ impl TraitRef {
407 fn substs_from_path( 408 fn substs_from_path(
408 db: &impl HirDatabase, 409 db: &impl HirDatabase,
409 resolver: &Resolver, 410 resolver: &Resolver,
410 segment: &PathSegment, 411 segment: PathSegment<'_>,
411 resolved: TraitId, 412 resolved: TraitId,
412 ) -> Substs { 413 ) -> Substs {
413 let has_self_param = 414 let has_self_param =
@@ -461,12 +462,12 @@ fn assoc_type_bindings_from_type_bound<'a>(
461 trait_ref: TraitRef, 462 trait_ref: TraitRef,
462) -> impl Iterator<Item = GenericPredicate> + 'a { 463) -> impl Iterator<Item = GenericPredicate> + 'a {
463 let last_segment = match bound { 464 let last_segment = match bound {
464 TypeBound::Path(path) => path.segments.last(), 465 TypeBound::Path(path) => path.segments().last(),
465 TypeBound::Error => None, 466 TypeBound::Error => None,
466 }; 467 };
467 last_segment 468 last_segment
468 .into_iter() 469 .into_iter()
469 .flat_map(|segment| segment.args_and_bindings.iter()) 470 .flat_map(|segment| segment.args_and_bindings.into_iter())
470 .flat_map(|args_and_bindings| args_and_bindings.bindings.iter()) 471 .flat_map(|args_and_bindings| args_and_bindings.bindings.iter())
471 .map(move |(name, type_ref)| { 472 .map(move |(name, type_ref)| {
472 let associated_ty = 473 let associated_ty =
@@ -532,6 +533,15 @@ pub(crate) fn generic_predicates_for_param_query(
532 .collect() 533 .collect()
533} 534}
534 535
536pub(crate) fn generic_predicates_for_param_recover(
537 _db: &impl HirDatabase,
538 _cycle: &[String],
539 _def: &GenericDefId,
540 _param_idx: &u32,
541) -> Arc<[GenericPredicate]> {
542 Arc::new([])
543}
544
535impl TraitEnvironment { 545impl TraitEnvironment {
536 pub fn lower(db: &impl HirDatabase, resolver: &Resolver) -> Arc<TraitEnvironment> { 546 pub fn lower(db: &impl HirDatabase, resolver: &Resolver) -> Arc<TraitEnvironment> {
537 let predicates = resolver 547 let predicates = resolver
@@ -558,12 +568,11 @@ pub(crate) fn generic_predicates_query(
558/// Resolve the default type params from generics 568/// Resolve the default type params from generics
559pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDefId) -> Substs { 569pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDefId) -> Substs {
560 let resolver = def.resolver(db); 570 let resolver = def.resolver(db);
561 let generic_params = db.generic_params(def.into()); 571 let generic_params = generics(db, def.into());
562 572
563 let defaults = generic_params 573 let defaults = generic_params
564 .params_including_parent() 574 .iter()
565 .into_iter() 575 .map(|(_idx, p)| p.default.as_ref().map_or(Ty::Unknown, |t| Ty::from_hir(db, &resolver, t)))
566 .map(|p| p.default.as_ref().map_or(Ty::Unknown, |t| Ty::from_hir(db, &resolver, t)))
567 .collect(); 576 .collect();
568 577
569 Substs(defaults) 578 Substs(defaults)
@@ -580,7 +589,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: FunctionId) -> FnSig {
580/// Build the declared type of a function. This should not need to look at the 589/// Build the declared type of a function. This should not need to look at the
581/// function body. 590/// function body.
582fn type_for_fn(db: &impl HirDatabase, def: FunctionId) -> Ty { 591fn type_for_fn(db: &impl HirDatabase, def: FunctionId) -> Ty {
583 let generics = db.generic_params(def.into()); 592 let generics = generics(db, def.into());
584 let substs = Substs::identity(&generics); 593 let substs = Substs::identity(&generics);
585 Ty::apply(TypeCtor::FnDef(def.into()), substs) 594 Ty::apply(TypeCtor::FnDef(def.into()), substs)
586} 595}
@@ -630,7 +639,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> Ty {
630 if struct_data.variant_data.is_unit() { 639 if struct_data.variant_data.is_unit() {
631 return type_for_adt(db, def.into()); // Unit struct 640 return type_for_adt(db, def.into()); // Unit struct
632 } 641 }
633 let generics = db.generic_params(def.into()); 642 let generics = generics(db, def.into());
634 let substs = Substs::identity(&generics); 643 let substs = Substs::identity(&generics);
635 Ty::apply(TypeCtor::FnDef(def.into()), substs) 644 Ty::apply(TypeCtor::FnDef(def.into()), substs)
636} 645}
@@ -644,7 +653,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId
644 .iter() 653 .iter()
645 .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) 654 .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref))
646 .collect::<Vec<_>>(); 655 .collect::<Vec<_>>();
647 let generics = db.generic_params(def.parent.into()); 656 let generics = generics(db, def.parent.into());
648 let substs = Substs::identity(&generics); 657 let substs = Substs::identity(&generics);
649 let ret = type_for_adt(db, def.parent.into()).subst(&substs); 658 let ret = type_for_adt(db, def.parent.into()).subst(&substs);
650 FnSig::from_params_and_return(params, ret) 659 FnSig::from_params_and_return(params, ret)
@@ -657,18 +666,18 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId)
657 if var_data.is_unit() { 666 if var_data.is_unit() {
658 return type_for_adt(db, def.parent.into()); // Unit variant 667 return type_for_adt(db, def.parent.into()); // Unit variant
659 } 668 }
660 let generics = db.generic_params(def.parent.into()); 669 let generics = generics(db, def.parent.into());
661 let substs = Substs::identity(&generics); 670 let substs = Substs::identity(&generics);
662 Ty::apply(TypeCtor::FnDef(EnumVariantId::from(def).into()), substs) 671 Ty::apply(TypeCtor::FnDef(EnumVariantId::from(def).into()), substs)
663} 672}
664 673
665fn type_for_adt(db: &impl HirDatabase, adt: AdtId) -> Ty { 674fn type_for_adt(db: &impl HirDatabase, adt: AdtId) -> Ty {
666 let generics = db.generic_params(adt.into()); 675 let generics = generics(db, adt.into());
667 Ty::apply(TypeCtor::Adt(adt), Substs::identity(&generics)) 676 Ty::apply(TypeCtor::Adt(adt), Substs::identity(&generics))
668} 677}
669 678
670fn type_for_type_alias(db: &impl HirDatabase, t: TypeAliasId) -> Ty { 679fn type_for_type_alias(db: &impl HirDatabase, t: TypeAliasId) -> Ty {
671 let generics = db.generic_params(t.into()); 680 let generics = generics(db, t.into());
672 let resolver = t.resolver(db); 681 let resolver = t.resolver(db);
673 let type_ref = &db.type_alias_data(t).type_ref; 682 let type_ref = &db.type_alias_data(t).type_ref;
674 let substs = Substs::identity(&generics); 683 let substs = Substs::identity(&generics);
@@ -687,10 +696,11 @@ impl_froms!(CallableDef: FunctionId, StructId, EnumVariantId);
687impl CallableDef { 696impl CallableDef {
688 pub fn krate(self, db: &impl HirDatabase) -> CrateId { 697 pub fn krate(self, db: &impl HirDatabase) -> CrateId {
689 match self { 698 match self {
690 CallableDef::FunctionId(f) => f.lookup(db).module(db).krate, 699 CallableDef::FunctionId(f) => f.lookup(db).module(db),
691 CallableDef::StructId(s) => s.module(db).krate, 700 CallableDef::StructId(s) => s.lookup(db).container.module(db),
692 CallableDef::EnumVariantId(e) => e.parent.module(db).krate, 701 CallableDef::EnumVariantId(e) => e.parent.lookup(db).container.module(db),
693 } 702 }
703 .krate
694 } 704 }
695} 705}
696 706
@@ -733,6 +743,11 @@ pub(crate) fn ty_query(db: &impl HirDatabase, def: TyDefId) -> Ty {
733 TyDefId::TypeAliasId(it) => type_for_type_alias(db, it), 743 TyDefId::TypeAliasId(it) => type_for_type_alias(db, it),
734 } 744 }
735} 745}
746
747pub(crate) fn ty_recover(_db: &impl HirDatabase, _cycle: &[String], _def: &TyDefId) -> Ty {
748 Ty::Unknown
749}
750
736pub(crate) fn value_ty_query(db: &impl HirDatabase, def: ValueTyDefId) -> Ty { 751pub(crate) fn value_ty_query(db: &impl HirDatabase, def: ValueTyDefId) -> Ty {
737 match def { 752 match def {
738 ValueTyDefId::FunctionId(it) => type_for_fn(db, it), 753 ValueTyDefId::FunctionId(it) => type_for_fn(db, it),
@@ -743,17 +758,24 @@ pub(crate) fn value_ty_query(db: &impl HirDatabase, def: ValueTyDefId) -> Ty {
743 } 758 }
744} 759}
745 760
746pub(crate) fn impl_ty_query(db: &impl HirDatabase, impl_id: ImplId) -> ImplTy { 761pub(crate) fn impl_self_ty_query(db: &impl HirDatabase, impl_id: ImplId) -> Ty {
747 let impl_data = db.impl_data(impl_id); 762 let impl_data = db.impl_data(impl_id);
748 let resolver = impl_id.resolver(db); 763 let resolver = impl_id.resolver(db);
749 let self_ty = Ty::from_hir(db, &resolver, &impl_data.target_type); 764 Ty::from_hir(db, &resolver, &impl_data.target_type)
750 match impl_data.target_trait.as_ref() { 765}
751 Some(trait_ref) => { 766
752 match TraitRef::from_hir(db, &resolver, trait_ref, Some(self_ty.clone())) { 767pub(crate) fn impl_self_ty_recover(
753 Some(it) => ImplTy::TraitRef(it), 768 _db: &impl HirDatabase,
754 None => ImplTy::Inherent(self_ty), 769 _cycle: &[String],
755 } 770 _impl_id: &ImplId,
756 } 771) -> Ty {
757 None => ImplTy::Inherent(self_ty), 772 Ty::Unknown
758 } 773}
774
775pub(crate) fn impl_trait_query(db: &impl HirDatabase, impl_id: ImplId) -> Option<TraitRef> {
776 let impl_data = db.impl_data(impl_id);
777 let resolver = impl_id.resolver(db);
778 let self_ty = db.impl_self_ty(impl_id);
779 let target_trait = impl_data.target_trait.as_ref()?;
780 TraitRef::from_hir(db, &resolver, target_trait, Some(self_ty.clone()))
759} 781}