aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/lower.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/lower.rs')
-rw-r--r--crates/ra_hir_ty/src/lower.rs220
1 files changed, 108 insertions, 112 deletions
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs
index b96dc126c..d7f250783 100644
--- a/crates/ra_hir_ty/src/lower.rs
+++ b/crates/ra_hir_ty/src/lower.rs
@@ -34,8 +34,8 @@ use crate::{
34}; 34};
35 35
36#[derive(Debug)] 36#[derive(Debug)]
37pub struct TyLoweringContext<'a, DB: HirDatabase> { 37pub struct TyLoweringContext<'a> {
38 pub db: &'a DB, 38 pub db: &'a dyn HirDatabase,
39 pub resolver: &'a Resolver, 39 pub resolver: &'a Resolver,
40 /// Note: Conceptually, it's thinkable that we could be in a location where 40 /// Note: Conceptually, it's thinkable that we could be in a location where
41 /// some type params should be represented as placeholders, and others 41 /// some type params should be represented as placeholders, and others
@@ -46,8 +46,8 @@ pub struct TyLoweringContext<'a, DB: HirDatabase> {
46 pub impl_trait_counter: std::cell::Cell<u16>, 46 pub impl_trait_counter: std::cell::Cell<u16>,
47} 47}
48 48
49impl<'a, DB: HirDatabase> TyLoweringContext<'a, DB> { 49impl<'a> TyLoweringContext<'a> {
50 pub fn new(db: &'a DB, resolver: &'a Resolver) -> Self { 50 pub fn new(db: &'a dyn HirDatabase, resolver: &'a Resolver) -> Self {
51 let impl_trait_counter = std::cell::Cell::new(0); 51 let impl_trait_counter = std::cell::Cell::new(0);
52 let impl_trait_mode = ImplTraitLoweringMode::Disallowed; 52 let impl_trait_mode = ImplTraitLoweringMode::Disallowed;
53 let type_param_mode = TypeParamLoweringMode::Placeholder; 53 let type_param_mode = TypeParamLoweringMode::Placeholder;
@@ -90,13 +90,10 @@ pub enum TypeParamLoweringMode {
90} 90}
91 91
92impl Ty { 92impl Ty {
93 pub fn from_hir(ctx: &TyLoweringContext<'_, impl HirDatabase>, type_ref: &TypeRef) -> Self { 93 pub fn from_hir(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> Self {
94 Ty::from_hir_ext(ctx, type_ref).0 94 Ty::from_hir_ext(ctx, type_ref).0
95 } 95 }
96 pub fn from_hir_ext( 96 pub fn from_hir_ext(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> (Self, Option<TypeNs>) {
97 ctx: &TyLoweringContext<'_, impl HirDatabase>,
98 type_ref: &TypeRef,
99 ) -> (Self, Option<TypeNs>) {
100 let mut res = None; 97 let mut res = None;
101 let ty = match type_ref { 98 let ty = match type_ref {
102 TypeRef::Never => Ty::simple(TypeCtor::Never), 99 TypeRef::Never => Ty::simple(TypeCtor::Never),
@@ -157,7 +154,7 @@ impl Ty {
157 let idx = ctx.impl_trait_counter.get(); 154 let idx = ctx.impl_trait_counter.get();
158 ctx.impl_trait_counter.set(idx + 1); 155 ctx.impl_trait_counter.set(idx + 1);
159 if let Some(def) = ctx.resolver.generic_def() { 156 if let Some(def) = ctx.resolver.generic_def() {
160 let generics = generics(ctx.db, def); 157 let generics = generics(ctx.db.upcast(), def);
161 let param = generics 158 let param = generics
162 .iter() 159 .iter()
163 .filter(|(_, data)| { 160 .filter(|(_, data)| {
@@ -175,7 +172,7 @@ impl Ty {
175 ctx.impl_trait_counter.set(idx + 1); 172 ctx.impl_trait_counter.set(idx + 1);
176 let (parent_params, self_params, list_params, _impl_trait_params) = 173 let (parent_params, self_params, list_params, _impl_trait_params) =
177 if let Some(def) = ctx.resolver.generic_def() { 174 if let Some(def) = ctx.resolver.generic_def() {
178 let generics = generics(ctx.db, def); 175 let generics = generics(ctx.db.upcast(), def);
179 generics.provenance_split() 176 generics.provenance_split()
180 } else { 177 } else {
181 (0, 0, 0, 0) 178 (0, 0, 0, 0)
@@ -201,10 +198,7 @@ impl Ty {
201 /// This is only for `generic_predicates_for_param`, where we can't just 198 /// This is only for `generic_predicates_for_param`, where we can't just
202 /// lower the self types of the predicates since that could lead to cycles. 199 /// lower the self types of the predicates since that could lead to cycles.
203 /// So we just check here if the `type_ref` resolves to a generic param, and which. 200 /// So we just check here if the `type_ref` resolves to a generic param, and which.
204 fn from_hir_only_param( 201 fn from_hir_only_param(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> Option<TypeParamId> {
205 ctx: &TyLoweringContext<'_, impl HirDatabase>,
206 type_ref: &TypeRef,
207 ) -> Option<TypeParamId> {
208 let path = match type_ref { 202 let path = match type_ref {
209 TypeRef::Path(path) => path, 203 TypeRef::Path(path) => path,
210 _ => return None, 204 _ => return None,
@@ -215,10 +209,11 @@ impl Ty {
215 if path.segments().len() > 1 { 209 if path.segments().len() > 1 {
216 return None; 210 return None;
217 } 211 }
218 let resolution = match ctx.resolver.resolve_path_in_type_ns(ctx.db, path.mod_path()) { 212 let resolution =
219 Some((it, None)) => it, 213 match ctx.resolver.resolve_path_in_type_ns(ctx.db.upcast(), path.mod_path()) {
220 _ => return None, 214 Some((it, None)) => it,
221 }; 215 _ => return None,
216 };
222 if let TypeNs::GenericParam(param_id) = resolution { 217 if let TypeNs::GenericParam(param_id) = resolution {
223 Some(param_id) 218 Some(param_id)
224 } else { 219 } else {
@@ -227,7 +222,7 @@ impl Ty {
227 } 222 }
228 223
229 pub(crate) fn from_type_relative_path( 224 pub(crate) fn from_type_relative_path(
230 ctx: &TyLoweringContext<'_, impl HirDatabase>, 225 ctx: &TyLoweringContext<'_>,
231 ty: Ty, 226 ty: Ty,
232 // We need the original resolution to lower `Self::AssocTy` correctly 227 // We need the original resolution to lower `Self::AssocTy` correctly
233 res: Option<TypeNs>, 228 res: Option<TypeNs>,
@@ -246,7 +241,7 @@ impl Ty {
246 } 241 }
247 242
248 pub(crate) fn from_partly_resolved_hir_path( 243 pub(crate) fn from_partly_resolved_hir_path(
249 ctx: &TyLoweringContext<'_, impl HirDatabase>, 244 ctx: &TyLoweringContext<'_>,
250 resolution: TypeNs, 245 resolution: TypeNs,
251 resolved_segment: PathSegment<'_>, 246 resolved_segment: PathSegment<'_>,
252 remaining_segments: PathSegments<'_>, 247 remaining_segments: PathSegments<'_>,
@@ -260,7 +255,7 @@ impl Ty {
260 let ty = if remaining_segments.len() == 1 { 255 let ty = if remaining_segments.len() == 1 {
261 let segment = remaining_segments.first().unwrap(); 256 let segment = remaining_segments.first().unwrap();
262 let associated_ty = associated_type_by_name_including_super_traits( 257 let associated_ty = associated_type_by_name_including_super_traits(
263 ctx.db, 258 ctx.db.upcast(),
264 trait_ref.trait_, 259 trait_ref.trait_,
265 &segment.name, 260 &segment.name,
266 ); 261 );
@@ -286,8 +281,10 @@ impl Ty {
286 return (ty, None); 281 return (ty, None);
287 } 282 }
288 TypeNs::GenericParam(param_id) => { 283 TypeNs::GenericParam(param_id) => {
289 let generics = 284 let generics = generics(
290 generics(ctx.db, ctx.resolver.generic_def().expect("generics in scope")); 285 ctx.db.upcast(),
286 ctx.resolver.generic_def().expect("generics in scope"),
287 );
291 match ctx.type_param_mode { 288 match ctx.type_param_mode {
292 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), 289 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id),
293 TypeParamLoweringMode::Variable => { 290 TypeParamLoweringMode::Variable => {
@@ -297,7 +294,7 @@ impl Ty {
297 } 294 }
298 } 295 }
299 TypeNs::SelfType(impl_id) => { 296 TypeNs::SelfType(impl_id) => {
300 let generics = generics(ctx.db, impl_id.into()); 297 let generics = generics(ctx.db.upcast(), impl_id.into());
301 let substs = match ctx.type_param_mode { 298 let substs = match ctx.type_param_mode {
302 TypeParamLoweringMode::Placeholder => { 299 TypeParamLoweringMode::Placeholder => {
303 Substs::type_params_for_generics(&generics) 300 Substs::type_params_for_generics(&generics)
@@ -307,7 +304,7 @@ impl Ty {
307 ctx.db.impl_self_ty(impl_id).subst(&substs) 304 ctx.db.impl_self_ty(impl_id).subst(&substs)
308 } 305 }
309 TypeNs::AdtSelfType(adt) => { 306 TypeNs::AdtSelfType(adt) => {
310 let generics = generics(ctx.db, adt.into()); 307 let generics = generics(ctx.db.upcast(), adt.into());
311 let substs = match ctx.type_param_mode { 308 let substs = match ctx.type_param_mode {
312 TypeParamLoweringMode::Placeholder => { 309 TypeParamLoweringMode::Placeholder => {
313 Substs::type_params_for_generics(&generics) 310 Substs::type_params_for_generics(&generics)
@@ -327,17 +324,14 @@ impl Ty {
327 Ty::from_type_relative_path(ctx, ty, Some(resolution), remaining_segments) 324 Ty::from_type_relative_path(ctx, ty, Some(resolution), remaining_segments)
328 } 325 }
329 326
330 pub(crate) fn from_hir_path( 327 pub(crate) fn from_hir_path(ctx: &TyLoweringContext<'_>, path: &Path) -> (Ty, Option<TypeNs>) {
331 ctx: &TyLoweringContext<'_, impl HirDatabase>,
332 path: &Path,
333 ) -> (Ty, Option<TypeNs>) {
334 // Resolve the path (in type namespace) 328 // Resolve the path (in type namespace)
335 if let Some(type_ref) = path.type_anchor() { 329 if let Some(type_ref) = path.type_anchor() {
336 let (ty, res) = Ty::from_hir_ext(ctx, &type_ref); 330 let (ty, res) = Ty::from_hir_ext(ctx, &type_ref);
337 return Ty::from_type_relative_path(ctx, ty, res, path.segments()); 331 return Ty::from_type_relative_path(ctx, ty, res, path.segments());
338 } 332 }
339 let (resolution, remaining_index) = 333 let (resolution, remaining_index) =
340 match ctx.resolver.resolve_path_in_type_ns(ctx.db, path.mod_path()) { 334 match ctx.resolver.resolve_path_in_type_ns(ctx.db.upcast(), path.mod_path()) {
341 Some(it) => it, 335 Some(it) => it,
342 None => return (Ty::Unknown, None), 336 None => return (Ty::Unknown, None),
343 }; 337 };
@@ -352,7 +346,7 @@ impl Ty {
352 } 346 }
353 347
354 fn select_associated_type( 348 fn select_associated_type(
355 ctx: &TyLoweringContext<'_, impl HirDatabase>, 349 ctx: &TyLoweringContext<'_>,
356 self_ty: Ty, 350 self_ty: Ty,
357 res: Option<TypeNs>, 351 res: Option<TypeNs>,
358 segment: PathSegment<'_>, 352 segment: PathSegment<'_>,
@@ -374,7 +368,7 @@ impl Ty {
374 } 368 }
375 _ => return Ty::Unknown, 369 _ => return Ty::Unknown,
376 }; 370 };
377 let traits = traits_from_env.into_iter().flat_map(|t| all_super_traits(ctx.db, t)); 371 let traits = traits_from_env.into_iter().flat_map(|t| all_super_traits(ctx.db.upcast(), t));
378 for t in traits { 372 for t in traits {
379 if let Some(associated_ty) = ctx.db.trait_data(t).associated_type_by_name(&segment.name) 373 if let Some(associated_ty) = ctx.db.trait_data(t).associated_type_by_name(&segment.name)
380 { 374 {
@@ -388,7 +382,7 @@ impl Ty {
388 } 382 }
389 383
390 fn from_hir_path_inner( 384 fn from_hir_path_inner(
391 ctx: &TyLoweringContext<'_, impl HirDatabase>, 385 ctx: &TyLoweringContext<'_>,
392 segment: PathSegment<'_>, 386 segment: PathSegment<'_>,
393 typable: TyDefId, 387 typable: TyDefId,
394 ) -> Ty { 388 ) -> Ty {
@@ -404,7 +398,7 @@ impl Ty {
404 /// Collect generic arguments from a path into a `Substs`. See also 398 /// Collect generic arguments from a path into a `Substs`. See also
405 /// `create_substs_for_ast_path` and `def_to_ty` in rustc. 399 /// `create_substs_for_ast_path` and `def_to_ty` in rustc.
406 pub(super) fn substs_from_path( 400 pub(super) fn substs_from_path(
407 ctx: &TyLoweringContext<'_, impl HirDatabase>, 401 ctx: &TyLoweringContext<'_>,
408 path: &Path, 402 path: &Path,
409 // Note that we don't call `db.value_type(resolved)` here, 403 // Note that we don't call `db.value_type(resolved)` here,
410 // `ValueTyDefId` is just a convenient way to pass generics and 404 // `ValueTyDefId` is just a convenient way to pass generics and
@@ -437,13 +431,13 @@ impl Ty {
437} 431}
438 432
439pub(super) fn substs_from_path_segment( 433pub(super) fn substs_from_path_segment(
440 ctx: &TyLoweringContext<'_, impl HirDatabase>, 434 ctx: &TyLoweringContext<'_>,
441 segment: PathSegment<'_>, 435 segment: PathSegment<'_>,
442 def_generic: Option<GenericDefId>, 436 def_generic: Option<GenericDefId>,
443 _add_self_param: bool, 437 _add_self_param: bool,
444) -> Substs { 438) -> Substs {
445 let mut substs = Vec::new(); 439 let mut substs = Vec::new();
446 let def_generics = def_generic.map(|def| generics(ctx.db, def)); 440 let def_generics = def_generic.map(|def| generics(ctx.db.upcast(), def));
447 441
448 let (parent_params, self_params, type_params, impl_trait_params) = 442 let (parent_params, self_params, type_params, impl_trait_params) =
449 def_generics.map_or((0, 0, 0, 0), |g| g.provenance_split()); 443 def_generics.map_or((0, 0, 0, 0), |g| g.provenance_split());
@@ -489,20 +483,21 @@ pub(super) fn substs_from_path_segment(
489 483
490impl TraitRef { 484impl TraitRef {
491 fn from_path( 485 fn from_path(
492 ctx: &TyLoweringContext<'_, impl HirDatabase>, 486 ctx: &TyLoweringContext<'_>,
493 path: &Path, 487 path: &Path,
494 explicit_self_ty: Option<Ty>, 488 explicit_self_ty: Option<Ty>,
495 ) -> Option<Self> { 489 ) -> Option<Self> {
496 let resolved = match ctx.resolver.resolve_path_in_type_ns_fully(ctx.db, path.mod_path())? { 490 let resolved =
497 TypeNs::TraitId(tr) => tr, 491 match ctx.resolver.resolve_path_in_type_ns_fully(ctx.db.upcast(), path.mod_path())? {
498 _ => return None, 492 TypeNs::TraitId(tr) => tr,
499 }; 493 _ => return None,
494 };
500 let segment = path.segments().last().expect("path should have at least one segment"); 495 let segment = path.segments().last().expect("path should have at least one segment");
501 Some(TraitRef::from_resolved_path(ctx, resolved, segment, explicit_self_ty)) 496 Some(TraitRef::from_resolved_path(ctx, resolved, segment, explicit_self_ty))
502 } 497 }
503 498
504 pub(crate) fn from_resolved_path( 499 pub(crate) fn from_resolved_path(
505 ctx: &TyLoweringContext<'_, impl HirDatabase>, 500 ctx: &TyLoweringContext<'_>,
506 resolved: TraitId, 501 resolved: TraitId,
507 segment: PathSegment<'_>, 502 segment: PathSegment<'_>,
508 explicit_self_ty: Option<Ty>, 503 explicit_self_ty: Option<Ty>,
@@ -515,7 +510,7 @@ impl TraitRef {
515 } 510 }
516 511
517 fn from_hir( 512 fn from_hir(
518 ctx: &TyLoweringContext<'_, impl HirDatabase>, 513 ctx: &TyLoweringContext<'_>,
519 type_ref: &TypeRef, 514 type_ref: &TypeRef,
520 explicit_self_ty: Option<Ty>, 515 explicit_self_ty: Option<Ty>,
521 ) -> Option<Self> { 516 ) -> Option<Self> {
@@ -527,7 +522,7 @@ impl TraitRef {
527 } 522 }
528 523
529 fn substs_from_path( 524 fn substs_from_path(
530 ctx: &TyLoweringContext<'_, impl HirDatabase>, 525 ctx: &TyLoweringContext<'_>,
531 segment: PathSegment<'_>, 526 segment: PathSegment<'_>,
532 resolved: TraitId, 527 resolved: TraitId,
533 ) -> Substs { 528 ) -> Substs {
@@ -537,7 +532,7 @@ impl TraitRef {
537 } 532 }
538 533
539 pub(crate) fn from_type_bound( 534 pub(crate) fn from_type_bound(
540 ctx: &TyLoweringContext<'_, impl HirDatabase>, 535 ctx: &TyLoweringContext<'_>,
541 bound: &TypeBound, 536 bound: &TypeBound,
542 self_ty: Ty, 537 self_ty: Ty,
543 ) -> Option<TraitRef> { 538 ) -> Option<TraitRef> {
@@ -550,14 +545,14 @@ impl TraitRef {
550 545
551impl GenericPredicate { 546impl GenericPredicate {
552 pub(crate) fn from_where_predicate<'a>( 547 pub(crate) fn from_where_predicate<'a>(
553 ctx: &'a TyLoweringContext<'a, impl HirDatabase>, 548 ctx: &'a TyLoweringContext<'a>,
554 where_predicate: &'a WherePredicate, 549 where_predicate: &'a WherePredicate,
555 ) -> impl Iterator<Item = GenericPredicate> + 'a { 550 ) -> impl Iterator<Item = GenericPredicate> + 'a {
556 let self_ty = match &where_predicate.target { 551 let self_ty = match &where_predicate.target {
557 WherePredicateTarget::TypeRef(type_ref) => Ty::from_hir(ctx, type_ref), 552 WherePredicateTarget::TypeRef(type_ref) => Ty::from_hir(ctx, type_ref),
558 WherePredicateTarget::TypeParam(param_id) => { 553 WherePredicateTarget::TypeParam(param_id) => {
559 let generic_def = ctx.resolver.generic_def().expect("generics in scope"); 554 let generic_def = ctx.resolver.generic_def().expect("generics in scope");
560 let generics = generics(ctx.db, generic_def); 555 let generics = generics(ctx.db.upcast(), generic_def);
561 let param_id = hir_def::TypeParamId { parent: generic_def, local_id: *param_id }; 556 let param_id = hir_def::TypeParamId { parent: generic_def, local_id: *param_id };
562 match ctx.type_param_mode { 557 match ctx.type_param_mode {
563 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), 558 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id),
@@ -572,7 +567,7 @@ impl GenericPredicate {
572 } 567 }
573 568
574 pub(crate) fn from_type_bound<'a>( 569 pub(crate) fn from_type_bound<'a>(
575 ctx: &'a TyLoweringContext<'a, impl HirDatabase>, 570 ctx: &'a TyLoweringContext<'a>,
576 bound: &'a TypeBound, 571 bound: &'a TypeBound,
577 self_ty: Ty, 572 self_ty: Ty,
578 ) -> impl Iterator<Item = GenericPredicate> + 'a { 573 ) -> impl Iterator<Item = GenericPredicate> + 'a {
@@ -587,7 +582,7 @@ impl GenericPredicate {
587} 582}
588 583
589fn assoc_type_bindings_from_type_bound<'a>( 584fn assoc_type_bindings_from_type_bound<'a>(
590 ctx: &'a TyLoweringContext<'a, impl HirDatabase>, 585 ctx: &'a TyLoweringContext<'a>,
591 bound: &'a TypeBound, 586 bound: &'a TypeBound,
592 trait_ref: TraitRef, 587 trait_ref: TraitRef,
593) -> impl Iterator<Item = GenericPredicate> + 'a { 588) -> impl Iterator<Item = GenericPredicate> + 'a {
@@ -600,8 +595,11 @@ fn assoc_type_bindings_from_type_bound<'a>(
600 .flat_map(|segment| segment.args_and_bindings.into_iter()) 595 .flat_map(|segment| segment.args_and_bindings.into_iter())
601 .flat_map(|args_and_bindings| args_and_bindings.bindings.iter()) 596 .flat_map(|args_and_bindings| args_and_bindings.bindings.iter())
602 .map(move |(name, type_ref)| { 597 .map(move |(name, type_ref)| {
603 let associated_ty = 598 let associated_ty = associated_type_by_name_including_super_traits(
604 associated_type_by_name_including_super_traits(ctx.db, trait_ref.trait_, &name); 599 ctx.db.upcast(),
600 trait_ref.trait_,
601 &name,
602 );
605 let associated_ty = match associated_ty { 603 let associated_ty = match associated_ty {
606 None => return GenericPredicate::Error, 604 None => return GenericPredicate::Error,
607 Some(t) => t, 605 Some(t) => t,
@@ -615,7 +613,7 @@ fn assoc_type_bindings_from_type_bound<'a>(
615} 613}
616 614
617/// Build the signature of a callable item (function, struct or enum variant). 615/// Build the signature of a callable item (function, struct or enum variant).
618pub fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> PolyFnSig { 616pub fn callable_item_sig(db: &dyn HirDatabase, def: CallableDef) -> PolyFnSig {
619 match def { 617 match def {
620 CallableDef::FunctionId(f) => fn_sig_for_fn(db, f), 618 CallableDef::FunctionId(f) => fn_sig_for_fn(db, f),
621 CallableDef::StructId(s) => fn_sig_for_struct_constructor(db, s), 619 CallableDef::StructId(s) => fn_sig_for_struct_constructor(db, s),
@@ -625,16 +623,16 @@ pub fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> PolyFnSig {
625 623
626/// Build the type of all specific fields of a struct or enum variant. 624/// Build the type of all specific fields of a struct or enum variant.
627pub(crate) fn field_types_query( 625pub(crate) fn field_types_query(
628 db: &impl HirDatabase, 626 db: &dyn HirDatabase,
629 variant_id: VariantId, 627 variant_id: VariantId,
630) -> Arc<ArenaMap<LocalStructFieldId, Binders<Ty>>> { 628) -> Arc<ArenaMap<LocalStructFieldId, Binders<Ty>>> {
631 let var_data = variant_data(db, variant_id); 629 let var_data = variant_data(db.upcast(), variant_id);
632 let (resolver, def): (_, GenericDefId) = match variant_id { 630 let (resolver, def): (_, GenericDefId) = match variant_id {
633 VariantId::StructId(it) => (it.resolver(db), it.into()), 631 VariantId::StructId(it) => (it.resolver(db.upcast()), it.into()),
634 VariantId::UnionId(it) => (it.resolver(db), it.into()), 632 VariantId::UnionId(it) => (it.resolver(db.upcast()), it.into()),
635 VariantId::EnumVariantId(it) => (it.parent.resolver(db), it.parent.into()), 633 VariantId::EnumVariantId(it) => (it.parent.resolver(db.upcast()), it.parent.into()),
636 }; 634 };
637 let generics = generics(db, def); 635 let generics = generics(db.upcast(), def);
638 let mut res = ArenaMap::default(); 636 let mut res = ArenaMap::default();
639 let ctx = 637 let ctx =
640 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 638 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
@@ -653,13 +651,13 @@ pub(crate) fn field_types_query(
653/// following bounds are disallowed: `T: Foo<U::Item>, U: Foo<T::Item>`, but 651/// following bounds are disallowed: `T: Foo<U::Item>, U: Foo<T::Item>`, but
654/// these are fine: `T: Foo<U::Item>, U: Foo<()>`. 652/// these are fine: `T: Foo<U::Item>, U: Foo<()>`.
655pub(crate) fn generic_predicates_for_param_query( 653pub(crate) fn generic_predicates_for_param_query(
656 db: &impl HirDatabase, 654 db: &dyn HirDatabase,
657 param_id: TypeParamId, 655 param_id: TypeParamId,
658) -> Arc<[Binders<GenericPredicate>]> { 656) -> Arc<[Binders<GenericPredicate>]> {
659 let resolver = param_id.parent.resolver(db); 657 let resolver = param_id.parent.resolver(db.upcast());
660 let ctx = 658 let ctx =
661 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 659 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
662 let generics = generics(db, param_id.parent); 660 let generics = generics(db.upcast(), param_id.parent);
663 resolver 661 resolver
664 .where_predicates_in_scope() 662 .where_predicates_in_scope()
665 // we have to filter out all other predicates *first*, before attempting to lower them 663 // we have to filter out all other predicates *first*, before attempting to lower them
@@ -677,7 +675,7 @@ pub(crate) fn generic_predicates_for_param_query(
677} 675}
678 676
679pub(crate) fn generic_predicates_for_param_recover( 677pub(crate) fn generic_predicates_for_param_recover(
680 _db: &impl HirDatabase, 678 _db: &dyn HirDatabase,
681 _cycle: &[String], 679 _cycle: &[String],
682 _param_id: &TypeParamId, 680 _param_id: &TypeParamId,
683) -> Arc<[Binders<GenericPredicate>]> { 681) -> Arc<[Binders<GenericPredicate>]> {
@@ -685,7 +683,7 @@ pub(crate) fn generic_predicates_for_param_recover(
685} 683}
686 684
687impl TraitEnvironment { 685impl TraitEnvironment {
688 pub fn lower(db: &impl HirDatabase, resolver: &Resolver) -> Arc<TraitEnvironment> { 686 pub fn lower(db: &dyn HirDatabase, resolver: &Resolver) -> Arc<TraitEnvironment> {
689 let ctx = TyLoweringContext::new(db, &resolver) 687 let ctx = TyLoweringContext::new(db, &resolver)
690 .with_type_param_mode(TypeParamLoweringMode::Placeholder); 688 .with_type_param_mode(TypeParamLoweringMode::Placeholder);
691 let mut predicates = resolver 689 let mut predicates = resolver
@@ -696,13 +694,13 @@ impl TraitEnvironment {
696 if let Some(def) = resolver.generic_def() { 694 if let Some(def) = resolver.generic_def() {
697 let container: Option<AssocContainerId> = match def { 695 let container: Option<AssocContainerId> = match def {
698 // FIXME: is there a function for this? 696 // FIXME: is there a function for this?
699 GenericDefId::FunctionId(f) => Some(f.lookup(db).container), 697 GenericDefId::FunctionId(f) => Some(f.lookup(db.upcast()).container),
700 GenericDefId::AdtId(_) => None, 698 GenericDefId::AdtId(_) => None,
701 GenericDefId::TraitId(_) => None, 699 GenericDefId::TraitId(_) => None,
702 GenericDefId::TypeAliasId(t) => Some(t.lookup(db).container), 700 GenericDefId::TypeAliasId(t) => Some(t.lookup(db.upcast()).container),
703 GenericDefId::ImplId(_) => None, 701 GenericDefId::ImplId(_) => None,
704 GenericDefId::EnumVariantId(_) => None, 702 GenericDefId::EnumVariantId(_) => None,
705 GenericDefId::ConstId(c) => Some(c.lookup(db).container), 703 GenericDefId::ConstId(c) => Some(c.lookup(db.upcast()).container),
706 }; 704 };
707 if let Some(AssocContainerId::TraitId(trait_id)) = container { 705 if let Some(AssocContainerId::TraitId(trait_id)) = container {
708 // add `Self: Trait<T1, T2, ...>` to the environment in trait 706 // add `Self: Trait<T1, T2, ...>` to the environment in trait
@@ -723,13 +721,13 @@ impl TraitEnvironment {
723 721
724/// Resolve the where clause(s) of an item with generics. 722/// Resolve the where clause(s) of an item with generics.
725pub(crate) fn generic_predicates_query( 723pub(crate) fn generic_predicates_query(
726 db: &impl HirDatabase, 724 db: &dyn HirDatabase,
727 def: GenericDefId, 725 def: GenericDefId,
728) -> Arc<[Binders<GenericPredicate>]> { 726) -> Arc<[Binders<GenericPredicate>]> {
729 let resolver = def.resolver(db); 727 let resolver = def.resolver(db.upcast());
730 let ctx = 728 let ctx =
731 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 729 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
732 let generics = generics(db, def); 730 let generics = generics(db.upcast(), def);
733 resolver 731 resolver
734 .where_predicates_in_scope() 732 .where_predicates_in_scope()
735 .flat_map(|pred| { 733 .flat_map(|pred| {
@@ -740,10 +738,10 @@ pub(crate) fn generic_predicates_query(
740} 738}
741 739
742/// Resolve the default type params from generics 740/// Resolve the default type params from generics
743pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDefId) -> Substs { 741pub(crate) fn generic_defaults_query(db: &dyn HirDatabase, def: GenericDefId) -> Substs {
744 let resolver = def.resolver(db); 742 let resolver = def.resolver(db.upcast());
745 let ctx = TyLoweringContext::new(db, &resolver); 743 let ctx = TyLoweringContext::new(db, &resolver);
746 let generic_params = generics(db, def); 744 let generic_params = generics(db.upcast(), def);
747 745
748 let defaults = generic_params 746 let defaults = generic_params
749 .iter() 747 .iter()
@@ -753,33 +751,33 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDefId) -
753 Substs(defaults) 751 Substs(defaults)
754} 752}
755 753
756fn fn_sig_for_fn(db: &impl HirDatabase, def: FunctionId) -> PolyFnSig { 754fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
757 let data = db.function_data(def); 755 let data = db.function_data(def);
758 let resolver = def.resolver(db); 756 let resolver = def.resolver(db.upcast());
759 let ctx_params = TyLoweringContext::new(db, &resolver) 757 let ctx_params = TyLoweringContext::new(db, &resolver)
760 .with_impl_trait_mode(ImplTraitLoweringMode::Variable) 758 .with_impl_trait_mode(ImplTraitLoweringMode::Variable)
761 .with_type_param_mode(TypeParamLoweringMode::Variable); 759 .with_type_param_mode(TypeParamLoweringMode::Variable);
762 let params = data.params.iter().map(|tr| Ty::from_hir(&ctx_params, tr)).collect::<Vec<_>>(); 760 let params = data.params.iter().map(|tr| Ty::from_hir(&ctx_params, tr)).collect::<Vec<_>>();
763 let ctx_ret = ctx_params.with_impl_trait_mode(ImplTraitLoweringMode::Opaque); 761 let ctx_ret = ctx_params.with_impl_trait_mode(ImplTraitLoweringMode::Opaque);
764 let ret = Ty::from_hir(&ctx_ret, &data.ret_type); 762 let ret = Ty::from_hir(&ctx_ret, &data.ret_type);
765 let generics = generics(db, def.into()); 763 let generics = generics(db.upcast(), def.into());
766 let num_binders = generics.len(); 764 let num_binders = generics.len();
767 Binders::new(num_binders, FnSig::from_params_and_return(params, ret)) 765 Binders::new(num_binders, FnSig::from_params_and_return(params, ret))
768} 766}
769 767
770/// Build the declared type of a function. This should not need to look at the 768/// Build the declared type of a function. This should not need to look at the
771/// function body. 769/// function body.
772fn type_for_fn(db: &impl HirDatabase, def: FunctionId) -> Binders<Ty> { 770fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
773 let generics = generics(db, def.into()); 771 let generics = generics(db.upcast(), def.into());
774 let substs = Substs::bound_vars(&generics); 772 let substs = Substs::bound_vars(&generics);
775 Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) 773 Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs))
776} 774}
777 775
778/// Build the declared type of a const. 776/// Build the declared type of a const.
779fn type_for_const(db: &impl HirDatabase, def: ConstId) -> Binders<Ty> { 777fn type_for_const(db: &dyn HirDatabase, def: ConstId) -> Binders<Ty> {
780 let data = db.const_data(def); 778 let data = db.const_data(def);
781 let generics = generics(db, def.into()); 779 let generics = generics(db.upcast(), def.into());
782 let resolver = def.resolver(db); 780 let resolver = def.resolver(db.upcast());
783 let ctx = 781 let ctx =
784 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 782 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
785 783
@@ -787,9 +785,9 @@ fn type_for_const(db: &impl HirDatabase, def: ConstId) -> Binders<Ty> {
787} 785}
788 786
789/// Build the declared type of a static. 787/// Build the declared type of a static.
790fn type_for_static(db: &impl HirDatabase, def: StaticId) -> Binders<Ty> { 788fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> {
791 let data = db.static_data(def); 789 let data = db.static_data(def);
792 let resolver = def.resolver(db); 790 let resolver = def.resolver(db.upcast());
793 let ctx = TyLoweringContext::new(db, &resolver); 791 let ctx = TyLoweringContext::new(db, &resolver);
794 792
795 Binders::new(0, Ty::from_hir(&ctx, &data.type_ref)) 793 Binders::new(0, Ty::from_hir(&ctx, &data.type_ref))
@@ -806,10 +804,10 @@ fn type_for_builtin(def: BuiltinType) -> Ty {
806 }) 804 })
807} 805}
808 806
809fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> PolyFnSig { 807fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig {
810 let struct_data = db.struct_data(def); 808 let struct_data = db.struct_data(def);
811 let fields = struct_data.variant_data.fields(); 809 let fields = struct_data.variant_data.fields();
812 let resolver = def.resolver(db); 810 let resolver = def.resolver(db.upcast());
813 let ctx = 811 let ctx =
814 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 812 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
815 let params = 813 let params =
@@ -819,21 +817,21 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> PolyFn
819} 817}
820 818
821/// Build the type of a tuple struct constructor. 819/// Build the type of a tuple struct constructor.
822fn type_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> Binders<Ty> { 820fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<Ty> {
823 let struct_data = db.struct_data(def); 821 let struct_data = db.struct_data(def);
824 if let StructKind::Unit = struct_data.variant_data.kind() { 822 if let StructKind::Unit = struct_data.variant_data.kind() {
825 return type_for_adt(db, def.into()); 823 return type_for_adt(db, def.into());
826 } 824 }
827 let generics = generics(db, def.into()); 825 let generics = generics(db.upcast(), def.into());
828 let substs = Substs::bound_vars(&generics); 826 let substs = Substs::bound_vars(&generics);
829 Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) 827 Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs))
830} 828}
831 829
832fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId) -> PolyFnSig { 830fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig {
833 let enum_data = db.enum_data(def.parent); 831 let enum_data = db.enum_data(def.parent);
834 let var_data = &enum_data.variants[def.local_id]; 832 let var_data = &enum_data.variants[def.local_id];
835 let fields = var_data.variant_data.fields(); 833 let fields = var_data.variant_data.fields();
836 let resolver = def.parent.resolver(db); 834 let resolver = def.parent.resolver(db.upcast());
837 let ctx = 835 let ctx =
838 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 836 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
839 let params = 837 let params =
@@ -843,26 +841,26 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId
843} 841}
844 842
845/// Build the type of a tuple enum variant constructor. 843/// Build the type of a tuple enum variant constructor.
846fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId) -> Binders<Ty> { 844fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> Binders<Ty> {
847 let enum_data = db.enum_data(def.parent); 845 let enum_data = db.enum_data(def.parent);
848 let var_data = &enum_data.variants[def.local_id].variant_data; 846 let var_data = &enum_data.variants[def.local_id].variant_data;
849 if let StructKind::Unit = var_data.kind() { 847 if let StructKind::Unit = var_data.kind() {
850 return type_for_adt(db, def.parent.into()); 848 return type_for_adt(db, def.parent.into());
851 } 849 }
852 let generics = generics(db, def.parent.into()); 850 let generics = generics(db.upcast(), def.parent.into());
853 let substs = Substs::bound_vars(&generics); 851 let substs = Substs::bound_vars(&generics);
854 Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) 852 Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs))
855} 853}
856 854
857fn type_for_adt(db: &impl HirDatabase, adt: AdtId) -> Binders<Ty> { 855fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
858 let generics = generics(db, adt.into()); 856 let generics = generics(db.upcast(), adt.into());
859 let substs = Substs::bound_vars(&generics); 857 let substs = Substs::bound_vars(&generics);
860 Binders::new(substs.len(), Ty::apply(TypeCtor::Adt(adt), substs)) 858 Binders::new(substs.len(), Ty::apply(TypeCtor::Adt(adt), substs))
861} 859}
862 860
863fn type_for_type_alias(db: &impl HirDatabase, t: TypeAliasId) -> Binders<Ty> { 861fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
864 let generics = generics(db, t.into()); 862 let generics = generics(db.upcast(), t.into());
865 let resolver = t.resolver(db); 863 let resolver = t.resolver(db.upcast());
866 let ctx = 864 let ctx =
867 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 865 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
868 let type_ref = &db.type_alias_data(t).type_ref; 866 let type_ref = &db.type_alias_data(t).type_ref;
@@ -880,7 +878,8 @@ pub enum CallableDef {
880impl_froms!(CallableDef: FunctionId, StructId, EnumVariantId); 878impl_froms!(CallableDef: FunctionId, StructId, EnumVariantId);
881 879
882impl CallableDef { 880impl CallableDef {
883 pub fn krate(self, db: &impl HirDatabase) -> CrateId { 881 pub fn krate(self, db: &dyn HirDatabase) -> CrateId {
882 let db = db.upcast();
884 match self { 883 match self {
885 CallableDef::FunctionId(f) => f.lookup(db).module(db), 884 CallableDef::FunctionId(f) => f.lookup(db).module(db),
886 CallableDef::StructId(s) => s.lookup(db).container.module(db), 885 CallableDef::StructId(s) => s.lookup(db).container.module(db),
@@ -922,7 +921,7 @@ impl_froms!(ValueTyDefId: FunctionId, StructId, EnumVariantId, ConstId, StaticId
922/// `struct Foo(usize)`, we have two types: The type of the struct itself, and 921/// `struct Foo(usize)`, we have two types: The type of the struct itself, and
923/// the constructor function `(usize) -> Foo` which lives in the values 922/// the constructor function `(usize) -> Foo` which lives in the values
924/// namespace. 923/// namespace.
925pub(crate) fn ty_query(db: &impl HirDatabase, def: TyDefId) -> Binders<Ty> { 924pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> {
926 match def { 925 match def {
927 TyDefId::BuiltinType(it) => Binders::new(0, type_for_builtin(it)), 926 TyDefId::BuiltinType(it) => Binders::new(0, type_for_builtin(it)),
928 TyDefId::AdtId(it) => type_for_adt(db, it), 927 TyDefId::AdtId(it) => type_for_adt(db, it),
@@ -930,16 +929,16 @@ pub(crate) fn ty_query(db: &impl HirDatabase, def: TyDefId) -> Binders<Ty> {
930 } 929 }
931} 930}
932 931
933pub(crate) fn ty_recover(db: &impl HirDatabase, _cycle: &[String], def: &TyDefId) -> Binders<Ty> { 932pub(crate) fn ty_recover(db: &dyn HirDatabase, _cycle: &[String], def: &TyDefId) -> Binders<Ty> {
934 let num_binders = match *def { 933 let num_binders = match *def {
935 TyDefId::BuiltinType(_) => 0, 934 TyDefId::BuiltinType(_) => 0,
936 TyDefId::AdtId(it) => generics(db, it.into()).len(), 935 TyDefId::AdtId(it) => generics(db.upcast(), it.into()).len(),
937 TyDefId::TypeAliasId(it) => generics(db, it.into()).len(), 936 TyDefId::TypeAliasId(it) => generics(db.upcast(), it.into()).len(),
938 }; 937 };
939 Binders::new(num_binders, Ty::Unknown) 938 Binders::new(num_binders, Ty::Unknown)
940} 939}
941 940
942pub(crate) fn value_ty_query(db: &impl HirDatabase, def: ValueTyDefId) -> Binders<Ty> { 941pub(crate) fn value_ty_query(db: &dyn HirDatabase, def: ValueTyDefId) -> Binders<Ty> {
943 match def { 942 match def {
944 ValueTyDefId::FunctionId(it) => type_for_fn(db, it), 943 ValueTyDefId::FunctionId(it) => type_for_fn(db, it),
945 ValueTyDefId::StructId(it) => type_for_struct_constructor(db, it), 944 ValueTyDefId::StructId(it) => type_for_struct_constructor(db, it),
@@ -949,30 +948,27 @@ pub(crate) fn value_ty_query(db: &impl HirDatabase, def: ValueTyDefId) -> Binder
949 } 948 }
950} 949}
951 950
952pub(crate) fn impl_self_ty_query(db: &impl HirDatabase, impl_id: ImplId) -> Binders<Ty> { 951pub(crate) fn impl_self_ty_query(db: &dyn HirDatabase, impl_id: ImplId) -> Binders<Ty> {
953 let impl_data = db.impl_data(impl_id); 952 let impl_data = db.impl_data(impl_id);
954 let resolver = impl_id.resolver(db); 953 let resolver = impl_id.resolver(db.upcast());
955 let generics = generics(db, impl_id.into()); 954 let generics = generics(db.upcast(), impl_id.into());
956 let ctx = 955 let ctx =
957 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 956 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
958 Binders::new(generics.len(), Ty::from_hir(&ctx, &impl_data.target_type)) 957 Binders::new(generics.len(), Ty::from_hir(&ctx, &impl_data.target_type))
959} 958}
960 959
961pub(crate) fn impl_self_ty_recover( 960pub(crate) fn impl_self_ty_recover(
962 db: &impl HirDatabase, 961 db: &dyn HirDatabase,
963 _cycle: &[String], 962 _cycle: &[String],
964 impl_id: &ImplId, 963 impl_id: &ImplId,
965) -> Binders<Ty> { 964) -> Binders<Ty> {
966 let generics = generics(db, (*impl_id).into()); 965 let generics = generics(db.upcast(), (*impl_id).into());
967 Binders::new(generics.len(), Ty::Unknown) 966 Binders::new(generics.len(), Ty::Unknown)
968} 967}
969 968
970pub(crate) fn impl_trait_query( 969pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option<Binders<TraitRef>> {
971 db: &impl HirDatabase,
972 impl_id: ImplId,
973) -> Option<Binders<TraitRef>> {
974 let impl_data = db.impl_data(impl_id); 970 let impl_data = db.impl_data(impl_id);
975 let resolver = impl_id.resolver(db); 971 let resolver = impl_id.resolver(db.upcast());
976 let ctx = 972 let ctx =
977 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 973 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
978 let self_ty = db.impl_self_ty(impl_id); 974 let self_ty = db.impl_self_ty(impl_id);