From 5d121cdb45f5199828ed64a2ca01a74998e023ad Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 1 Mar 2021 14:24:00 +0100 Subject: Introduce Ty::Alias --- crates/hir/src/code_model.rs | 8 +-- crates/hir_ty/src/autoderef.rs | 5 +- crates/hir_ty/src/diagnostics/unsafe_check.rs | 2 +- crates/hir_ty/src/display.rs | 19 +++---- crates/hir_ty/src/infer.rs | 4 +- crates/hir_ty/src/infer/coerce.rs | 10 ++-- crates/hir_ty/src/infer/expr.rs | 2 +- crates/hir_ty/src/infer/unify.rs | 6 +-- crates/hir_ty/src/lib.rs | 76 +++++++++++++++------------ crates/hir_ty/src/lower.rs | 35 ++++++------ crates/hir_ty/src/method_resolution.rs | 8 +-- crates/hir_ty/src/traits.rs | 2 +- crates/hir_ty/src/traits/chalk.rs | 10 ++-- crates/hir_ty/src/traits/chalk/mapping.rs | 20 +++---- 14 files changed, 109 insertions(+), 98 deletions(-) diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 351ba75ff..00b0dc082 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -31,7 +31,7 @@ use hir_ty::{ display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, method_resolution, traits::{FnTrait, Solution, SolutionVariables}, - BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, + AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, Ty, TyDefId, TyVariableKind, }; @@ -1648,7 +1648,7 @@ impl Type { .build(); let predicate = ProjectionPredicate { projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst }, - ty: Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)), + ty: Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)), }; let goal = Canonical { value: InEnvironment::new( @@ -1709,7 +1709,7 @@ impl Type { } pub fn is_raw_ptr(&self) -> bool { - matches!(&self.ty.value, Ty::RawPtr(..)) + matches!(&self.ty.value, Ty::Raw(..)) } pub fn contains_unknown(&self) -> bool { @@ -1937,7 +1937,7 @@ impl Type { walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); } } - Ty::Opaque(opaque_ty) => { + Ty::Alias(AliasTy::Opaque(opaque_ty)) => { if let Some(bounds) = ty.impl_trait_bounds(db) { walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); } diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index 21d1e5446..be1fd1f13 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs @@ -81,7 +81,7 @@ fn deref_by_trait( // Now do the assoc type projection let projection = super::traits::ProjectionPredicate { - ty: Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())), + ty: Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())), projection_ty: super::ProjectionTy { associated_ty: target, parameters }, }; @@ -114,7 +114,8 @@ fn deref_by_trait( // new variables in that case for i in 1..vars.0.kinds.len() { - if vars.0.value[i - 1] != Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, i - 1)) + if vars.0.value[i - 1] + != Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i - 1)) { warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.value, solution); return None; diff --git a/crates/hir_ty/src/diagnostics/unsafe_check.rs b/crates/hir_ty/src/diagnostics/unsafe_check.rs index b439915c7..e77a20fea 100644 --- a/crates/hir_ty/src/diagnostics/unsafe_check.rs +++ b/crates/hir_ty/src/diagnostics/unsafe_check.rs @@ -110,7 +110,7 @@ fn walk_unsafe( } } Expr::UnaryOp { expr, op: UnaryOp::Deref } => { - if let Ty::RawPtr(..) = &infer[*expr] { + if let Ty::Raw(..) = &infer[*expr] { unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); } } diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index ff8211094..f3a4333cb 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -3,8 +3,9 @@ use std::{borrow::Cow, fmt}; use crate::{ - db::HirDatabase, primitive, utils::generics, CallableDefId, CallableSig, GenericPredicate, - Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, + db::HirDatabase, primitive, utils::generics, AliasTy, CallableDefId, CallableSig, + GenericPredicate, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, + TraitRef, Ty, }; use arrayvec::ArrayVec; use hir_def::{ @@ -284,12 +285,12 @@ impl HirDisplay for Ty { t.hir_fmt(f)?; write!(f, "; _]")?; } - Ty::RawPtr(m, parameters) | Ty::Ref(m, parameters) => { + Ty::Raw(m, parameters) | Ty::Ref(m, parameters) => { let t = parameters.as_single(); let ty_display = t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target); - if matches!(self, Ty::RawPtr(..)) { + if matches!(self, Ty::Raw(..)) { write!(f, "*{}", m.as_keyword_for_ptr())?; } else { write!(f, "&{}", m.as_keyword_for_ref())?; @@ -300,10 +301,10 @@ impl HirDisplay for Ty { Ty::Dyn(predicates) if predicates.len() > 1 => { Cow::Borrowed(predicates.as_ref()) } - &Ty::Opaque(OpaqueTy { + &Ty::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id: OpaqueTyId::ReturnTypeImplTrait(func, idx), ref parameters, - }) => { + })) => { datas = f.db.return_type_impl_traits(func).expect("impl trait id without data"); let data = (*datas) @@ -518,7 +519,6 @@ impl HirDisplay for Ty { write!(f, "{{closure}}")?; } } - Ty::Projection(p_ty) => p_ty.hir_fmt(f)?, Ty::Placeholder(id) => { let generics = generics(f.db.upcast(), id.parent); let param_data = &generics.params.types[id.local_id]; @@ -537,11 +537,12 @@ impl HirDisplay for Ty { } } } - Ty::Bound(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?, + Ty::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?, Ty::Dyn(predicates) => { write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?; } - Ty::Opaque(opaque_ty) => { + Ty::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?, + Ty::Alias(AliasTy::Opaque(opaque_ty)) => { match opaque_ty.opaque_ty_id { OpaqueTyId::ReturnTypeImplTrait(func, idx) => { let datas = diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index 1d78d1feb..18a4f5e8a 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -40,7 +40,7 @@ use super::{ InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, }; use crate::{ - db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, + db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, AliasTy, }; pub(crate) use unify::unify; @@ -395,7 +395,7 @@ impl<'a> InferenceContext<'a> { fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty { let ty = self.resolve_ty_as_possible(ty); ty.fold(&mut |ty| match ty { - Ty::Projection(proj_ty) => self.normalize_projection_ty(proj_ty), + Ty::Alias(AliasTy::Projection(proj_ty)) => self.normalize_projection_ty(proj_ty), _ => ty, }) } diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index 667b26a76..c33d8c61e 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs @@ -73,19 +73,19 @@ impl<'a> InferenceContext<'a> { match (&mut from_ty, to_ty) { // `*mut T` -> `*const T` // `&mut T` -> `&T` - (Ty::RawPtr(m1, ..), Ty::RawPtr(m2 @ Mutability::Shared, ..)) + (Ty::Raw(m1, ..), Ty::Raw(m2 @ Mutability::Shared, ..)) | (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Shared, ..)) => { *m1 = *m2; } // `&T` -> `*const T` // `&mut T` -> `*mut T`/`*const T` - (Ty::Ref(.., substs), &Ty::RawPtr(m2 @ Mutability::Shared, ..)) - | (Ty::Ref(Mutability::Mut, substs), &Ty::RawPtr(m2, ..)) => { - from_ty = Ty::RawPtr(m2, substs.clone()); + (Ty::Ref(.., substs), &Ty::Raw(m2 @ Mutability::Shared, ..)) + | (Ty::Ref(Mutability::Mut, substs), &Ty::Raw(m2, ..)) => { + from_ty = Ty::Raw(m2, substs.clone()); } // Illegal mutability conversion - (Ty::RawPtr(Mutability::Shared, ..), Ty::RawPtr(Mutability::Mut, ..)) + (Ty::Raw(Mutability::Shared, ..), Ty::Raw(Mutability::Mut, ..)) | (Ty::Ref(Mutability::Shared, ..), Ty::Ref(Mutability::Mut, ..)) => return false, // `{function_type}` -> `fn()` diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 928ad37a3..7852b3d23 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -479,7 +479,7 @@ impl<'a> InferenceContext<'a> { }; let inner_ty = self.infer_expr_inner(*expr, &expectation); match rawness { - Rawness::RawPtr => Ty::RawPtr(*mutability, Substs::single(inner_ty)), + Rawness::RawPtr => Ty::Raw(*mutability, Substs::single(inner_ty)), Rawness::Ref => Ty::Ref(*mutability, Substs::single(inner_ty)), } } diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index b481aa1b3..99a89a7f3 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -68,7 +68,7 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { } else { let root = self.ctx.table.var_unification_table.find(inner); let position = self.add(InferenceVar::from_inner(root), kind); - Ty::Bound(BoundVar::new(binders, position)) + Ty::BoundVar(BoundVar::new(binders, position)) } } _ => ty, @@ -110,7 +110,7 @@ impl Canonicalized { pub(super) fn decanonicalize_ty(&self, mut ty: Ty) -> Ty { ty.walk_mut_binders( &mut |ty, binders| { - if let &mut Ty::Bound(bound) = ty { + if let &mut Ty::BoundVar(bound) = ty { if bound.debruijn >= binders { let (v, k) = self.free_vars[bound.index]; *ty = Ty::InferenceVar(v, k); @@ -168,7 +168,7 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option { // (kind of hacky) for (i, var) in vars.iter().enumerate() { if &*table.resolve_ty_shallow(var) == var { - table.unify(var, &Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, i))); + table.unify(var, &Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i))); } } Some( diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 1131eaf92..9bcaf6fa7 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -111,6 +111,19 @@ pub struct FnPointer { pub substs: Substs, } +#[derive(Clone, PartialEq, Eq, Debug, Hash)] +pub enum AliasTy { + /// A "projection" type corresponds to an (unnormalized) + /// projection like `>::Foo`. Note that the + /// trait and all its parameters are fully known. + Projection(ProjectionTy), + /// An opaque type (`impl Trait`). + /// + /// This is currently only used for return type impl trait; each instance of + /// `impl Trait` in a return type gets its own ID. + Opaque(OpaqueTy), +} + /// A type. /// /// See also the `TyKind` enum in rustc (librustc/ty/sty.rs), which represents @@ -141,7 +154,7 @@ pub enum Ty { Slice(Substs), /// A raw pointer. Written as `*mut T` or `*const T` - RawPtr(Mutability, Substs), + Raw(Mutability, Substs), /// A reference; a pointer with an associated lifetime. Written as /// `&'a mut T` or `&'a T`. @@ -193,16 +206,11 @@ pub enum Ty { /// ``` Function(FnPointer), - /// A "projection" type corresponds to an (unnormalized) - /// projection like `>::Foo`. Note that the - /// trait and all its parameters are fully known. - Projection(ProjectionTy), - - /// An opaque type (`impl Trait`). - /// - /// This is currently only used for return type impl trait; each instance of - /// `impl Trait` in a return type gets its own ID. - Opaque(OpaqueTy), + /// An "alias" type represents some form of type alias, such as: + /// - An associated type projection like `::Item` + /// - `impl Trait` types + /// - Named type aliases like `type Foo = Vec` + Alias(AliasTy), /// A placeholder for a type parameter; for example, `T` in `fn f(x: T) /// {}` when we're type-checking the body of that function. In this @@ -215,7 +223,7 @@ pub enum Ty { /// parameters get turned into variables; during trait resolution, inference /// variables get turned into bound variables and back; and in `Dyn` the /// `Self` type is represented with a bound variable as well. - Bound(BoundVar), + BoundVar(BoundVar), /// A type variable used during type checking. InferenceVar(InferenceVar, TyVariableKind), @@ -299,7 +307,7 @@ impl Substs { generic_params .iter() .enumerate() - .map(|(idx, _)| Ty::Bound(BoundVar::new(debruijn, idx))) + .map(|(idx, _)| Ty::BoundVar(BoundVar::new(debruijn, idx))) .collect(), ) } @@ -347,7 +355,7 @@ impl SubstsBuilder { } pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { - self.fill((starting_from..).map(|idx| Ty::Bound(BoundVar::new(debruijn, idx)))) + self.fill((starting_from..).map(|idx| Ty::BoundVar(BoundVar::new(debruijn, idx)))) } pub fn fill_with_unknown(self) -> Self { @@ -627,7 +635,7 @@ impl Ty { Ty::Ref(mutability, parameters) => { Some((parameters.as_single(), Rawness::Ref, *mutability)) } - Ty::RawPtr(mutability, parameters) => { + Ty::Raw(mutability, parameters) => { Some((parameters.as_single(), Rawness::RawPtr, *mutability)) } _ => None, @@ -688,9 +696,7 @@ impl Ty { expr == expr2 && def == def2 } (Ty::Ref(mutability, ..), Ty::Ref(mutability2, ..)) - | (Ty::RawPtr(mutability, ..), Ty::RawPtr(mutability2, ..)) => { - mutability == mutability2 - } + | (Ty::Raw(mutability, ..), Ty::Raw(mutability2, ..)) => mutability == mutability2, ( Ty::Function(FnPointer { num_args, sig, .. }), Ty::Function(FnPointer { num_args: num_args2, sig: sig2, .. }), @@ -721,7 +727,7 @@ impl Ty { fn builtin_deref(&self) -> Option { match self { Ty::Ref(.., parameters) => Some(Ty::clone(parameters.as_single())), - Ty::RawPtr(.., parameters) => Some(Ty::clone(parameters.as_single())), + Ty::Raw(.., parameters) => Some(Ty::clone(parameters.as_single())), _ => None, } } @@ -757,7 +763,7 @@ impl Ty { Ty::Adt(_, substs) | Ty::Slice(substs) | Ty::Array(substs) - | Ty::RawPtr(_, substs) + | Ty::Raw(_, substs) | Ty::Ref(_, substs) | Ty::FnDef(_, substs) | Ty::Function(FnPointer { substs, .. }) @@ -780,7 +786,7 @@ impl Ty { Ty::Adt(_, substs) | Ty::Slice(substs) | Ty::Array(substs) - | Ty::RawPtr(_, substs) + | Ty::Raw(_, substs) | Ty::Ref(_, substs) | Ty::FnDef(_, substs) | Ty::Function(FnPointer { substs, .. }) @@ -797,7 +803,7 @@ impl Ty { Ty::Adt(_, substs) | Ty::Slice(substs) | Ty::Array(substs) - | Ty::RawPtr(_, substs) + | Ty::Raw(_, substs) | Ty::Ref(_, substs) | Ty::FnDef(_, substs) | Ty::Function(FnPointer { substs, .. }) @@ -834,7 +840,7 @@ impl Ty { OpaqueTyId::ReturnTypeImplTrait(..) => None, } } - Ty::Opaque(opaque_ty) => { + Ty::Alias(AliasTy::Opaque(opaque_ty)) => { let predicates = match opaque_ty.opaque_ty_id { OpaqueTyId::ReturnTypeImplTrait(func, idx) => { db.return_type_impl_traits(func).map(|it| { @@ -878,7 +884,7 @@ impl Ty { _ => None, } } - Ty::Projection(projection_ty) => { + Ty::Alias(AliasTy::Projection(projection_ty)) => { match projection_ty.associated_ty.lookup(db.upcast()).container { AssocContainerId::TraitId(trait_id) => Some(trait_id), _ => None, @@ -956,7 +962,7 @@ pub trait TypeWalk { { self.walk_mut_binders( &mut |ty, binders| { - if let &mut Ty::Bound(bound) = ty { + if let &mut Ty::BoundVar(bound) = ty { if bound.debruijn >= binders { *ty = substs.0[bound.index].clone().shift_bound_vars(binders); } @@ -974,8 +980,8 @@ pub trait TypeWalk { { self.fold_binders( &mut |ty, binders| match ty { - Ty::Bound(bound) if bound.debruijn >= binders => { - Ty::Bound(bound.shifted_in_from(n)) + Ty::BoundVar(bound) if bound.debruijn >= binders => { + Ty::BoundVar(bound.shifted_in_from(n)) } ty => ty, }, @@ -987,21 +993,21 @@ pub trait TypeWalk { impl TypeWalk for Ty { fn walk(&self, f: &mut impl FnMut(&Ty)) { match self { - Ty::Projection(p_ty) => { + Ty::Alias(AliasTy::Projection(p_ty)) => { for t in p_ty.parameters.iter() { t.walk(f); } } + Ty::Alias(AliasTy::Opaque(o_ty)) => { + for t in o_ty.parameters.iter() { + t.walk(f); + } + } Ty::Dyn(predicates) => { for p in predicates.iter() { p.walk(f); } } - Ty::Opaque(o_ty) => { - for t in o_ty.parameters.iter() { - t.walk(f); - } - } _ => { if let Some(substs) = self.substs() { for t in substs.iter() { @@ -1019,7 +1025,7 @@ impl TypeWalk for Ty { binders: DebruijnIndex, ) { match self { - Ty::Projection(p_ty) => { + Ty::Alias(AliasTy::Projection(p_ty)) => { p_ty.parameters.walk_mut_binders(f, binders); } Ty::Dyn(predicates) => { @@ -1027,7 +1033,7 @@ impl TypeWalk for Ty { p.walk_mut_binders(f, binders.shifted_in()); } } - Ty::Opaque(o_ty) => { + Ty::Alias(AliasTy::Opaque(o_ty)) => { o_ty.parameters.walk_mut_binders(f, binders); } _ => { diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 44bd95a9a..ca06c9fe2 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -31,8 +31,8 @@ use crate::{ all_super_trait_refs, associated_type_by_name_including_super_traits, generics, make_mut_slice, variant_data, }, - Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, GenericPredicate, OpaqueTy, - OpaqueTyId, PolyFnSig, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, + AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, GenericPredicate, + OpaqueTy, OpaqueTyId, PolyFnSig, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, ReturnTypeImplTraits, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, }; @@ -157,7 +157,7 @@ impl Ty { } TypeRef::RawPtr(inner, mutability) => { let inner_ty = Ty::from_hir(ctx, inner); - Ty::RawPtr(*mutability, Substs::single(inner_ty)) + Ty::Raw(*mutability, Substs::single(inner_ty)) } TypeRef::Array(inner) => { let inner_ty = Ty::from_hir(ctx, inner); @@ -181,7 +181,7 @@ impl Ty { }) } TypeRef::DynTrait(bounds) => { - let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); + let self_ty = Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)); let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { bounds .iter() @@ -225,7 +225,10 @@ impl Ty { let impl_trait_id = OpaqueTyId::ReturnTypeImplTrait(func, idx); let generics = generics(ctx.db.upcast(), func.into()); let parameters = Substs::bound_vars(&generics, ctx.in_binders); - Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) + Ty::Alias(AliasTy::Opaque(OpaqueTy { + opaque_ty_id: impl_trait_id, + parameters, + })) } ImplTraitLoweringMode::Param => { let idx = ctx.impl_trait_counter.get(); @@ -256,7 +259,7 @@ impl Ty { } else { (0, 0, 0, 0) }; - Ty::Bound(BoundVar::new( + Ty::BoundVar(BoundVar::new( ctx.in_binders, idx as usize + parent_params + self_params + list_params, )) @@ -328,7 +331,7 @@ impl Ty { TypeNs::TraitId(trait_) => { // if this is a bare dyn Trait, we'll directly put the required ^0 for the self type in there let self_ty = if remaining_segments.len() == 0 { - Some(Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0))) + Some(Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))) } else { None }; @@ -344,10 +347,10 @@ impl Ty { match found { Some((super_trait_ref, associated_ty)) => { // FIXME handle type parameters on the segment - Ty::Projection(ProjectionTy { + Ty::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters: super_trait_ref.substs, - }) + })) } None => { // FIXME: report error (associated type not found) @@ -371,7 +374,7 @@ impl Ty { TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), TypeParamLoweringMode::Variable => { let idx = generics.param_idx(param_id).expect("matching generics"); - Ty::Bound(BoundVar::new(ctx.in_binders, idx)) + Ty::BoundVar(BoundVar::new(ctx.in_binders, idx)) } } } @@ -469,10 +472,10 @@ impl Ty { // associated_type_shorthand_candidates does not do that let substs = substs.shift_bound_vars(ctx.in_binders); // FIXME handle type parameters on the segment - return Some(Ty::Projection(ProjectionTy { + return Some(Ty::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters: substs, - })); + }))); } None @@ -673,7 +676,7 @@ impl GenericPredicate { TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), TypeParamLoweringMode::Variable => { let idx = generics.param_idx(param_id).expect("matching generics"); - Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, idx)) + Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, idx)) } } } @@ -747,7 +750,7 @@ fn assoc_type_bindings_from_type_bound<'a>( preds.extend(GenericPredicate::from_type_bound( ctx, bound, - Ty::Projection(projection_ty.clone()), + Ty::Alias(AliasTy::Projection(projection_ty.clone())), )); } preds @@ -757,7 +760,7 @@ fn assoc_type_bindings_from_type_bound<'a>( impl ReturnTypeImplTrait { fn from_hir(ctx: &TyLoweringContext, bounds: &[TypeBound]) -> Self { mark::hit!(lower_rpit); - let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); + let self_ty = Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)); let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { bounds .iter() @@ -981,7 +984,7 @@ pub(crate) fn generic_defaults_query( // Each default can only refer to previous parameters. ty.walk_mut_binders( &mut |ty, binders| match ty { - Ty::Bound(BoundVar { debruijn, index }) if *debruijn == binders => { + Ty::BoundVar(BoundVar { debruijn, index }) if *debruijn == binders => { if *index >= idx { // type variable default referring to parameter coming // after it. This is forbidden (FIXME: report diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 8b1717873..dd5109d4e 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -51,7 +51,7 @@ impl TyFingerprint { &Ty::Scalar(scalar) => TyFingerprint::Scalar(scalar), &Ty::Adt(adt, _) => TyFingerprint::Adt(adt), &Ty::Tuple(cardinality, _) => TyFingerprint::Tuple(cardinality), - &Ty::RawPtr(mutability, ..) => TyFingerprint::RawPtr(mutability), + &Ty::Raw(mutability, ..) => TyFingerprint::RawPtr(mutability), &Ty::ForeignType(alias_id, ..) => TyFingerprint::ForeignType(alias_id), &Ty::Function(FnPointer { num_args, sig, .. }) => TyFingerprint::FnPtr(num_args, sig), Ty::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_))?, @@ -251,8 +251,8 @@ impl Ty { } Ty::Str => lang_item_crate!("str_alloc", "str"), Ty::Slice(_) => lang_item_crate!("slice_alloc", "slice"), - Ty::RawPtr(Mutability::Shared, _) => lang_item_crate!("const_ptr"), - Ty::RawPtr(Mutability::Mut, _) => lang_item_crate!("mut_ptr"), + Ty::Raw(Mutability::Shared, _) => lang_item_crate!("const_ptr"), + Ty::Raw(Mutability::Mut, _) => lang_item_crate!("mut_ptr"), Ty::Dyn(_) => { return self.dyn_trait().and_then(|trait_| { mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast())) @@ -683,7 +683,7 @@ pub(crate) fn inherent_impl_substs( fn fallback_bound_vars(s: Substs, num_vars_to_keep: usize) -> Substs { s.fold_binders( &mut |ty, binders| { - if let Ty::Bound(bound) = &ty { + if let Ty::BoundVar(bound) = &ty { if bound.index >= num_vars_to_keep && bound.debruijn >= binders { Ty::Unknown } else { diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs index dfa51896b..e4cdb6d53 100644 --- a/crates/hir_ty/src/traits.rs +++ b/crates/hir_ty/src/traits.rs @@ -129,7 +129,7 @@ pub(crate) fn trait_solve_query( log::info!("trait_solve_query({})", goal.value.value.display(db)); if let Obligation::Projection(pred) = &goal.value.value { - if let Ty::Bound(_) = &pred.projection_ty.parameters[0] { + if let Ty::BoundVar(_) = &pred.projection_ty.parameters[0] { // Hack: don't ask Chalk to normalize with an unknown self type, it'll say that's impossible return Some(Solution::Ambig(Guidance::Unknown)); } diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 3f5f5091f..e513fa8f4 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -90,7 +90,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { ty: &Ty, binders: &CanonicalVarKinds, ) -> Option { - if let Ty::Bound(bv) = ty { + if let Ty::BoundVar(bv) = ty { let binders = binders.as_slice(&Interner); if bv.debruijn == DebruijnIndex::INNERMOST { if let chalk_ir::VariableKind::Ty(tk) = binders[bv.index].kind { @@ -220,18 +220,18 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { let impl_bound = GenericPredicate::Implemented(TraitRef { trait_: future_trait, // Self type as the first parameter. - substs: Substs::single(Ty::Bound(BoundVar { + substs: Substs::single(Ty::BoundVar(BoundVar { debruijn: DebruijnIndex::INNERMOST, index: 0, })), }); let proj_bound = GenericPredicate::Projection(ProjectionPredicate { // The parameter of the opaque type. - ty: Ty::Bound(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }), + ty: Ty::BoundVar(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }), projection_ty: ProjectionTy { associated_ty: future_output, // Self type as the first parameter. - parameters: Substs::single(Ty::Bound(BoundVar::new( + parameters: Substs::single(Ty::BoundVar(BoundVar::new( DebruijnIndex::INNERMOST, 0, ))), @@ -392,7 +392,7 @@ pub(crate) fn associated_ty_data_query( let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); let ctx = crate::TyLoweringContext::new(db, &resolver) .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); - let self_ty = Ty::Bound(crate::BoundVar::new(crate::DebruijnIndex::INNERMOST, 0)); + let self_ty = Ty::BoundVar(crate::BoundVar::new(crate::DebruijnIndex::INNERMOST, 0)); let bounds = type_alias_data .bounds .iter() diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 995ff6a9a..6e6055d80 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -16,8 +16,8 @@ use crate::{ db::HirDatabase, primitive::UintTy, traits::{Canonical, Obligation}, - CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, - ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, + AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, + OpaqueTyId, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, }; use super::interner::*; @@ -63,7 +63,7 @@ impl ToChalk for Ty { let substitution = substs.to_chalk(db); chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) } - Ty::RawPtr(mutability, substs) => { + Ty::Raw(mutability, substs) => { let ty = substs[0].clone().to_chalk(db); chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner) } @@ -88,7 +88,7 @@ impl ToChalk for Ty { let substitution = substs.to_chalk(db); chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner) } - Ty::Projection(proj_ty) => { + Ty::Alias(AliasTy::Projection(proj_ty)) => { let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); let substitution = proj_ty.parameters.to_chalk(db); chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { @@ -106,7 +106,7 @@ impl ToChalk for Ty { } .to_ty::(&Interner) } - Ty::Bound(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), + Ty::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), Ty::InferenceVar(..) => panic!("uncanonicalized infer ty"), Ty::Dyn(predicates) => { let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( @@ -119,7 +119,7 @@ impl ToChalk for Ty { }; chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) } - Ty::Opaque(opaque_ty) => { + Ty::Alias(AliasTy::Opaque(opaque_ty)) => { let opaque_ty_id = opaque_ty.opaque_ty_id.to_chalk(db); let substitution = opaque_ty.parameters.to_chalk(db); chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { @@ -146,12 +146,12 @@ impl ToChalk for Ty { let associated_ty = from_chalk::(db, proj.associated_ty_id).0; let parameters = from_chalk(db, proj.substitution); - Ty::Projection(ProjectionTy { associated_ty, parameters }) + Ty::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters })) } chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { let impl_trait_id = from_chalk(db, opaque_ty.opaque_ty_id); let parameters = from_chalk(db, opaque_ty.substitution); - Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) + Ty::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters })) } chalk_ir::TyKind::Function(chalk_ir::FnPointer { num_binders, @@ -170,7 +170,7 @@ impl ToChalk for Ty { substs, }) } - chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx), + chalk_ir::TyKind::BoundVar(idx) => Ty::BoundVar(idx), chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown, chalk_ir::TyKind::Dyn(where_clauses) => { assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); @@ -198,7 +198,7 @@ impl ToChalk for Ty { Ty::Tuple(cardinality, from_chalk(db, subst)) } chalk_ir::TyKind::Raw(mutability, ty) => { - Ty::RawPtr(from_chalk(db, mutability), Substs::single(from_chalk(db, ty))) + Ty::Raw(from_chalk(db, mutability), Substs::single(from_chalk(db, ty))) } chalk_ir::TyKind::Slice(ty) => Ty::Slice(Substs::single(from_chalk(db, ty))), chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { -- cgit v1.2.3