aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/lower.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/lower.rs')
-rw-r--r--crates/hir_ty/src/lower.rs45
1 files changed, 28 insertions, 17 deletions
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs
index 44bd95a9a..1b5843d48 100644
--- a/crates/hir_ty/src/lower.rs
+++ b/crates/hir_ty/src/lower.rs
@@ -8,6 +8,7 @@
8use std::{iter, sync::Arc}; 8use std::{iter, sync::Arc};
9 9
10use base_db::CrateId; 10use base_db::CrateId;
11use chalk_ir::Mutability;
11use hir_def::{ 12use hir_def::{
12 adt::StructKind, 13 adt::StructKind,
13 builtin_type::BuiltinType, 14 builtin_type::BuiltinType,
@@ -31,8 +32,8 @@ use crate::{
31 all_super_trait_refs, associated_type_by_name_including_super_traits, generics, 32 all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
32 make_mut_slice, variant_data, 33 make_mut_slice, variant_data,
33 }, 34 },
34 Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, GenericPredicate, OpaqueTy, 35 AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, GenericPredicate,
35 OpaqueTyId, PolyFnSig, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, 36 OpaqueTy, OpaqueTyId, PolyFnSig, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait,
36 ReturnTypeImplTraits, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, 37 ReturnTypeImplTraits, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk,
37}; 38};
38 39
@@ -157,7 +158,7 @@ impl Ty {
157 } 158 }
158 TypeRef::RawPtr(inner, mutability) => { 159 TypeRef::RawPtr(inner, mutability) => {
159 let inner_ty = Ty::from_hir(ctx, inner); 160 let inner_ty = Ty::from_hir(ctx, inner);
160 Ty::RawPtr(*mutability, Substs::single(inner_ty)) 161 Ty::Raw(lower_to_chalk_mutability(*mutability), Substs::single(inner_ty))
161 } 162 }
162 TypeRef::Array(inner) => { 163 TypeRef::Array(inner) => {
163 let inner_ty = Ty::from_hir(ctx, inner); 164 let inner_ty = Ty::from_hir(ctx, inner);
@@ -169,7 +170,7 @@ impl Ty {
169 } 170 }
170 TypeRef::Reference(inner, _, mutability) => { 171 TypeRef::Reference(inner, _, mutability) => {
171 let inner_ty = Ty::from_hir(ctx, inner); 172 let inner_ty = Ty::from_hir(ctx, inner);
172 Ty::Ref(*mutability, Substs::single(inner_ty)) 173 Ty::Ref(lower_to_chalk_mutability(*mutability), Substs::single(inner_ty))
173 } 174 }
174 TypeRef::Placeholder => Ty::Unknown, 175 TypeRef::Placeholder => Ty::Unknown,
175 TypeRef::Fn(params, is_varargs) => { 176 TypeRef::Fn(params, is_varargs) => {
@@ -181,7 +182,7 @@ impl Ty {
181 }) 182 })
182 } 183 }
183 TypeRef::DynTrait(bounds) => { 184 TypeRef::DynTrait(bounds) => {
184 let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); 185 let self_ty = Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0));
185 let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { 186 let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| {
186 bounds 187 bounds
187 .iter() 188 .iter()
@@ -225,7 +226,10 @@ impl Ty {
225 let impl_trait_id = OpaqueTyId::ReturnTypeImplTrait(func, idx); 226 let impl_trait_id = OpaqueTyId::ReturnTypeImplTrait(func, idx);
226 let generics = generics(ctx.db.upcast(), func.into()); 227 let generics = generics(ctx.db.upcast(), func.into());
227 let parameters = Substs::bound_vars(&generics, ctx.in_binders); 228 let parameters = Substs::bound_vars(&generics, ctx.in_binders);
228 Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) 229 Ty::Alias(AliasTy::Opaque(OpaqueTy {
230 opaque_ty_id: impl_trait_id,
231 parameters,
232 }))
229 } 233 }
230 ImplTraitLoweringMode::Param => { 234 ImplTraitLoweringMode::Param => {
231 let idx = ctx.impl_trait_counter.get(); 235 let idx = ctx.impl_trait_counter.get();
@@ -256,7 +260,7 @@ impl Ty {
256 } else { 260 } else {
257 (0, 0, 0, 0) 261 (0, 0, 0, 0)
258 }; 262 };
259 Ty::Bound(BoundVar::new( 263 Ty::BoundVar(BoundVar::new(
260 ctx.in_binders, 264 ctx.in_binders,
261 idx as usize + parent_params + self_params + list_params, 265 idx as usize + parent_params + self_params + list_params,
262 )) 266 ))
@@ -328,7 +332,7 @@ impl Ty {
328 TypeNs::TraitId(trait_) => { 332 TypeNs::TraitId(trait_) => {
329 // if this is a bare dyn Trait, we'll directly put the required ^0 for the self type in there 333 // if this is a bare dyn Trait, we'll directly put the required ^0 for the self type in there
330 let self_ty = if remaining_segments.len() == 0 { 334 let self_ty = if remaining_segments.len() == 0 {
331 Some(Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0))) 335 Some(Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)))
332 } else { 336 } else {
333 None 337 None
334 }; 338 };
@@ -344,10 +348,10 @@ impl Ty {
344 match found { 348 match found {
345 Some((super_trait_ref, associated_ty)) => { 349 Some((super_trait_ref, associated_ty)) => {
346 // FIXME handle type parameters on the segment 350 // FIXME handle type parameters on the segment
347 Ty::Projection(ProjectionTy { 351 Ty::Alias(AliasTy::Projection(ProjectionTy {
348 associated_ty, 352 associated_ty,
349 parameters: super_trait_ref.substs, 353 parameters: super_trait_ref.substs,
350 }) 354 }))
351 } 355 }
352 None => { 356 None => {
353 // FIXME: report error (associated type not found) 357 // FIXME: report error (associated type not found)
@@ -371,7 +375,7 @@ impl Ty {
371 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), 375 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id),
372 TypeParamLoweringMode::Variable => { 376 TypeParamLoweringMode::Variable => {
373 let idx = generics.param_idx(param_id).expect("matching generics"); 377 let idx = generics.param_idx(param_id).expect("matching generics");
374 Ty::Bound(BoundVar::new(ctx.in_binders, idx)) 378 Ty::BoundVar(BoundVar::new(ctx.in_binders, idx))
375 } 379 }
376 } 380 }
377 } 381 }
@@ -469,10 +473,10 @@ impl Ty {
469 // associated_type_shorthand_candidates does not do that 473 // associated_type_shorthand_candidates does not do that
470 let substs = substs.shift_bound_vars(ctx.in_binders); 474 let substs = substs.shift_bound_vars(ctx.in_binders);
471 // FIXME handle type parameters on the segment 475 // FIXME handle type parameters on the segment
472 return Some(Ty::Projection(ProjectionTy { 476 return Some(Ty::Alias(AliasTy::Projection(ProjectionTy {
473 associated_ty, 477 associated_ty,
474 parameters: substs, 478 parameters: substs,
475 })); 479 })));
476 } 480 }
477 481
478 None 482 None
@@ -673,7 +677,7 @@ impl GenericPredicate {
673 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), 677 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id),
674 TypeParamLoweringMode::Variable => { 678 TypeParamLoweringMode::Variable => {
675 let idx = generics.param_idx(param_id).expect("matching generics"); 679 let idx = generics.param_idx(param_id).expect("matching generics");
676 Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, idx)) 680 Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, idx))
677 } 681 }
678 } 682 }
679 } 683 }
@@ -747,7 +751,7 @@ fn assoc_type_bindings_from_type_bound<'a>(
747 preds.extend(GenericPredicate::from_type_bound( 751 preds.extend(GenericPredicate::from_type_bound(
748 ctx, 752 ctx,
749 bound, 753 bound,
750 Ty::Projection(projection_ty.clone()), 754 Ty::Alias(AliasTy::Projection(projection_ty.clone())),
751 )); 755 ));
752 } 756 }
753 preds 757 preds
@@ -757,7 +761,7 @@ fn assoc_type_bindings_from_type_bound<'a>(
757impl ReturnTypeImplTrait { 761impl ReturnTypeImplTrait {
758 fn from_hir(ctx: &TyLoweringContext, bounds: &[TypeBound]) -> Self { 762 fn from_hir(ctx: &TyLoweringContext, bounds: &[TypeBound]) -> Self {
759 mark::hit!(lower_rpit); 763 mark::hit!(lower_rpit);
760 let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); 764 let self_ty = Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0));
761 let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { 765 let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| {
762 bounds 766 bounds
763 .iter() 767 .iter()
@@ -981,7 +985,7 @@ pub(crate) fn generic_defaults_query(
981 // Each default can only refer to previous parameters. 985 // Each default can only refer to previous parameters.
982 ty.walk_mut_binders( 986 ty.walk_mut_binders(
983 &mut |ty, binders| match ty { 987 &mut |ty, binders| match ty {
984 Ty::Bound(BoundVar { debruijn, index }) if *debruijn == binders => { 988 Ty::BoundVar(BoundVar { debruijn, index }) if *debruijn == binders => {
985 if *index >= idx { 989 if *index >= idx {
986 // type variable default referring to parameter coming 990 // type variable default referring to parameter coming
987 // after it. This is forbidden (FIXME: report 991 // after it. This is forbidden (FIXME: report
@@ -1256,3 +1260,10 @@ pub(crate) fn return_type_impl_traits(
1256 Some(Arc::new(Binders::new(num_binders, return_type_impl_traits))) 1260 Some(Arc::new(Binders::new(num_binders, return_type_impl_traits)))
1257 } 1261 }
1258} 1262}
1263
1264pub(crate) fn lower_to_chalk_mutability(m: hir_def::type_ref::Mutability) -> Mutability {
1265 match m {
1266 hir_def::type_ref::Mutability::Shared => Mutability::Not,
1267 hir_def::type_ref::Mutability::Mut => Mutability::Mut,
1268 }
1269}