From 77d974ae6b73f8b733f12e18d220fa8c4fc1ee38 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 17:40:56 +0200 Subject: Replace all the types by their Chalk versions --- crates/hir_ty/src/lib.rs | 35 ++- crates/hir_ty/src/types.rs | 549 --------------------------------------------- crates/hir_ty/src/walk.rs | 2 +- 3 files changed, 32 insertions(+), 554 deletions(-) delete mode 100644 crates/hir_ty/src/types.rs (limited to 'crates') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 87f10e9d5..3378015ca 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -17,7 +17,6 @@ mod chalk_cast; mod chalk_ext; mod builder; mod walk; -mod types; pub mod display; pub mod db; @@ -48,7 +47,6 @@ pub use lower::{ TyDefId, TyLoweringContext, ValueTyDefId, }; pub use traits::{chalk::Interner, TraitEnvironment}; -pub use types::*; pub use walk::TypeWalk; pub use chalk_ir::{ @@ -65,6 +63,21 @@ pub type PlaceholderIndex = chalk_ir::PlaceholderIndex; pub type VariableKind = chalk_ir::VariableKind; pub type VariableKinds = chalk_ir::VariableKinds; pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds; +pub type Binders = chalk_ir::Binders; +pub type Substitution = chalk_ir::Substitution; +pub type GenericArg = chalk_ir::GenericArg; +pub type GenericArgData = chalk_ir::GenericArgData; + +pub type Ty = chalk_ir::Ty; +pub type TyKind = chalk_ir::TyKind; +pub type DynTy = chalk_ir::DynTy; +pub type FnPointer = chalk_ir::FnPointer; +// pub type FnSubst = chalk_ir::FnSubst; +pub use chalk_ir::FnSubst; +pub type ProjectionTy = chalk_ir::ProjectionTy; +pub type AliasTy = chalk_ir::AliasTy; +pub type OpaqueTy = chalk_ir::OpaqueTy; +pub type InferenceVar = chalk_ir::InferenceVar; pub type Lifetime = chalk_ir::Lifetime; pub type LifetimeData = chalk_ir::LifetimeData; @@ -79,6 +92,14 @@ pub type ChalkTraitId = chalk_ir::TraitId; pub type FnSig = chalk_ir::FnSig; +pub type InEnvironment = chalk_ir::InEnvironment; +pub type DomainGoal = chalk_ir::DomainGoal; +pub type AliasEq = chalk_ir::AliasEq; +pub type Solution = chalk_solve::Solution; +pub type ConstrainedSubst = chalk_ir::ConstrainedSubst; +pub type Guidance = chalk_solve::Guidance; +pub type WhereClause = chalk_ir::WhereClause; + // FIXME: get rid of this pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution { Substitution::intern(s.interned()[..std::cmp::min(s.len(&Interner), n)].into()) @@ -121,6 +142,14 @@ pub fn make_canonical( Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } } +pub type TraitRef = chalk_ir::TraitRef; + +pub type QuantifiedWhereClause = Binders; + +pub type QuantifiedWhereClauses = chalk_ir::QuantifiedWhereClauses; + +pub type Canonical = chalk_ir::Canonical; + /// A function signature as seen by type inference: Several parameter types and /// one return type. #[derive(Clone, PartialEq, Eq, Debug)] @@ -164,8 +193,6 @@ impl CallableSig { } } -impl Ty {} - #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum ImplTraitId { ReturnTypeImplTrait(hir_def::FunctionId, u16), diff --git a/crates/hir_ty/src/types.rs b/crates/hir_ty/src/types.rs deleted file mode 100644 index 89adad108..000000000 --- a/crates/hir_ty/src/types.rs +++ /dev/null @@ -1,549 +0,0 @@ -//! This is the home of `Ty` etc. until they get replaced by their chalk_ir -//! equivalents. - -use std::sync::Arc; - -use chalk_ir::{ - cast::{Cast, CastTo, Caster}, - BoundVar, Mutability, Scalar, TyVariableKind, -}; -use smallvec::SmallVec; - -use crate::{ - AssocTypeId, CanonicalVarKinds, ChalkTraitId, ClosureId, Const, FnDefId, FnSig, ForeignDefId, - Interner, Lifetime, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKind, VariableKinds, -}; - -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub struct OpaqueTy { - pub opaque_ty_id: OpaqueTyId, - pub substitution: Substitution, -} - -/// A "projection" type corresponds to an (unnormalized) -/// projection like `>::Foo`. Note that the -/// trait and all its parameters are fully known. -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub struct ProjectionTy { - pub associated_ty_id: AssocTypeId, - pub substitution: Substitution, -} - -impl ProjectionTy { - pub fn self_type_parameter(&self, interner: &Interner) -> Ty { - self.substitution.interned()[0].assert_ty_ref(interner).clone() - } -} - -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub struct DynTy { - /// The unknown self type. - pub bounds: Binders, - pub lifetime: Lifetime, -} - -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub struct FnPointer { - pub num_binders: usize, - pub sig: FnSig, - pub substitution: FnSubst, -} -/// A wrapper for the substs on a Fn. -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub struct FnSubst(pub Substitution); - -impl FnPointer { - /// Represent the current `Fn` as if it was wrapped in `Binders` - pub fn into_binders(self, interner: &Interner) -> Binders { - Binders::new( - VariableKinds::from_iter( - interner, - (0..self.num_binders).map(|_| VariableKind::Lifetime), - ), - self.substitution, - ) - } - - /// Represent the current `Fn` as if it was wrapped in `Binders` - pub fn as_binders(&self, interner: &Interner) -> Binders<&FnSubst> { - Binders::new( - VariableKinds::from_iter( - interner, - (0..self.num_binders).map(|_| VariableKind::Lifetime), - ), - &self.substitution, - ) - } -} - -#[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 -/// the same thing (but in a different way). -/// -/// This should be cheap to clone. -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub enum TyKind { - /// Structures, enumerations and unions. - Adt(chalk_ir::AdtId, Substitution), - - /// Represents an associated item like `Iterator::Item`. This is used - /// when we have tried to normalize a projection like `T::Item` but - /// couldn't find a better representation. In that case, we generate - /// an **application type** like `(Iterator::Item)`. - AssociatedType(AssocTypeId, Substitution), - - /// a scalar type like `bool` or `u32` - Scalar(Scalar), - - /// A tuple type. For example, `(i32, bool)`. - Tuple(usize, Substitution), - - /// An array with the given length. Written as `[T; n]`. - Array(Ty, Const), - - /// The pointee of an array slice. Written as `[T]`. - Slice(Ty), - - /// A raw pointer. Written as `*mut T` or `*const T` - Raw(Mutability, Ty), - - /// A reference; a pointer with an associated lifetime. Written as - /// `&'a mut T` or `&'a T`. - Ref(Mutability, Lifetime, Ty), - - /// This represents a placeholder for an opaque type in situations where we - /// don't know the hidden type (i.e. currently almost always). This is - /// analogous to the `AssociatedType` type constructor. - /// It is also used as the type of async block, with one type parameter - /// representing the Future::Output type. - OpaqueType(OpaqueTyId, Substitution), - - /// The anonymous type of a function declaration/definition. Each - /// function has a unique type, which is output (for a function - /// named `foo` returning an `i32`) as `fn() -> i32 {foo}`. - /// - /// This includes tuple struct / enum variant constructors as well. - /// - /// For example the type of `bar` here: - /// - /// ``` - /// fn foo() -> i32 { 1 } - /// let bar = foo; // bar: fn() -> i32 {foo} - /// ``` - FnDef(FnDefId, Substitution), - - /// The pointee of a string slice. Written as `str`. - Str, - - /// The never type `!`. - Never, - - /// The type of a specific closure. - /// - /// The closure signature is stored in a `FnPtr` type in the first type - /// parameter. - Closure(ClosureId, Substitution), - - /// Represents a foreign type declared in external blocks. - Foreign(ForeignDefId), - - /// A pointer to a function. Written as `fn() -> i32`. - /// - /// For example the type of `bar` here: - /// - /// ``` - /// fn foo() -> i32 { 1 } - /// let bar: fn() -> i32 = foo; - /// ``` - Function(FnPointer), - - /// 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 - /// situation, we know this stands for *some* type, but don't know the exact - /// type. - Placeholder(PlaceholderIndex), - - /// A bound type variable. This is used in various places: when representing - /// some polymorphic type like the type of function `fn f`, the type - /// 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. - BoundVar(BoundVar), - - /// A type variable used during type checking. - InferenceVar(InferenceVar, TyVariableKind), - - /// A trait object (`dyn Trait` or bare `Trait` in pre-2018 Rust). - /// - /// The predicates are quantified over the `Self` type, i.e. `Ty::Bound(0)` - /// represents the `Self` type inside the bounds. This is currently - /// implicit; Chalk has the `Binders` struct to make it explicit, but it - /// didn't seem worth the overhead yet. - Dyn(DynTy), - - /// A placeholder for a type which could not be computed; this is propagated - /// to avoid useless error messages. Doubles as a placeholder where type - /// variables are inserted before type checking, since we want to try to - /// infer a better type here anyway -- for the IDE use case, we want to try - /// to infer as much as possible even in the presence of type errors. - Error, -} - -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub struct Ty(Arc); - -impl TyKind { - pub fn intern(self, _interner: &Interner) -> Ty { - Ty(Arc::new(self)) - } -} - -impl Ty { - pub fn kind(&self, _interner: &Interner) -> &TyKind { - &self.0 - } - - pub fn interned_mut(&mut self) -> &mut TyKind { - Arc::make_mut(&mut self.0) - } - - pub fn into_inner(self) -> TyKind { - Arc::try_unwrap(self.0).unwrap_or_else(|a| (*a).clone()) - } -} - -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub struct GenericArg { - interned: GenericArgData, -} - -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub enum GenericArgData { - Ty(Ty), -} - -impl GenericArg { - /// Constructs a generic argument using `GenericArgData`. - pub fn new(_interner: &Interner, data: GenericArgData) -> Self { - GenericArg { interned: data } - } - - /// Gets the interned value. - pub fn interned(&self) -> &GenericArgData { - &self.interned - } - - /// Asserts that this is a type argument. - pub fn assert_ty_ref(&self, interner: &Interner) -> &Ty { - self.ty(interner).unwrap() - } - - /// Checks whether the generic argument is a type. - pub fn is_ty(&self, _interner: &Interner) -> bool { - match self.interned() { - GenericArgData::Ty(_) => true, - } - } - - /// Returns the type if it is one, `None` otherwise. - pub fn ty(&self, _interner: &Interner) -> Option<&Ty> { - match self.interned() { - GenericArgData::Ty(t) => Some(t), - } - } - - pub fn interned_mut(&mut self) -> &mut GenericArgData { - &mut self.interned - } -} - -/// A list of substitutions for generic parameters. -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub struct Substitution(SmallVec<[GenericArg; 2]>); - -impl Substitution { - pub fn interned(&self) -> &SmallVec<[GenericArg; 2]> { - &self.0 - } - - pub fn len(&self, _: &Interner) -> usize { - self.0.len() - } - - pub fn is_empty(&self, _: &Interner) -> bool { - self.0.is_empty() - } - - pub fn at(&self, _: &Interner, i: usize) -> &GenericArg { - &self.0[i] - } - - pub fn empty(_: &Interner) -> Substitution { - Substitution(SmallVec::new()) - } - - pub fn iter(&self, _: &Interner) -> std::slice::Iter<'_, GenericArg> { - self.0.iter() - } - - pub fn from1(_interner: &Interner, ty: Ty) -> Substitution { - Substitution::intern({ - let mut v = SmallVec::new(); - v.push(ty.cast(&Interner)); - v - }) - } - - pub fn from_iter( - interner: &Interner, - elements: impl IntoIterator>, - ) -> Self { - Substitution(elements.into_iter().casted(interner).collect()) - } - - pub fn apply(&self, value: T, _interner: &Interner) -> T { - value.subst_bound_vars(self) - } - - // Temporary helper functions, to be removed - pub fn intern(interned: SmallVec<[GenericArg; 2]>) -> Substitution { - Substitution(interned) - } - - pub fn interned_mut(&mut self) -> &mut SmallVec<[GenericArg; 2]> { - &mut self.0 - } -} - -#[derive(Clone, PartialEq, Eq, Hash)] -pub struct Binders { - /// The binders that quantify over the value. - pub binders: VariableKinds, - value: T, -} - -impl Binders { - pub fn new(binders: VariableKinds, value: T) -> Self { - Self { binders, value } - } - - pub fn empty(_interner: &Interner, value: T) -> Self { - crate::make_only_type_binders(0, value) - } - - pub fn as_ref(&self) -> Binders<&T> { - Binders { binders: self.binders.clone(), value: &self.value } - } - - pub fn map(self, f: impl FnOnce(T) -> U) -> Binders { - Binders { binders: self.binders, value: f(self.value) } - } - - pub fn filter_map(self, f: impl FnOnce(T) -> Option) -> Option> { - Some(Binders { binders: self.binders, value: f(self.value)? }) - } - - pub fn skip_binders(&self) -> &T { - &self.value - } - - pub fn into_value_and_skipped_binders(self) -> (T, VariableKinds) { - (self.value, self.binders) - } - - /// Returns the number of binders. - pub fn len(&self, interner: &Interner) -> usize { - self.binders.len(interner) - } - - // Temporary helper function, to be removed - pub fn skip_binders_mut(&mut self) -> &mut T { - &mut self.value - } -} - -impl Binders<&T> { - pub fn cloned(&self) -> Binders { - Binders::new(self.binders.clone(), self.value.clone()) - } -} - -impl Binders { - /// Substitutes all variables. - pub fn substitute(self, interner: &Interner, subst: &Substitution) -> T { - let (value, binders) = self.into_value_and_skipped_binders(); - assert_eq!(subst.len(interner), binders.len(interner)); - value.subst_bound_vars(subst) - } -} - -impl std::fmt::Debug for Binders { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - let Binders { ref binders, ref value } = *self; - write!(fmt, "for{:?} ", binders.inner_debug(&Interner))?; - std::fmt::Debug::fmt(value, fmt) - } -} - -/// A trait with type parameters. This includes the `Self`, so this represents a concrete type implementing the trait. -#[derive(Clone, PartialEq, Eq, Debug, Hash)] -pub struct TraitRef { - pub trait_id: ChalkTraitId, - pub substitution: Substitution, -} - -impl TraitRef { - pub fn self_type_parameter(&self, interner: &Interner) -> Ty { - self.substitution.at(interner, 0).assert_ty_ref(interner).clone() - } -} - -/// Like `generics::WherePredicate`, but with resolved types: A condition on the -/// parameters of a generic item. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum WhereClause { - /// The given trait needs to be implemented for its type parameters. - Implemented(TraitRef), - /// An associated type bindings like in `Iterator`. - AliasEq(AliasEq), -} - -pub type QuantifiedWhereClause = Binders; - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct QuantifiedWhereClauses(Arc<[QuantifiedWhereClause]>); - -impl QuantifiedWhereClauses { - pub fn from_iter( - _interner: &Interner, - elements: impl IntoIterator, - ) -> Self { - QuantifiedWhereClauses(elements.into_iter().collect()) - } - - pub fn interned(&self) -> &Arc<[QuantifiedWhereClause]> { - &self.0 - } - - pub fn interned_mut(&mut self) -> &mut Arc<[QuantifiedWhereClause]> { - &mut self.0 - } -} - -/// Basically a claim (currently not validated / checked) that the contained -/// type / trait ref contains no inference variables; any inference variables it -/// contained have been replaced by bound variables, and `kinds` tells us how -/// many there are and whether they were normal or float/int variables. This is -/// used to erase irrelevant differences between types before using them in -/// queries. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Canonical { - pub value: T, - pub binders: CanonicalVarKinds, -} - -/// Something (usually a goal), along with an environment. -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct InEnvironment { - pub environment: chalk_ir::Environment, - pub goal: T, -} - -impl InEnvironment { - pub fn new(environment: &chalk_ir::Environment, value: T) -> InEnvironment { - InEnvironment { environment: environment.clone(), goal: value } - } -} - -/// Something that needs to be proven (by Chalk) during type checking, e.g. that -/// a certain type implements a certain trait. Proving the Obligation might -/// result in additional information about inference variables. -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub enum DomainGoal { - Holds(WhereClause), -} - -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct AliasEq { - pub alias: AliasTy, - pub ty: Ty, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct ConstrainedSubst { - pub subst: Substitution, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -/// A (possible) solution for a proposed goal. -pub enum Solution { - /// The goal indeed holds, and there is a unique value for all existential - /// variables. - Unique(Canonical), - - /// The goal may be provable in multiple ways, but regardless we may have some guidance - /// for type inference. In this case, we don't return any lifetime - /// constraints, since we have not "committed" to any particular solution - /// yet. - Ambig(Guidance), -} - -#[derive(Clone, Debug, PartialEq, Eq)] -/// When a goal holds ambiguously (e.g., because there are multiple possible -/// solutions), we issue a set of *guidance* back to type inference. -pub enum Guidance { - /// The existential variables *must* have the given values if the goal is - /// ever to hold, but that alone isn't enough to guarantee the goal will - /// actually hold. - Definite(Canonical), - - /// There are multiple plausible values for the existentials, but the ones - /// here are suggested as the preferred choice heuristically. These should - /// be used for inference fallback only. - Suggested(Canonical), - - /// There's no useful information to feed back to type inference - Unknown, -} - -/// The kinds of placeholders we need during type inference. There's separate -/// values for general types, and for integer and float variables. The latter -/// two are used for inference of literal values (e.g. `100` could be one of -/// several integer types). -#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] -pub struct InferenceVar { - index: u32, -} - -impl From for InferenceVar { - fn from(index: u32) -> InferenceVar { - InferenceVar { index } - } -} - -impl InferenceVar { - /// Gets the underlying index value. - pub fn index(self) -> u32 { - self.index - } -} diff --git a/crates/hir_ty/src/walk.rs b/crates/hir_ty/src/walk.rs index 91116dcda..873d563c0 100644 --- a/crates/hir_ty/src/walk.rs +++ b/crates/hir_ty/src/walk.rs @@ -408,7 +408,7 @@ impl TypeWalk for AliasEq { } } -impl TypeWalk for FnSubst { +impl TypeWalk for FnSubst { fn walk(&self, f: &mut impl FnMut(&Ty)) { self.0.walk(f) } -- cgit v1.2.3 From 429bbbd39a7bcb8650b522a9d683d20d76269770 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 17:41:14 +0200 Subject: Make ToChalk implementations identity --- crates/hir_ty/src/traits.rs | 15 +- crates/hir_ty/src/traits/chalk/mapping.rs | 299 ++++-------------------------- 2 files changed, 42 insertions(+), 272 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs index 7d87741b8..cc1d2ca18 100644 --- a/crates/hir_ty/src/traits.rs +++ b/crates/hir_ty/src/traits.rs @@ -173,20 +173,7 @@ fn solution_from_chalk( db: &dyn HirDatabase, solution: chalk_solve::Solution, ) -> Solution { - match solution { - chalk_solve::Solution::Unique(constr_subst) => { - Solution::Unique(from_chalk(db, constr_subst)) - } - chalk_solve::Solution::Ambig(chalk_solve::Guidance::Definite(subst)) => { - Solution::Ambig(Guidance::Definite(from_chalk(db, subst))) - } - chalk_solve::Solution::Ambig(chalk_solve::Guidance::Suggested(subst)) => { - Solution::Ambig(Guidance::Suggested(from_chalk(db, subst))) - } - chalk_solve::Solution::Ambig(chalk_solve::Guidance::Unknown) => { - Solution::Ambig(Guidance::Unknown) - } - } + solution } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 701359e6f..c2e289660 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -21,159 +21,10 @@ use super::*; impl ToChalk for Ty { type Chalk = chalk_ir::Ty; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty { - match self.into_inner() { - TyKind::Ref(m, lt, ty) => { - chalk_ir::TyKind::Ref(m, lt, ty.to_chalk(db)).intern(&Interner) - } - TyKind::Array(ty, size) => { - chalk_ir::TyKind::Array(ty.to_chalk(db), size).intern(&Interner) - } - TyKind::Function(FnPointer { sig, substitution: substs, num_binders }) => { - let substitution = chalk_ir::FnSubst(substs.0.to_chalk(db)); - chalk_ir::TyKind::Function(chalk_ir::FnPointer { num_binders, sig, substitution }) - .intern(&Interner) - } - TyKind::AssociatedType(assoc_type_id, substs) => { - let substitution = substs.to_chalk(db); - chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) - } - - TyKind::OpaqueType(id, substs) => { - let substitution = substs.to_chalk(db); - chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) - } - - TyKind::Foreign(id) => chalk_ir::TyKind::Foreign(id).intern(&Interner), - - TyKind::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), - - TyKind::Tuple(cardinality, substs) => { - let substitution = substs.to_chalk(db); - chalk_ir::TyKind::Tuple(cardinality, substitution).intern(&Interner) - } - TyKind::Raw(mutability, ty) => { - let ty = ty.to_chalk(db); - chalk_ir::TyKind::Raw(mutability, ty).intern(&Interner) - } - TyKind::Slice(ty) => chalk_ir::TyKind::Slice(ty.to_chalk(db)).intern(&Interner), - TyKind::Str => chalk_ir::TyKind::Str.intern(&Interner), - TyKind::FnDef(id, substs) => { - let substitution = substs.to_chalk(db); - chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) - } - TyKind::Never => chalk_ir::TyKind::Never.intern(&Interner), - - TyKind::Closure(closure_id, substs) => { - let substitution = substs.to_chalk(db); - chalk_ir::TyKind::Closure(closure_id, substitution).intern(&Interner) - } - - TyKind::Adt(adt_id, substs) => { - let substitution = substs.to_chalk(db); - chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) - } - TyKind::Alias(AliasTy::Projection(proj_ty)) => { - chalk_ir::AliasTy::Projection(proj_ty.to_chalk(db)) - .cast(&Interner) - .intern(&Interner) - } - TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { - chalk_ir::AliasTy::Opaque(opaque_ty.to_chalk(db)).cast(&Interner).intern(&Interner) - } - TyKind::Placeholder(idx) => idx.to_ty::(&Interner), - TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), - TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"), - TyKind::Dyn(dyn_ty) => { - let (bounds, binders) = dyn_ty.bounds.into_value_and_skipped_binders(); - let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( - &Interner, - bounds.interned().iter().cloned().map(|p| p.to_chalk(db)), - ); - let bounded_ty = chalk_ir::DynTy { - bounds: chalk_ir::Binders::new(binders, where_clauses), - lifetime: dyn_ty.lifetime, - }; - chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) - } - TyKind::Error => chalk_ir::TyKind::Error.intern(&Interner), - } + self } fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty) -> Self { - match chalk.data(&Interner).kind.clone() { - chalk_ir::TyKind::Error => TyKind::Error, - chalk_ir::TyKind::Array(ty, size) => TyKind::Array(from_chalk(db, ty), size), - chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx), - chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { - TyKind::Alias(AliasTy::Projection(from_chalk(db, proj))) - } - chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { - TyKind::Alias(AliasTy::Opaque(from_chalk(db, opaque_ty))) - } - chalk_ir::TyKind::Function(chalk_ir::FnPointer { - num_binders, - sig, - substitution, - .. - }) => { - assert_eq!(num_binders, 0); - let substs = crate::FnSubst(from_chalk(db, substitution.0)); - TyKind::Function(FnPointer { num_binders, sig, substitution: substs }) - } - chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx), - chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Error, - chalk_ir::TyKind::Dyn(dyn_ty) => { - assert_eq!(dyn_ty.bounds.binders.len(&Interner), 1); - let (bounds, binders) = dyn_ty.bounds.into_value_and_skipped_binders(); - let where_clauses = crate::QuantifiedWhereClauses::from_iter( - &Interner, - bounds.interned().iter().cloned().map(|p| from_chalk(db, p)), - ); - TyKind::Dyn(crate::DynTy { - bounds: crate::Binders::new(binders, where_clauses), - // HACK: we sometimes get lifetime variables back in solutions - // from Chalk, and don't have the infrastructure to substitute - // them yet. So for now we just turn them into 'static right - // when we get them - lifetime: static_lifetime(), - }) - } - - chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)), - chalk_ir::TyKind::AssociatedType(type_id, subst) => { - TyKind::AssociatedType(type_id, from_chalk(db, subst)) - } - - chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { - TyKind::OpaqueType(opaque_type_id, from_chalk(db, subst)) - } - - chalk_ir::TyKind::Scalar(scalar) => TyKind::Scalar(scalar), - chalk_ir::TyKind::Tuple(cardinality, subst) => { - TyKind::Tuple(cardinality, from_chalk(db, subst)) - } - chalk_ir::TyKind::Raw(mutability, ty) => TyKind::Raw(mutability, from_chalk(db, ty)), - chalk_ir::TyKind::Slice(ty) => TyKind::Slice(from_chalk(db, ty)), - chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { - // HACK: we sometimes get lifetime variables back in solutions - // from Chalk, and don't have the infrastructure to substitute - // them yet. So for now we just turn them into 'static right - // when we get them - TyKind::Ref(mutability, static_lifetime(), from_chalk(db, ty)) - } - chalk_ir::TyKind::Str => TyKind::Str, - chalk_ir::TyKind::Never => TyKind::Never, - - chalk_ir::TyKind::FnDef(fn_def_id, subst) => { - TyKind::FnDef(fn_def_id, from_chalk(db, subst)) - } - - chalk_ir::TyKind::Closure(id, subst) => TyKind::Closure(id, from_chalk(db, subst)), - - chalk_ir::TyKind::Foreign(foreign_def_id) => TyKind::Foreign(foreign_def_id), - chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME - chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME - } - .intern(&Interner) + chalk } } @@ -181,17 +32,11 @@ impl ToChalk for GenericArg { type Chalk = chalk_ir::GenericArg; fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk { - match self.interned() { - crate::GenericArgData::Ty(ty) => ty.clone().to_chalk(db).cast(&Interner), - } + self } fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { - match chalk.interned() { - chalk_ir::GenericArgData::Ty(ty) => Ty::from_chalk(db, ty.clone()).cast(&Interner), - chalk_ir::GenericArgData::Lifetime(_) => unimplemented!(), - chalk_ir::GenericArgData::Const(_) => unimplemented!(), - } + chalk } } @@ -199,18 +44,14 @@ impl ToChalk for Substitution { type Chalk = chalk_ir::Substitution; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution { - chalk_ir::Substitution::from_iter( - &Interner, - self.iter(&Interner).map(|ty| ty.clone().to_chalk(db)), - ) + self } fn from_chalk( db: &dyn HirDatabase, parameters: chalk_ir::Substitution, ) -> Substitution { - let tys = parameters.iter(&Interner).map(|p| from_chalk(db, p.clone())).collect(); - Substitution::intern(tys) + parameters } } @@ -218,15 +59,11 @@ impl ToChalk for TraitRef { type Chalk = chalk_ir::TraitRef; fn to_chalk(self: TraitRef, db: &dyn HirDatabase) -> chalk_ir::TraitRef { - let trait_id = self.trait_id; - let substitution = self.substitution.to_chalk(db); - chalk_ir::TraitRef { trait_id, substitution } + self } fn from_chalk(db: &dyn HirDatabase, trait_ref: chalk_ir::TraitRef) -> Self { - let trait_id = trait_ref.trait_id; - let substs = from_chalk(db, trait_ref.substitution); - TraitRef { trait_id, substitution: substs } + trait_ref } } @@ -287,34 +124,14 @@ impl ToChalk for WhereClause { type Chalk = chalk_ir::WhereClause; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::WhereClause { - match self { - WhereClause::Implemented(trait_ref) => { - chalk_ir::WhereClause::Implemented(trait_ref.to_chalk(db)) - } - WhereClause::AliasEq(alias_eq) => chalk_ir::WhereClause::AliasEq(alias_eq.to_chalk(db)), - } + self } fn from_chalk( db: &dyn HirDatabase, where_clause: chalk_ir::WhereClause, ) -> WhereClause { - match where_clause { - chalk_ir::WhereClause::Implemented(tr) => WhereClause::Implemented(from_chalk(db, tr)), - chalk_ir::WhereClause::AliasEq(alias_eq) => { - WhereClause::AliasEq(from_chalk(db, alias_eq)) - } - - chalk_ir::WhereClause::LifetimeOutlives(_) => { - // we shouldn't get these from Chalk - panic!("encountered LifetimeOutlives from Chalk") - } - - chalk_ir::WhereClause::TypeOutlives(_) => { - // we shouldn't get these from Chalk - panic!("encountered TypeOutlives from Chalk") - } - } + where_clause } } @@ -322,37 +139,25 @@ impl ToChalk for ProjectionTy { type Chalk = chalk_ir::ProjectionTy; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy { - chalk_ir::ProjectionTy { - associated_ty_id: self.associated_ty_id, - substitution: self.substitution.to_chalk(db), - } + self } fn from_chalk( db: &dyn HirDatabase, projection_ty: chalk_ir::ProjectionTy, ) -> ProjectionTy { - ProjectionTy { - associated_ty_id: projection_ty.associated_ty_id, - substitution: from_chalk(db, projection_ty.substitution), - } + projection_ty } } impl ToChalk for OpaqueTy { type Chalk = chalk_ir::OpaqueTy; fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk { - chalk_ir::OpaqueTy { - opaque_ty_id: self.opaque_ty_id, - substitution: self.substitution.to_chalk(db), - } + self } fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { - OpaqueTy { - opaque_ty_id: chalk.opaque_ty_id, - substitution: from_chalk(db, chalk.substitution), - } + chalk } } @@ -360,21 +165,11 @@ impl ToChalk for AliasTy { type Chalk = chalk_ir::AliasTy; fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk { - match self { - AliasTy::Projection(projection_ty) => { - chalk_ir::AliasTy::Projection(projection_ty.to_chalk(db)) - } - AliasTy::Opaque(opaque_ty) => chalk_ir::AliasTy::Opaque(opaque_ty.to_chalk(db)), - } + self } fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { - match chalk { - chalk_ir::AliasTy::Projection(projection_ty) => { - AliasTy::Projection(from_chalk(db, projection_ty)) - } - chalk_ir::AliasTy::Opaque(opaque_ty) => AliasTy::Opaque(from_chalk(db, opaque_ty)), - } + chalk } } @@ -382,11 +177,11 @@ impl ToChalk for AliasEq { type Chalk = chalk_ir::AliasEq; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::AliasEq { - chalk_ir::AliasEq { alias: self.alias.to_chalk(db), ty: self.ty.to_chalk(db) } + self } fn from_chalk(db: &dyn HirDatabase, alias_eq: chalk_ir::AliasEq) -> Self { - AliasEq { alias: from_chalk(db, alias_eq.alias), ty: from_chalk(db, alias_eq.ty) } + alias_eq } } @@ -394,68 +189,56 @@ impl ToChalk for DomainGoal { type Chalk = chalk_ir::DomainGoal; fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::DomainGoal { - match self { - DomainGoal::Holds(WhereClause::Implemented(tr)) => tr.to_chalk(db).cast(&Interner), - DomainGoal::Holds(WhereClause::AliasEq(alias_eq)) => { - alias_eq.to_chalk(db).cast(&Interner) - } - } + self } - fn from_chalk(_db: &dyn HirDatabase, _goal: chalk_ir::DomainGoal) -> Self { - unimplemented!() + fn from_chalk(_db: &dyn HirDatabase, goal: chalk_ir::DomainGoal) -> Self { + goal } } impl ToChalk for Canonical where - T: ToChalk, - T::Chalk: HasInterner, + T: HasInterner, { - type Chalk = chalk_ir::Canonical; + type Chalk = chalk_ir::Canonical; - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical { - let value = self.value.to_chalk(db); - chalk_ir::Canonical { value, binders: self.binders } + fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical { + self } - fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical) -> Canonical { - Canonical { binders: canonical.binders, value: from_chalk(db, canonical.value) } + fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical) -> Canonical { + canonical } } impl ToChalk for InEnvironment where - T::Chalk: chalk_ir::interner::HasInterner, + T: HasInterner, { - type Chalk = chalk_ir::InEnvironment; + type Chalk = chalk_ir::InEnvironment; - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::InEnvironment { - chalk_ir::InEnvironment { environment: self.environment, goal: self.goal.to_chalk(db) } + fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::InEnvironment { + self } - fn from_chalk( - _db: &dyn HirDatabase, - _in_env: chalk_ir::InEnvironment, - ) -> InEnvironment { - unimplemented!() + fn from_chalk(_db: &dyn HirDatabase, in_env: chalk_ir::InEnvironment) -> InEnvironment { + in_env } } impl ToChalk for crate::Binders where - T::Chalk: chalk_ir::interner::HasInterner, + T: HasInterner, { - type Chalk = chalk_ir::Binders; + type Chalk = chalk_ir::Binders; - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Binders { - let (value, binders) = self.into_value_and_skipped_binders(); - chalk_ir::Binders::new(binders, value.to_chalk(db)) + fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Binders { + self } - fn from_chalk(db: &dyn HirDatabase, binders: chalk_ir::Binders) -> crate::Binders { - let (v, b) = binders.into_value_and_skipped_binders(); - crate::Binders::new(b, from_chalk(db, v)) + fn from_chalk(db: &dyn HirDatabase, binders: chalk_ir::Binders) -> crate::Binders { + binders } } @@ -463,11 +246,11 @@ impl ToChalk for crate::ConstrainedSubst { type Chalk = chalk_ir::ConstrainedSubst; fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk { - unimplemented!() + self } fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { - ConstrainedSubst { subst: from_chalk(db, chalk.subst) } + chalk } } -- cgit v1.2.3 From e5d294765ae224ad59c6cd55442088fd3e604708 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 17:41:55 +0200 Subject: Remove obsolete Cast impls --- crates/hir_ty/src/chalk_cast.rs | 47 ----------------------------------------- 1 file changed, 47 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/chalk_cast.rs b/crates/hir_ty/src/chalk_cast.rs index df6492113..1c3feabca 100644 --- a/crates/hir_ty/src/chalk_cast.rs +++ b/crates/hir_ty/src/chalk_cast.rs @@ -15,35 +15,6 @@ macro_rules! has_interner { }; } -has_interner!(WhereClause); -has_interner!(DomainGoal); -has_interner!(GenericArg); -has_interner!(Ty); - -impl CastTo for TraitRef { - fn cast_to(self, _interner: &Interner) -> WhereClause { - WhereClause::Implemented(self) - } -} - -impl CastTo for AliasEq { - fn cast_to(self, _interner: &Interner) -> WhereClause { - WhereClause::AliasEq(self) - } -} - -impl CastTo for WhereClause { - fn cast_to(self, _interner: &Interner) -> DomainGoal { - DomainGoal::Holds(self) - } -} - -impl CastTo for Ty { - fn cast_to(self, interner: &Interner) -> GenericArg { - GenericArg::new(interner, GenericArgData::Ty(self)) - } -} - macro_rules! transitive_impl { ($a:ty, $b:ty, $c:ty) => { impl CastTo<$c> for $a { @@ -53,21 +24,3 @@ macro_rules! transitive_impl { } }; } - -// In Chalk, these can be done as blanket impls, but that doesn't work here -// because of coherence - -transitive_impl!(TraitRef, WhereClause, DomainGoal); -transitive_impl!(AliasEq, WhereClause, DomainGoal); - -macro_rules! reflexive_impl { - ($a:ty) => { - impl CastTo<$a> for $a { - fn cast_to(self, _interner: &Interner) -> $a { - self - } - } - }; -} - -reflexive_impl!(GenericArg); -- cgit v1.2.3 From 926bfef0efc5caca2a04a50beaba9127338e21e0 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 17:42:13 +0200 Subject: Allow unused --- crates/hir_ty/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'crates') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 3378015ca..5fa8b3296 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -1,5 +1,8 @@ //! The type system. We currently use this to infer types for completion, hover //! information and various assists. + +#![allow(unused)] + #[allow(unused)] macro_rules! eprintln { ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; -- cgit v1.2.3 From 7e541e69b1eaab3c242c864a0930cb263d2cbaf5 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 3 Apr 2021 17:49:29 +0200 Subject: Add HasInterner bounds --- crates/hir_ty/src/chalk_cast.rs | 8 +++++++- crates/hir_ty/src/infer/unify.rs | 16 ++++++++++++---- crates/hir_ty/src/lib.rs | 15 +++++++++++---- crates/hir_ty/src/lower.rs | 4 ++-- crates/hir_ty/src/traits/chalk.rs | 2 +- crates/hir_ty/src/walk.rs | 4 ++-- 6 files changed, 35 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/chalk_cast.rs b/crates/hir_ty/src/chalk_cast.rs index 1c3feabca..8bdd32ad6 100644 --- a/crates/hir_ty/src/chalk_cast.rs +++ b/crates/hir_ty/src/chalk_cast.rs @@ -5,7 +5,10 @@ use chalk_ir::{ interner::HasInterner, }; -use crate::{AliasEq, DomainGoal, GenericArg, GenericArgData, Interner, TraitRef, Ty, WhereClause}; +use crate::{ + AliasEq, CallableSig, DomainGoal, GenericArg, GenericArgData, Interner, PolyFnSig, + ReturnTypeImplTraits, TraitRef, Ty, WhereClause, +}; macro_rules! has_interner { ($t:ty) => { @@ -24,3 +27,6 @@ macro_rules! transitive_impl { } }; } + +has_interner!(CallableSig); +has_interner!(ReturnTypeImplTraits); diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 2ea9dd920..3732d8ebd 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -2,7 +2,9 @@ use std::borrow::Cow; -use chalk_ir::{FloatTy, IntTy, TyVariableKind, UniverseIndex, VariableKind}; +use chalk_ir::{ + interner::HasInterner, FloatTy, IntTy, TyVariableKind, UniverseIndex, VariableKind, +}; use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; use super::{DomainGoal, InferenceContext}; @@ -34,7 +36,10 @@ where } #[derive(Debug)] -pub(super) struct Canonicalized { +pub(super) struct Canonicalized +where + T: HasInterner, +{ pub(super) value: Canonical, free_vars: Vec<(InferenceVar, TyVariableKind)>, } @@ -76,7 +81,10 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { ) } - fn into_canonicalized(self, result: T) -> Canonicalized { + fn into_canonicalized>( + self, + result: T, + ) -> Canonicalized { let kinds = self .free_vars .iter() @@ -108,7 +116,7 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { } } -impl Canonicalized { +impl> Canonicalized { pub(super) fn decanonicalize_ty(&self, ty: Ty) -> Ty { ty.fold_binders( &mut |ty, binders| { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 5fa8b3296..23dec7040 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -33,7 +33,11 @@ mod test_db; use std::sync::Arc; use base_db::salsa; -use chalk_ir::UintTy; +use chalk_ir::{ + cast::{CastTo, Caster}, + interner::HasInterner, + UintTy, +}; use hir_def::{ expr::ExprId, type_ref::Rawness, ConstParamId, LifetimeParamId, TraitId, TypeAliasId, TypeParamId, @@ -115,12 +119,15 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option { pub fn wrap_empty_binders(value: T) -> Binders where - T: TypeWalk, + T: TypeWalk + HasInterner, { Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE)) } -pub fn make_only_type_binders(num_vars: usize, value: T) -> Binders { +pub fn make_only_type_binders>( + num_vars: usize, + value: T, +) -> Binders { Binders::new( VariableKinds::from_iter( &Interner, @@ -132,7 +139,7 @@ pub fn make_only_type_binders(num_vars: usize, value: T) -> Binders { } // FIXME: get rid of this -pub fn make_canonical( +pub fn make_canonical>( value: T, kinds: impl IntoIterator, ) -> Canonical { diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index e6903e189..d4ba707b4 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -8,7 +8,7 @@ use std::{iter, sync::Arc}; use base_db::CrateId; -use chalk_ir::{cast::Cast, Mutability, Safety}; +use chalk_ir::{cast::Cast, interner::HasInterner, Mutability, Safety}; use hir_def::{ adt::StructKind, builtin_type::BuiltinType, @@ -1307,6 +1307,6 @@ pub(crate) fn lower_to_chalk_mutability(m: hir_def::type_ref::Mutability) -> Mut } } -fn make_binders(generics: &Generics, value: T) -> Binders { +fn make_binders>(generics: &Generics, value: T) -> Binders { crate::make_only_type_binders(generics.len(), value) } diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 090f6492b..a5aa40c10 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use log::debug; -use chalk_ir::{fold::shift::Shift, CanonicalVarKinds}; +use chalk_ir::{fold::shift::Shift, interner::HasInterner, CanonicalVarKinds}; use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; use base_db::{salsa::InternKey, CrateId}; diff --git a/crates/hir_ty/src/walk.rs b/crates/hir_ty/src/walk.rs index 873d563c0..c1aecdafc 100644 --- a/crates/hir_ty/src/walk.rs +++ b/crates/hir_ty/src/walk.rs @@ -3,7 +3,7 @@ use std::mem; -use chalk_ir::DebruijnIndex; +use chalk_ir::{interner::HasInterner, DebruijnIndex}; use crate::{ utils::make_mut_slice, AliasEq, AliasTy, Binders, CallableSig, FnSubst, GenericArg, @@ -320,7 +320,7 @@ impl TypeWalk for Substitution { } } -impl TypeWalk for Binders { +impl> TypeWalk for Binders { fn walk(&self, f: &mut impl FnMut(&Ty)) { self.skip_binders().walk(f); } -- cgit v1.2.3 From b24e6f60300300125ff353073a36d1ef7a64e37f Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 21:10:28 +0200 Subject: Impl Fold for CallableSig --- crates/hir_ty/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'crates') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 23dec7040..0954d495c 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -35,6 +35,7 @@ use std::sync::Arc; use base_db::salsa; use chalk_ir::{ cast::{CastTo, Caster}, + fold::Fold, interner::HasInterner, UintTy, }; @@ -203,6 +204,23 @@ impl CallableSig { } } +impl Fold for CallableSig { + type Result = CallableSig; + + fn fold_with<'i>( + self, + folder: &mut dyn chalk_ir::fold::Folder<'i, Interner>, + outer_binder: DebruijnIndex, + ) -> chalk_ir::Fallible + where + Interner: 'i, + { + let vec = self.params_and_return.to_vec(); + let folded = vec.fold_with(folder, outer_binder)?; + Ok(CallableSig { params_and_return: folded.into(), is_varargs: self.is_varargs }) + } +} + #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum ImplTraitId { ReturnTypeImplTrait(hir_def::FunctionId, u16), -- cgit v1.2.3 From 1332e72d09036e31961bb0ab5a7175d34c2fbf68 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 21:12:42 +0200 Subject: Fix TyBuilder methods --- crates/hir_ty/src/builder.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/builder.rs b/crates/hir_ty/src/builder.rs index 09512d1ce..9a698898b 100644 --- a/crates/hir_ty/src/builder.rs +++ b/crates/hir_ty/src/builder.rs @@ -4,6 +4,7 @@ use std::iter; use chalk_ir::{ cast::{Cast, CastTo, Caster}, + fold::Fold, interner::HasInterner, AdtId, BoundVar, DebruijnIndex, Safety, Scalar, }; @@ -32,8 +33,7 @@ impl TyBuilder { fn build_internal(self) -> (D, Substitution) { assert_eq!(self.vec.len(), self.param_count); - // FIXME: would be good to have a way to construct a chalk_ir::Substitution from the interned form - let subst = Substitution::intern(self.vec); + let subst = Substitution::from_iter(&Interner, self.vec); (self.data, subst) } @@ -141,7 +141,7 @@ impl TyBuilder { self.vec.push(fallback().cast(&Interner)); } else { // each default can depend on the previous parameters - let subst_so_far = Substitution::intern(self.vec.clone()); + let subst_so_far = Substitution::from_iter(&Interner, self.vec.clone()); self.vec .push(default_ty.clone().substitute(&Interner, &subst_so_far).cast(&Interner)); } @@ -196,13 +196,13 @@ impl TyBuilder { } } -impl> TyBuilder> { +impl + Fold> TyBuilder> { fn subst_binders(b: Binders) -> Self { let param_count = b.binders.len(&Interner); TyBuilder::new(b, param_count) } - pub fn build(self) -> T { + pub fn build(self) -> >::Result { let (b, subst) = self.build_internal(); b.substitute(&Interner, &subst) } -- cgit v1.2.3 From 5794a090bf8609d695d7e9fd1c3df8d0ca56a708 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 21:16:18 +0200 Subject: Get rid of walk_mut [not compiling] --- crates/hir_ty/src/walk.rs | 272 ---------------------------------------------- 1 file changed, 272 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/walk.rs b/crates/hir_ty/src/walk.rs index c1aecdafc..28875dbe0 100644 --- a/crates/hir_ty/src/walk.rs +++ b/crates/hir_ty/src/walk.rs @@ -15,124 +15,6 @@ use crate::{ /// types, similar to Chalk's `Fold` trait. pub trait TypeWalk { fn walk(&self, f: &mut impl FnMut(&Ty)); - fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { - self.walk_mut_binders(&mut |ty, _binders| f(ty), DebruijnIndex::INNERMOST); - } - /// Walk the type, counting entered binders. - /// - /// `TyKind::Bound` variables use DeBruijn indexing, which means that 0 refers - /// to the innermost binder, 1 to the next, etc.. So when we want to - /// substitute a certain bound variable, we can't just walk the whole type - /// and blindly replace each instance of a certain index; when we 'enter' - /// things that introduce new bound variables, we have to keep track of - /// that. Currently, the only thing that introduces bound variables on our - /// side are `TyKind::Dyn` and `TyKind::Opaque`, which each introduce a bound - /// variable for the self type. - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ); - - fn fold_binders( - mut self, - f: &mut impl FnMut(Ty, DebruijnIndex) -> Ty, - binders: DebruijnIndex, - ) -> Self - where - Self: Sized, - { - self.walk_mut_binders( - &mut |ty_mut, binders| { - let ty = mem::replace(ty_mut, TyKind::Error.intern(&Interner)); - *ty_mut = f(ty, binders); - }, - binders, - ); - self - } - - fn fold(mut self, f: &mut impl FnMut(Ty) -> Ty) -> Self - where - Self: Sized, - { - self.walk_mut(&mut |ty_mut| { - let ty = mem::replace(ty_mut, TyKind::Error.intern(&Interner)); - *ty_mut = f(ty); - }); - self - } - - /// Substitutes `TyKind::Bound` vars with the given substitution. - fn subst_bound_vars(self, substs: &Substitution) -> Self - where - Self: Sized, - { - self.subst_bound_vars_at_depth(substs, DebruijnIndex::INNERMOST) - } - - /// Substitutes `TyKind::Bound` vars with the given substitution. - fn subst_bound_vars_at_depth(mut self, substs: &Substitution, depth: DebruijnIndex) -> Self - where - Self: Sized, - { - self.walk_mut_binders( - &mut |ty, binders| { - if let &mut TyKind::BoundVar(bound) = ty.interned_mut() { - if bound.debruijn >= binders { - *ty = substs.interned()[bound.index] - .assert_ty_ref(&Interner) - .clone() - .shifted_in_from(binders); - } - } - }, - depth, - ); - self - } - - fn shifted_in(self, _interner: &Interner) -> Self - where - Self: Sized, - { - self.shifted_in_from(DebruijnIndex::ONE) - } - - /// Shifts up debruijn indices of `TyKind::Bound` vars by `n`. - fn shifted_in_from(self, n: DebruijnIndex) -> Self - where - Self: Sized, - { - self.fold_binders( - &mut |ty, binders| match ty.kind(&Interner) { - TyKind::BoundVar(bound) if bound.debruijn >= binders => { - TyKind::BoundVar(bound.shifted_in_from(n)).intern(&Interner) - } - _ => ty, - }, - DebruijnIndex::INNERMOST, - ) - } - - /// Shifts debruijn indices of `TyKind::Bound` vars out (down) by `n`. - fn shifted_out_to(self, n: DebruijnIndex) -> Option - where - Self: Sized + std::fmt::Debug, - { - Some(self.fold_binders( - &mut |ty, binders| { - match ty.kind(&Interner) { - TyKind::BoundVar(bound) if bound.debruijn >= binders => { - TyKind::BoundVar(bound.shifted_out_to(n).unwrap_or(bound.clone())) - .intern(&Interner) - } - _ => ty, - } - }, - DebruijnIndex::INNERMOST, - )) - } } impl TypeWalk for Ty { @@ -174,45 +56,6 @@ impl TypeWalk for Ty { } f(self); } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - match self.interned_mut() { - TyKind::Alias(AliasTy::Projection(p_ty)) => { - p_ty.substitution.walk_mut_binders(f, binders); - } - TyKind::Dyn(dyn_ty) => { - for p in make_mut_slice(dyn_ty.bounds.skip_binders_mut().interned_mut()) { - p.walk_mut_binders(f, binders.shifted_in()); - } - } - TyKind::Alias(AliasTy::Opaque(o_ty)) => { - o_ty.substitution.walk_mut_binders(f, binders); - } - TyKind::Slice(ty) - | TyKind::Array(ty, _) - | TyKind::Ref(_, _, ty) - | TyKind::Raw(_, ty) => { - ty.walk_mut_binders(f, binders); - } - TyKind::Function(fn_pointer) => { - fn_pointer.substitution.0.walk_mut_binders(f, binders.shifted_in()); - } - TyKind::Adt(_, substs) - | TyKind::FnDef(_, substs) - | TyKind::Tuple(_, substs) - | TyKind::OpaqueType(_, substs) - | TyKind::AssociatedType(_, substs) - | TyKind::Closure(.., substs) => { - substs.walk_mut_binders(f, binders); - } - _ => {} - } - f(self, binders); - } } impl TypeWalk for Vec { @@ -221,43 +64,18 @@ impl TypeWalk for Vec { t.walk(f); } } - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - for t in self { - t.walk_mut_binders(f, binders); - } - } } impl TypeWalk for OpaqueTy { fn walk(&self, f: &mut impl FnMut(&Ty)) { self.substitution.walk(f); } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - self.substitution.walk_mut_binders(f, binders); - } } impl TypeWalk for ProjectionTy { fn walk(&self, f: &mut impl FnMut(&Ty)) { self.substitution.walk(f); } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - self.substitution.walk_mut_binders(f, binders); - } } impl TypeWalk for AliasTy { @@ -267,17 +85,6 @@ impl TypeWalk for AliasTy { AliasTy::Opaque(it) => it.walk(f), } } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - match self { - AliasTy::Projection(it) => it.walk_mut_binders(f, binders), - AliasTy::Opaque(it) => it.walk_mut_binders(f, binders), - } - } } impl TypeWalk for GenericArg { @@ -288,18 +95,6 @@ impl TypeWalk for GenericArg { } } } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - match self.interned_mut() { - GenericArgData::Ty(ty) => { - ty.walk_mut_binders(f, binders); - } - } - } } impl TypeWalk for Substitution { @@ -308,44 +103,18 @@ impl TypeWalk for Substitution { t.walk(f); } } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - for t in self.interned_mut() { - t.walk_mut_binders(f, binders); - } - } } impl> TypeWalk for Binders { fn walk(&self, f: &mut impl FnMut(&Ty)) { self.skip_binders().walk(f); } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - self.skip_binders_mut().walk_mut_binders(f, binders.shifted_in()) - } } impl TypeWalk for TraitRef { fn walk(&self, f: &mut impl FnMut(&Ty)) { self.substitution.walk(f); } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - self.substitution.walk_mut_binders(f, binders); - } } impl TypeWalk for WhereClause { @@ -355,17 +124,6 @@ impl TypeWalk for WhereClause { WhereClause::AliasEq(alias_eq) => alias_eq.walk(f), } } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - match self { - WhereClause::Implemented(trait_ref) => trait_ref.walk_mut_binders(f, binders), - WhereClause::AliasEq(alias_eq) => alias_eq.walk_mut_binders(f, binders), - } - } } impl TypeWalk for CallableSig { @@ -374,16 +132,6 @@ impl TypeWalk for CallableSig { t.walk(f); } } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - for t in make_mut_slice(&mut self.params_and_return) { - t.walk_mut_binders(f, binders); - } - } } impl TypeWalk for AliasEq { @@ -394,30 +142,10 @@ impl TypeWalk for AliasEq { AliasTy::Opaque(opaque) => opaque.walk(f), } } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - self.ty.walk_mut_binders(f, binders); - match &mut self.alias { - AliasTy::Projection(projection_ty) => projection_ty.walk_mut_binders(f, binders), - AliasTy::Opaque(opaque) => opaque.walk_mut_binders(f, binders), - } - } } impl TypeWalk for FnSubst { fn walk(&self, f: &mut impl FnMut(&Ty)) { self.0.walk(f) } - - fn walk_mut_binders( - &mut self, - f: &mut impl FnMut(&mut Ty, DebruijnIndex), - binders: DebruijnIndex, - ) { - self.0.walk_mut_binders(f, binders) - } } -- cgit v1.2.3 From dc0b5817366f9b7b99a1a1f6e69ca7ac63e9ed0f Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 21:17:51 +0200 Subject: Fix subst_prefix --- crates/hir_ty/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 0954d495c..78ae3545a 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -110,7 +110,10 @@ pub type WhereClause = chalk_ir::WhereClause; // FIXME: get rid of this pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution { - Substitution::intern(s.interned()[..std::cmp::min(s.len(&Interner), n)].into()) + Substitution::from_iter( + &Interner, + s.interned()[..std::cmp::min(s.len(&Interner), n)].iter().cloned(), + ) } /// Return an index of a parameter in the generic type parameter list by it's id. -- cgit v1.2.3 From b25b147e8604f62a5620a5833112e358ebeeb287 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 21:26:24 +0200 Subject: Fix shifted_{in,out} calls --- crates/hir_ty/src/infer/expr.rs | 2 +- crates/hir_ty/src/lib.rs | 8 ++++---- crates/hir_ty/src/lower.rs | 6 +++--- crates/hir_ty/src/traits/chalk/mapping.rs | 2 +- crates/hir_ty/src/utils.rs | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 9841988c5..5f03079e8 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -3,7 +3,7 @@ use std::iter::{repeat, repeat_with}; use std::{mem, sync::Arc}; -use chalk_ir::{cast::Cast, Mutability, TyVariableKind}; +use chalk_ir::{cast::Cast, fold::Shift, Mutability, TyVariableKind}; use hir_def::{ expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, path::{GenericArg, GenericArgs}, diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 78ae3545a..7d4793c2f 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -35,7 +35,7 @@ use std::sync::Arc; use base_db::salsa; use chalk_ir::{ cast::{CastTo, Caster}, - fold::Fold, + fold::{Fold, Shift}, interner::HasInterner, UintTy, }; @@ -123,9 +123,9 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option { pub fn wrap_empty_binders(value: T) -> Binders where - T: TypeWalk + HasInterner, + T: Fold + HasInterner, { - Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE)) + Binders::empty(&Interner, value.shifted_in_from(&Interner, DebruijnIndex::ONE)) } pub fn make_only_type_binders>( @@ -187,7 +187,7 @@ impl CallableSig { params_and_return: fn_ptr .substitution .clone() - .shifted_out_to(DebruijnIndex::ONE) + .shifted_out_to(&Interner, DebruijnIndex::ONE) .expect("unexpected lifetime vars in fn ptr") .0 .interned() diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index d4ba707b4..b45e811fa 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -8,7 +8,7 @@ use std::{iter, sync::Arc}; use base_db::CrateId; -use chalk_ir::{cast::Cast, interner::HasInterner, Mutability, Safety}; +use chalk_ir::{cast::Cast, fold::Shift, interner::HasInterner, Mutability, Safety}; use hir_def::{ adt::StructKind, builtin_type::BuiltinType, @@ -488,7 +488,7 @@ impl<'a> TyLoweringContext<'a> { }; // We need to shift in the bound vars, since // associated_type_shorthand_candidates does not do that - let substs = substs.shifted_in_from(self.in_binders); + let substs = substs.shifted_in_from(&Interner, self.in_binders); // FIXME handle type parameters on the segment return Some( TyKind::Alias(AliasTy::Projection(ProjectionTy { @@ -847,7 +847,7 @@ pub fn associated_type_shorthand_candidates( // FIXME: how to correctly handle higher-ranked bounds here? WhereClause::Implemented(tr) => search( tr.clone() - .shifted_out_to(DebruijnIndex::ONE) + .shifted_out_to(&Interner, DebruijnIndex::ONE) .expect("FIXME unexpected higher-ranked trait bound"), ), _ => None, diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index c2e289660..62cf0fecf 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -288,7 +288,7 @@ pub(super) fn generic_predicate_to_inline_bound( ) -> Option>> { // An InlineBound is like a GenericPredicate, except the self type is left out. // We don't have a special type for this, but Chalk does. - let self_ty_shifted_in = self_ty.clone().shifted_in_from(DebruijnIndex::ONE); + let self_ty_shifted_in = self_ty.clone().shifted_in_from(&Interner, DebruijnIndex::ONE); let (pred, binders) = pred.as_ref().into_value_and_skipped_binders(); match pred { WhereClause::Implemented(trait_ref) => { diff --git a/crates/hir_ty/src/utils.rs b/crates/hir_ty/src/utils.rs index 8d5d5cd73..d26f5152c 100644 --- a/crates/hir_ty/src/utils.rs +++ b/crates/hir_ty/src/utils.rs @@ -2,7 +2,7 @@ //! query, but can't be computed directly from `*Data` (ie, which need a `db`). use std::sync::Arc; -use chalk_ir::{BoundVar, DebruijnIndex}; +use chalk_ir::{fold::Shift, BoundVar, DebruijnIndex}; use hir_def::{ db::DefDatabase, generics::{ @@ -69,7 +69,7 @@ fn direct_super_trait_refs(db: &dyn HirDatabase, trait_ref: &TraitRef) -> Vec Some( tr.clone() - .shifted_out_to(DebruijnIndex::ONE) + .shifted_out_to(&Interner, DebruijnIndex::ONE) .expect("FIXME unexpected higher-ranked trait bound"), ), _ => None, -- cgit v1.2.3 From caee3b6c2d9a7dbcf678cc06d0c1832a4bf19be4 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 21:26:37 +0200 Subject: Replace some `fold` calls --- crates/hir_ty/src/infer/unify.rs | 19 ++++--------------- crates/hir_ty/src/lib.rs | 26 ++++++++++++++++++++++++++ crates/hir_ty/src/lower.rs | 26 ++++++++++---------------- crates/hir_ty/src/method_resolution.rs | 29 +++++++++++++---------------- 4 files changed, 53 insertions(+), 47 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 3732d8ebd..c3d9e81c3 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -118,21 +118,10 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { impl> Canonicalized { pub(super) fn decanonicalize_ty(&self, ty: Ty) -> Ty { - ty.fold_binders( - &mut |ty, binders| { - if let TyKind::BoundVar(bound) = ty.kind(&Interner) { - if bound.debruijn >= binders { - let (v, k) = self.free_vars[bound.index]; - TyKind::InferenceVar(v, k).intern(&Interner) - } else { - ty - } - } else { - ty - } - }, - DebruijnIndex::INNERMOST, - ) + crate::fold_free_vars(ty, |bound, _binders| { + let (v, k) = self.free_vars[bound.index]; + TyKind::InferenceVar(v, k).intern(&Interner) + }) } pub(super) fn apply_solution( diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 7d4793c2f..60680ec86 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -302,3 +302,29 @@ pub fn dummy_usize_const() -> Const { } .intern(&Interner) } + +pub(crate) fn fold_free_vars + Fold>( + t: T, + f: impl FnMut(BoundVar, DebruijnIndex) -> Ty, +) -> T::Result { + use chalk_ir::{fold::Folder, Fallible}; + struct FreeVarFolder(F); + impl<'i, F: FnMut(BoundVar, DebruijnIndex) -> Ty + 'i> Folder<'i, Interner> for FreeVarFolder { + fn as_dyn(&mut self) -> &mut dyn Folder<'i, Interner> { + self + } + + fn interner(&self) -> &'i Interner { + &Interner + } + + fn fold_free_var_ty( + &mut self, + bound_var: BoundVar, + outer_binder: DebruijnIndex, + ) -> Fallible { + Ok(self.0(bound_var, outer_binder)) + } + } + t.fold_with(&mut FreeVarFolder(f), DebruijnIndex::INNERMOST).expect("fold failed unexpectedly") +} diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index b45e811fa..109157a5e 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -1016,22 +1016,16 @@ pub(crate) fn generic_defaults_query( p.default.as_ref().map_or(TyKind::Error.intern(&Interner), |t| ctx.lower_ty(t)); // Each default can only refer to previous parameters. - ty = ty.fold_binders( - &mut |ty, binders| match ty.kind(&Interner) { - TyKind::BoundVar(BoundVar { debruijn, index }) if *debruijn == binders => { - if *index >= idx { - // type variable default referring to parameter coming - // after it. This is forbidden (FIXME: report - // diagnostic) - TyKind::Error.intern(&Interner) - } else { - ty - } - } - _ => ty, - }, - DebruijnIndex::INNERMOST, - ); + ty = crate::fold_free_vars(ty, |bound, binders| { + if bound.index >= idx && bound.debruijn == DebruijnIndex::INNERMOST { + // type variable default referring to parameter coming + // after it. This is forbidden (FIXME: report + // diagnostic) + TyKind::Error.intern(&Interner) + } else { + bound.shifted_in_from(binders).to_ty(&Interner) + } + }); crate::make_only_type_binders(idx, ty) }) diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 7e09a1539..766880b14 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -6,7 +6,11 @@ use std::{iter, sync::Arc}; use arrayvec::ArrayVec; use base_db::CrateId; -use chalk_ir::{cast::Cast, Mutability, UniverseIndex}; +use chalk_ir::{ + cast::Cast, + fold::{Fold, Folder}, + Fallible, Mutability, UniverseIndex, +}; use hir_def::{ lang_item::LangItemTarget, nameres::DefMap, AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule, ImplId, Lookup, ModuleId, TraitId, @@ -21,7 +25,7 @@ use crate::{ primitive::{self, FloatTy, IntTy, UintTy}, static_lifetime, utils::all_super_traits, - AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId, + AdtId, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId, InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, TypeWalk, }; @@ -757,20 +761,13 @@ pub(crate) fn inherent_impl_substs( /// This replaces any 'free' Bound vars in `s` (i.e. those with indices past /// num_vars_to_keep) by `TyKind::Unknown`. fn fallback_bound_vars(s: Substitution, num_vars_to_keep: usize) -> Substitution { - s.fold_binders( - &mut |ty, binders| { - if let TyKind::BoundVar(bound) = ty.kind(&Interner) { - if bound.index >= num_vars_to_keep && bound.debruijn >= binders { - TyKind::Error.intern(&Interner) - } else { - ty - } - } else { - ty - } - }, - DebruijnIndex::INNERMOST, - ) + crate::fold_free_vars(s, |bound, binders| { + if bound.index >= num_vars_to_keep && bound.debruijn == DebruijnIndex::INNERMOST { + TyKind::Error.intern(&Interner) + } else { + bound.shifted_in_from(binders).to_ty(&Interner) + } + }) } fn transform_receiver_ty( -- cgit v1.2.3 From 0f058d61cea9805897702654bcca146fd0cdc507 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 13:32:48 +0200 Subject: Replace remaining `fold` calls --- crates/hir_ty/src/infer.rs | 25 +++++---- crates/hir_ty/src/infer/unify.rs | 111 ++++++++++++++++++++++----------------- crates/hir_ty/src/lib.rs | 27 ++++++++++ 3 files changed, 104 insertions(+), 59 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index 531159e54..1d8184ef5 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -18,7 +18,7 @@ use std::mem; use std::ops::Index; use std::sync::Arc; -use chalk_ir::{cast::Cast, Mutability}; +use chalk_ir::{cast::Cast, DebruijnIndex, Mutability}; use hir_def::{ body::Body, data::{ConstData, FunctionData, StaticData}, @@ -41,8 +41,9 @@ use super::{ TypeWalk, }; use crate::{ - db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, - to_assoc_type_id, AliasEq, AliasTy, Canonical, Interner, TyBuilder, TyExt, TyKind, + db::HirDatabase, fold_tys, infer::diagnostics::InferenceDiagnostic, + lower::ImplTraitLoweringMode, to_assoc_type_id, AliasEq, AliasTy, Canonical, Interner, + TyBuilder, TyExt, TyKind, }; // This lint has a false positive here. See the link below for details. @@ -323,7 +324,7 @@ impl<'a> InferenceContext<'a> { } fn insert_type_vars(&mut self, ty: Ty) -> Ty { - ty.fold(&mut |ty| self.insert_type_vars_shallow(ty)) + fold_tys(ty, |ty, _| self.insert_type_vars_shallow(ty), DebruijnIndex::INNERMOST) } fn resolve_obligations_as_possible(&mut self) { @@ -434,12 +435,16 @@ impl<'a> InferenceContext<'a> { /// to do it as well. fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty { let ty = self.resolve_ty_as_possible(ty); - ty.fold(&mut |ty| match ty.kind(&Interner) { - TyKind::Alias(AliasTy::Projection(proj_ty)) => { - self.normalize_projection_ty(proj_ty.clone()) - } - _ => ty, - }) + fold_tys( + ty, + |ty, _| match ty.kind(&Interner) { + TyKind::Alias(AliasTy::Projection(proj_ty)) => { + self.normalize_projection_ty(proj_ty.clone()) + } + _ => ty, + }, + DebruijnIndex::INNERMOST, + ) } fn normalize_projection_ty(&mut self, proj_ty: ProjectionTy) -> Ty { diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index c3d9e81c3..ffa324b6d 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -3,15 +3,15 @@ use std::borrow::Cow; use chalk_ir::{ - interner::HasInterner, FloatTy, IntTy, TyVariableKind, UniverseIndex, VariableKind, + fold::Fold, interner::HasInterner, FloatTy, IntTy, TyVariableKind, UniverseIndex, VariableKind, }; use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; use super::{DomainGoal, InferenceContext}; use crate::{ - AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSubst, - InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyExt, TyKind, TypeWalk, - WhereClause, + fold_tys, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, + FnSubst, InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyExt, TyKind, + TypeWalk, WhereClause, }; impl<'a> InferenceContext<'a> { @@ -53,9 +53,14 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { }) } - fn do_canonicalize(&mut self, t: T, binders: DebruijnIndex) -> T { - t.fold_binders( - &mut |ty, binders| match ty.kind(&Interner) { + fn do_canonicalize + HasInterner>( + &mut self, + t: T, + binders: DebruijnIndex, + ) -> T { + fold_tys( + t, + |ty, binders| match ty.kind(&Interner) { &TyKind::InferenceVar(var, kind) => { let inner = from_inference_var(var); if self.var_stack.contains(&inner) { @@ -485,55 +490,63 @@ impl InferenceTable { /// be resolved as far as possible, i.e. contain no type variables with /// known type. fn resolve_ty_as_possible_inner(&mut self, tv_stack: &mut Vec, ty: Ty) -> Ty { - ty.fold(&mut |ty| match ty.kind(&Interner) { - &TyKind::InferenceVar(tv, kind) => { - let inner = from_inference_var(tv); - if tv_stack.contains(&inner) { - cov_mark::hit!(type_var_cycles_resolve_as_possible); - // recursive type - return self.type_variable_table.fallback_value(tv, kind); - } - if let Some(known_ty) = - self.var_unification_table.inlined_probe_value(inner).known() - { - // known_ty may contain other variables that are known by now - tv_stack.push(inner); - let result = self.resolve_ty_as_possible_inner(tv_stack, known_ty.clone()); - tv_stack.pop(); - result - } else { - ty + fold_tys( + ty, + |ty, _| match ty.kind(&Interner) { + &TyKind::InferenceVar(tv, kind) => { + let inner = from_inference_var(tv); + if tv_stack.contains(&inner) { + cov_mark::hit!(type_var_cycles_resolve_as_possible); + // recursive type + return self.type_variable_table.fallback_value(tv, kind); + } + if let Some(known_ty) = + self.var_unification_table.inlined_probe_value(inner).known() + { + // known_ty may contain other variables that are known by now + tv_stack.push(inner); + let result = self.resolve_ty_as_possible_inner(tv_stack, known_ty.clone()); + tv_stack.pop(); + result + } else { + ty + } } - } - _ => ty, - }) + _ => ty, + }, + DebruijnIndex::INNERMOST, + ) } /// Resolves the type completely; type variables without known type are /// replaced by TyKind::Unknown. fn resolve_ty_completely_inner(&mut self, tv_stack: &mut Vec, ty: Ty) -> Ty { - ty.fold(&mut |ty| match ty.kind(&Interner) { - &TyKind::InferenceVar(tv, kind) => { - let inner = from_inference_var(tv); - if tv_stack.contains(&inner) { - cov_mark::hit!(type_var_cycles_resolve_completely); - // recursive type - return self.type_variable_table.fallback_value(tv, kind); - } - if let Some(known_ty) = - self.var_unification_table.inlined_probe_value(inner).known() - { - // known_ty may contain other variables that are known by now - tv_stack.push(inner); - let result = self.resolve_ty_completely_inner(tv_stack, known_ty.clone()); - tv_stack.pop(); - result - } else { - self.type_variable_table.fallback_value(tv, kind) + fold_tys( + ty, + |ty, _| match ty.kind(&Interner) { + &TyKind::InferenceVar(tv, kind) => { + let inner = from_inference_var(tv); + if tv_stack.contains(&inner) { + cov_mark::hit!(type_var_cycles_resolve_completely); + // recursive type + return self.type_variable_table.fallback_value(tv, kind); + } + if let Some(known_ty) = + self.var_unification_table.inlined_probe_value(inner).known() + { + // known_ty may contain other variables that are known by now + tv_stack.push(inner); + let result = self.resolve_ty_completely_inner(tv_stack, known_ty.clone()); + tv_stack.pop(); + result + } else { + self.type_variable_table.fallback_value(tv, kind) + } } - } - _ => ty, - }) + _ => ty, + }, + DebruijnIndex::INNERMOST, + ) } } diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 60680ec86..f7af8b8aa 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -328,3 +328,30 @@ pub(crate) fn fold_free_vars + Fold + Fold>( + t: T, + f: impl FnMut(Ty, DebruijnIndex) -> Ty, + binders: DebruijnIndex, +) -> T::Result { + use chalk_ir::{ + fold::{Folder, SuperFold}, + Fallible, + }; + struct TyFolder(F); + impl<'i, F: FnMut(Ty, DebruijnIndex) -> Ty + 'i> Folder<'i, Interner> for TyFolder { + fn as_dyn(&mut self) -> &mut dyn Folder<'i, Interner> { + self + } + + fn interner(&self) -> &'i Interner { + &Interner + } + + fn fold_ty(&mut self, ty: Ty, outer_binder: DebruijnIndex) -> Fallible { + let ty = ty.super_fold_with(self.as_dyn(), outer_binder)?; + Ok(self.0(ty, outer_binder)) + } + } + t.fold_with(&mut TyFolder(f), binders).expect("fold failed unexpectedly") +} -- cgit v1.2.3 From f43edb2151408d0127201312ade7fef9d9328222 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 13:32:56 +0200 Subject: Fix remaining `interned_mut` call --- crates/hir_ty/src/infer/coerce.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index fd679f444..26dc6ceba 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs @@ -71,12 +71,14 @@ impl<'a> InferenceContext<'a> { } // Pointer weakening and function to pointer - match (from_ty.interned_mut(), to_ty.kind(&Interner)) { + match (from_ty.kind(&Interner), to_ty.kind(&Interner)) { // `*mut T` -> `*const T` + (TyKind::Raw(m1, inner), TyKind::Raw(m2 @ Mutability::Not, ..)) => { + from_ty = TyKind::Raw(*m2, inner.clone()).intern(&Interner); + } // `&mut T` -> `&T` - (TyKind::Raw(m1, ..), TyKind::Raw(m2 @ Mutability::Not, ..)) - | (TyKind::Ref(m1, ..), TyKind::Ref(m2 @ Mutability::Not, ..)) => { - *m1 = *m2; + (TyKind::Ref(m1, lt, inner), TyKind::Ref(m2 @ Mutability::Not, ..)) => { + from_ty = TyKind::Ref(*m2, lt.clone(), inner.clone()).intern(&Interner); } // `&T` -> `*const T` // `&mut T` -> `*mut T`/`*const T` -- cgit v1.2.3 From a838a60caaa5351d7543bcbebb1aa976b0b73f39 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 13:51:04 +0200 Subject: Fix missing match arms --- crates/hir/src/lib.rs | 4 +++- crates/hir_ty/src/display.rs | 18 ++++++++++++++++-- crates/hir_ty/src/infer/unify.rs | 1 + crates/hir_ty/src/traits.rs | 1 + crates/hir_ty/src/walk.rs | 2 ++ 5 files changed, 23 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 0afc06906..817a01db1 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1903,7 +1903,9 @@ impl Type { | TyKind::Dyn(_) | TyKind::Function(_) | TyKind::Alias(_) - | TyKind::Foreign(_) => false, + | TyKind::Foreign(_) + | TyKind::Generator(..) + | TyKind::GeneratorWitness(..) => false, } } } diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index e0ca96c6d..d7a3977e5 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -287,6 +287,8 @@ impl HirDisplay for GenericArg { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { match self.interned() { crate::GenericArgData::Ty(ty) => ty.hir_fmt(f), + crate::GenericArgData::Lifetime(lt) => lt.hir_fmt(f), + crate::GenericArgData::Const(c) => c.hir_fmt(f), } } } @@ -664,6 +666,8 @@ impl HirDisplay for Ty { write!(f, "{{unknown}}")?; } TyKind::InferenceVar(..) => write!(f, "_")?, + TyKind::Generator(..) => write!(f, "{{generator}}")?, + TyKind::GeneratorWitness(..) => write!(f, "{{generator witness}}")?, } Ok(()) } @@ -741,7 +745,7 @@ fn write_bounds_like_dyn_trait( if !first { write!(f, " + ")?; } - // We assume that the self type is $0 (i.e. the + // We assume that the self type is ^0.0 (i.e. the // existential) here, which is the only thing that's // possible in actual Rust, and hence don't print it write!(f, "{}", f.db.trait_data(trait_).name)?; @@ -783,6 +787,10 @@ fn write_bounds_like_dyn_trait( } ty.hir_fmt(f)?; } + + // FIXME implement these + WhereClause::LifetimeOutlives(_) => {} + WhereClause::TypeOutlives(_) => {} } first = false; } @@ -837,6 +845,10 @@ impl HirDisplay for WhereClause { ty.hir_fmt(f)?; } WhereClause::AliasEq(_) => write!(f, "{{error}}")?, + + // FIXME implement these + WhereClause::TypeOutlives(..) => {} + WhereClause::LifetimeOutlives(..) => {} } Ok(()) } @@ -881,9 +893,11 @@ impl HirDisplay for DomainGoal { DomainGoal::Holds(wc) => { write!(f, "Holds(")?; wc.hir_fmt(f)?; - write!(f, ")") + write!(f, ")")?; } + _ => write!(f, "?")?, } + Ok(()) } } diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index ffa324b6d..648af4ebf 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -116,6 +116,7 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { DomainGoal::Holds(wc) => { DomainGoal::Holds(self.do_canonicalize(wc, DebruijnIndex::INNERMOST)) } + _ => unimplemented!(), }; self.into_canonicalized(InEnvironment { goal: result, environment: obligation.environment }) } diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs index cc1d2ca18..de1d37e17 100644 --- a/crates/hir_ty/src/traits.rs +++ b/crates/hir_ty/src/traits.rs @@ -81,6 +81,7 @@ pub(crate) fn trait_solve_query( db.trait_data(it.hir_trait_id()).name.to_string() } DomainGoal::Holds(WhereClause::AliasEq(_)) => "alias_eq".to_string(), + _ => "??".to_string(), }); log::info!("trait_solve_query({})", goal.value.goal.display(db)); diff --git a/crates/hir_ty/src/walk.rs b/crates/hir_ty/src/walk.rs index 28875dbe0..57343fd61 100644 --- a/crates/hir_ty/src/walk.rs +++ b/crates/hir_ty/src/walk.rs @@ -93,6 +93,7 @@ impl TypeWalk for GenericArg { GenericArgData::Ty(ty) => { ty.walk(f); } + _ => {} } } } @@ -122,6 +123,7 @@ impl TypeWalk for WhereClause { match self { WhereClause::Implemented(trait_ref) => trait_ref.walk(f), WhereClause::AliasEq(alias_eq) => alias_eq.walk(f), + _ => {} } } } -- cgit v1.2.3 From a163554857db677e892c211b41deda676a6d1511 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 13:58:03 +0200 Subject: Fix `Canonicalized::apply_solution` Now that we're using Chalk's `substitute` which actually knows about lifetimes, the hack doesn't work anymore, but we can put in a proper lifetime. --- crates/hir_ty/src/infer/unify.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 648af4ebf..3b9c4a495 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -3,15 +3,16 @@ use std::borrow::Cow; use chalk_ir::{ - fold::Fold, interner::HasInterner, FloatTy, IntTy, TyVariableKind, UniverseIndex, VariableKind, + cast::Cast, fold::Fold, interner::HasInterner, FloatTy, IntTy, TyVariableKind, UniverseIndex, + VariableKind, }; use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; use super::{DomainGoal, InferenceContext}; use crate::{ - fold_tys, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, - FnSubst, InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyExt, TyKind, - TypeWalk, WhereClause, + fold_tys, static_lifetime, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, + DebruijnIndex, FnPointer, FnSubst, InEnvironment, InferenceVar, Interner, Scalar, Substitution, + Ty, TyExt, TyKind, TypeWalk, WhereClause, }; impl<'a> InferenceContext<'a> { @@ -139,15 +140,17 @@ impl> Canonicalized { let new_vars = Substitution::from_iter( &Interner, solution.binders.iter(&Interner).map(|k| match k.kind { - VariableKind::Ty(TyVariableKind::General) => ctx.table.new_type_var(), - VariableKind::Ty(TyVariableKind::Integer) => ctx.table.new_integer_var(), - VariableKind::Ty(TyVariableKind::Float) => ctx.table.new_float_var(), - // HACK: Chalk can sometimes return new lifetime variables. We - // want to just skip them, but to not mess up the indices of - // other variables, we'll just create a new type variable in - // their place instead. This should not matter (we never see the - // actual *uses* of the lifetime variable). - VariableKind::Lifetime => ctx.table.new_type_var(), + VariableKind::Ty(TyVariableKind::General) => { + ctx.table.new_type_var().cast(&Interner) + } + VariableKind::Ty(TyVariableKind::Integer) => { + ctx.table.new_integer_var().cast(&Interner) + } + VariableKind::Ty(TyVariableKind::Float) => { + ctx.table.new_float_var().cast(&Interner) + } + // Chalk can sometimes return new lifetime variables. We just use the static lifetime everywhere + VariableKind::Lifetime => static_lifetime().cast(&Interner), _ => panic!("const variable in solution"), }), ); -- cgit v1.2.3 From 8ce6fea325c001deeed2857da560fa5cfbc6eea3 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 14:16:05 +0200 Subject: Remove identity impls for ToChalk --- crates/hir_ty/src/lower.rs | 5 +- crates/hir_ty/src/traits.rs | 2 +- crates/hir_ty/src/traits/chalk.rs | 38 +++--- crates/hir_ty/src/traits/chalk/mapping.rs | 191 +----------------------------- 4 files changed, 21 insertions(+), 215 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 109157a5e..6584bcf34 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -950,8 +950,7 @@ pub(crate) fn trait_environment_query( traits_in_scope .push((tr.self_type_parameter(&Interner).clone(), tr.hir_trait_id())); } - let program_clause: chalk_ir::ProgramClause = - pred.clone().to_chalk(db).cast(&Interner); + let program_clause: chalk_ir::ProgramClause = pred.clone().cast(&Interner); clauses.push(program_clause.into_from_env_clause(&Interner)); } } @@ -974,7 +973,7 @@ pub(crate) fn trait_environment_query( let substs = TyBuilder::type_params_subst(db, trait_id); let trait_ref = TraitRef { trait_id: to_chalk_trait_id(trait_id), substitution: substs }; let pred = WhereClause::Implemented(trait_ref); - let program_clause: chalk_ir::ProgramClause = pred.to_chalk(db).cast(&Interner); + let program_clause: chalk_ir::ProgramClause = pred.cast(&Interner); clauses.push(program_clause.into_from_env_clause(&Interner)); } diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs index de1d37e17..63d87fe61 100644 --- a/crates/hir_ty/src/traits.rs +++ b/crates/hir_ty/src/traits.rs @@ -96,7 +96,7 @@ pub(crate) fn trait_solve_query( } } - let canonical = goal.to_chalk(db).cast(&Interner); + let canonical = goal.cast(&Interner); // We currently don't deal with universes (I think / hope they're not yet // relevant for our use cases?) diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index a5aa40c10..fcdcc2b4d 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -86,7 +86,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { debug!("impls_for_trait {:?}", trait_id); let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); - let ty: Ty = from_chalk(self.db, parameters[0].assert_ty_ref(&Interner).clone()); + let ty: Ty = parameters[0].assert_ty_ref(&Interner).clone(); fn binder_kind( ty: &Ty, @@ -187,15 +187,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { let (datas, binders) = (*datas).as_ref().into_value_and_skipped_binders(); let data = &datas.impl_traits[idx as usize]; let bound = OpaqueTyDatumBound { - bounds: make_binders( - data.bounds - .skip_binders() - .iter() - .cloned() - .map(|b| b.to_chalk(self.db)) - .collect(), - 1, - ), + bounds: make_binders(data.bounds.skip_binders().iter().cloned().collect(), 1), where_clauses: make_binders(vec![], 0), }; chalk_ir::Binders::new(binders, bound) @@ -246,8 +238,8 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { let bound = OpaqueTyDatumBound { bounds: make_binders( vec![ - crate::wrap_empty_binders(impl_bound).to_chalk(self.db), - crate::wrap_empty_binders(proj_bound).to_chalk(self.db), + crate::wrap_empty_binders(impl_bound), + crate::wrap_empty_binders(proj_bound), ], 1, ), @@ -272,7 +264,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { fn hidden_opaque_type(&self, _id: chalk_ir::OpaqueTyId) -> chalk_ir::Ty { // FIXME: actually provide the hidden type; it is relevant for auto traits - TyKind::Error.intern(&Interner).to_chalk(self.db) + TyKind::Error.intern(&Interner) } fn is_object_safe(&self, _trait_id: chalk_ir::TraitId) -> bool { @@ -293,12 +285,11 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { _closure_id: chalk_ir::ClosureId, substs: &chalk_ir::Substitution, ) -> chalk_ir::Binders> { - let sig_ty: Ty = - from_chalk(self.db, substs.at(&Interner, 0).assert_ty_ref(&Interner).clone()); + let sig_ty = substs.at(&Interner, 0).assert_ty_ref(&Interner).clone(); let sig = &sig_ty.callable_sig(self.db).expect("first closure param should be fn ptr"); let io = rust_ir::FnDefInputsAndOutputDatum { - argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(self.db)).collect(), - return_type: sig.ret().clone().to_chalk(self.db), + argument_types: sig.params().iter().cloned().collect(), + return_type: sig.ret().clone(), }; make_binders(io.shifted_in(&Interner), 0) } @@ -307,7 +298,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { _closure_id: chalk_ir::ClosureId, _substs: &chalk_ir::Substitution, ) -> chalk_ir::Binders> { - let ty = TyBuilder::unit().to_chalk(self.db); + let ty = TyBuilder::unit(); make_binders(ty, 0) } fn closure_fn_substitution( @@ -315,7 +306,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { _closure_id: chalk_ir::ClosureId, _substs: &chalk_ir::Substitution, ) -> chalk_ir::Substitution { - Substitution::empty(&Interner).to_chalk(self.db) + Substitution::empty(&Interner) } fn trait_name(&self, trait_id: chalk_ir::TraitId) -> String { @@ -410,7 +401,7 @@ pub(crate) fn associated_ty_data_query( let where_clauses = convert_where_clauses(db, type_alias.into(), &bound_vars); let bound_data = rust_ir::AssociatedTyDatumBound { bounds, where_clauses }; let datum = AssociatedTyDatum { - trait_id: trait_.to_chalk(db), + trait_id: to_chalk_trait_id(trait_), id, name: type_alias, binders: make_binders(bound_data, generic_params.len()), @@ -563,7 +554,6 @@ fn impl_def_datum( trait_ref.display(db), where_clauses ); - let trait_ref = trait_ref.to_chalk(db); let polarity = if negative { rust_ir::Polarity::Negative } else { rust_ir::Polarity::Positive }; @@ -624,7 +614,7 @@ fn type_alias_associated_ty_value( .associated_type_by_name(&type_alias_data.name) .expect("assoc ty value should not exist"); // validated when building the impl data as well let (ty, binders) = db.ty(type_alias.into()).into_value_and_skipped_binders(); - let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) }; + let value_bound = rust_ir::AssociatedTyValueBound { ty }; let value = rust_ir::AssociatedTyValue { impl_id: impl_id.to_chalk(db), associated_ty_id: to_assoc_type_id(assoc_ty), @@ -647,8 +637,8 @@ pub(crate) fn fn_def_datum_query( // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway inputs_and_output: make_binders( rust_ir::FnDefInputsAndOutputDatum { - argument_types: sig.params().iter().map(|ty| ty.clone().to_chalk(db)).collect(), - return_type: sig.ret().clone().to_chalk(db), + argument_types: sig.params().iter().cloned().collect(), + return_type: sig.ret().clone(), } .shifted_in(&Interner), 0, diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 62cf0fecf..54783b58c 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -18,55 +18,6 @@ use crate::{ use super::interner::*; use super::*; -impl ToChalk for Ty { - type Chalk = chalk_ir::Ty; - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty { - self - } - fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty) -> Self { - chalk - } -} - -impl ToChalk for GenericArg { - type Chalk = chalk_ir::GenericArg; - - fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk { - self - } - - fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { - chalk - } -} - -impl ToChalk for Substitution { - type Chalk = chalk_ir::Substitution; - - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution { - self - } - - fn from_chalk( - db: &dyn HirDatabase, - parameters: chalk_ir::Substitution, - ) -> Substitution { - parameters - } -} - -impl ToChalk for TraitRef { - type Chalk = chalk_ir::TraitRef; - - fn to_chalk(self: TraitRef, db: &dyn HirDatabase) -> chalk_ir::TraitRef { - self - } - - fn from_chalk(db: &dyn HirDatabase, trait_ref: chalk_ir::TraitRef) -> Self { - trait_ref - } -} - impl ToChalk for hir_def::TraitId { type Chalk = TraitId; @@ -120,140 +71,6 @@ impl ToChalk for TypeAliasAsValue { } } -impl ToChalk for WhereClause { - type Chalk = chalk_ir::WhereClause; - - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::WhereClause { - self - } - - fn from_chalk( - db: &dyn HirDatabase, - where_clause: chalk_ir::WhereClause, - ) -> WhereClause { - where_clause - } -} - -impl ToChalk for ProjectionTy { - type Chalk = chalk_ir::ProjectionTy; - - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy { - self - } - - fn from_chalk( - db: &dyn HirDatabase, - projection_ty: chalk_ir::ProjectionTy, - ) -> ProjectionTy { - projection_ty - } -} -impl ToChalk for OpaqueTy { - type Chalk = chalk_ir::OpaqueTy; - - fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk { - self - } - - fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { - chalk - } -} - -impl ToChalk for AliasTy { - type Chalk = chalk_ir::AliasTy; - - fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk { - self - } - - fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { - chalk - } -} - -impl ToChalk for AliasEq { - type Chalk = chalk_ir::AliasEq; - - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::AliasEq { - self - } - - fn from_chalk(db: &dyn HirDatabase, alias_eq: chalk_ir::AliasEq) -> Self { - alias_eq - } -} - -impl ToChalk for DomainGoal { - type Chalk = chalk_ir::DomainGoal; - - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::DomainGoal { - self - } - - fn from_chalk(_db: &dyn HirDatabase, goal: chalk_ir::DomainGoal) -> Self { - goal - } -} - -impl ToChalk for Canonical -where - T: HasInterner, -{ - type Chalk = chalk_ir::Canonical; - - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical { - self - } - - fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical) -> Canonical { - canonical - } -} - -impl ToChalk for InEnvironment -where - T: HasInterner, -{ - type Chalk = chalk_ir::InEnvironment; - - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::InEnvironment { - self - } - - fn from_chalk(_db: &dyn HirDatabase, in_env: chalk_ir::InEnvironment) -> InEnvironment { - in_env - } -} - -impl ToChalk for crate::Binders -where - T: HasInterner, -{ - type Chalk = chalk_ir::Binders; - - fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Binders { - self - } - - fn from_chalk(db: &dyn HirDatabase, binders: chalk_ir::Binders) -> crate::Binders { - binders - } -} - -impl ToChalk for crate::ConstrainedSubst { - type Chalk = chalk_ir::ConstrainedSubst; - - fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk { - self - } - - fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { - chalk - } -} - pub(super) fn make_binders(value: T, num_vars: usize) -> chalk_ir::Binders where T: HasInterner, @@ -276,7 +93,7 @@ pub(super) fn convert_where_clauses( let generic_predicates = db.generic_predicates(def); let mut result = Vec::with_capacity(generic_predicates.len()); for pred in generic_predicates.iter() { - result.push(pred.clone().substitute(&Interner, substs).to_chalk(db)); + result.push(pred.clone().substitute(&Interner, substs)); } result } @@ -299,7 +116,7 @@ pub(super) fn generic_predicate_to_inline_bound( } let args_no_self = trait_ref.substitution.interned()[1..] .iter() - .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) + .map(|ty| ty.clone().cast(&Interner)) .collect(); let trait_bound = rust_ir::TraitBound { trait_id: trait_ref.trait_id, args_no_self }; Some(chalk_ir::Binders::new(binders, rust_ir::InlineBound::TraitBound(trait_bound))) @@ -311,10 +128,10 @@ pub(super) fn generic_predicate_to_inline_bound( let trait_ = projection_ty.trait_(db); let args_no_self = projection_ty.substitution.interned()[1..] .iter() - .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) + .map(|ty| ty.clone().cast(&Interner)) .collect(); let alias_eq_bound = rust_ir::AliasEqBound { - value: ty.clone().to_chalk(db), + value: ty.clone(), trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, associated_ty_id: projection_ty.associated_ty_id, parameters: Vec::new(), // FIXME we don't support generic associated types yet -- cgit v1.2.3 From 8040f4a5e3792f95b3194e21b3f6d375fb7499c5 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 14:21:22 +0200 Subject: Replace `make_binders` by the now equivalent `make_only_type_binders` --- crates/hir_ty/src/traits/chalk.rs | 43 ++++++++++++++++--------------- crates/hir_ty/src/traits/chalk/mapping.rs | 14 ---------- 2 files changed, 22 insertions(+), 35 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index fcdcc2b4d..e2a54fd92 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -17,16 +17,14 @@ use super::ChalkContext; use crate::{ db::HirDatabase, display::HirDisplay, - from_assoc_type_id, + from_assoc_type_id, make_only_type_binders, method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, to_assoc_type_id, to_chalk_trait_id, utils::generics, AliasEq, AliasTy, BoundVar, CallableDefId, DebruijnIndex, FnDefId, ProjectionTy, Substitution, TraitRef, TraitRefExt, Ty, TyBuilder, TyExt, TyKind, WhereClause, }; -use mapping::{ - convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, -}; +use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, TypeAliasAsValue}; pub use self::interner::Interner; pub(crate) use self::interner::*; @@ -187,8 +185,11 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { let (datas, binders) = (*datas).as_ref().into_value_and_skipped_binders(); let data = &datas.impl_traits[idx as usize]; let bound = OpaqueTyDatumBound { - bounds: make_binders(data.bounds.skip_binders().iter().cloned().collect(), 1), - where_clauses: make_binders(vec![], 0), + bounds: make_only_type_binders( + 1, + data.bounds.skip_binders().iter().cloned().collect(), + ), + where_clauses: make_only_type_binders(0, vec![]), }; chalk_ir::Binders::new(binders, bound) } @@ -236,25 +237,25 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { .intern(&Interner), }); let bound = OpaqueTyDatumBound { - bounds: make_binders( + bounds: make_only_type_binders( + 1, vec![ crate::wrap_empty_binders(impl_bound), crate::wrap_empty_binders(proj_bound), ], - 1, ), - where_clauses: make_binders(vec![], 0), + where_clauses: make_only_type_binders(0, vec![]), }; // The opaque type has 1 parameter. - make_binders(bound, 1) + make_only_type_binders(1, bound) } else { // If failed to find Symbol’s value as variable is void: Future::Output, return empty bounds as fallback. let bound = OpaqueTyDatumBound { - bounds: make_binders(vec![], 0), - where_clauses: make_binders(vec![], 0), + bounds: make_only_type_binders(0, vec![]), + where_clauses: make_only_type_binders(0, vec![]), }; // The opaque type has 1 parameter. - make_binders(bound, 1) + make_only_type_binders(1, bound) } } }; @@ -291,7 +292,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { argument_types: sig.params().iter().cloned().collect(), return_type: sig.ret().clone(), }; - make_binders(io.shifted_in(&Interner), 0) + make_only_type_binders(0, io.shifted_in(&Interner)) } fn closure_upvars( &self, @@ -299,7 +300,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { _substs: &chalk_ir::Substitution, ) -> chalk_ir::Binders> { let ty = TyBuilder::unit(); - make_binders(ty, 0) + make_only_type_binders(0, ty) } fn closure_fn_substitution( &self, @@ -404,7 +405,7 @@ pub(crate) fn associated_ty_data_query( trait_id: to_chalk_trait_id(trait_), id, name: type_alias, - binders: make_binders(bound_data, generic_params.len()), + binders: make_only_type_binders(generic_params.len(), bound_data), }; Arc::new(datum) } @@ -437,7 +438,7 @@ pub(crate) fn trait_datum_query( lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); let trait_datum = TraitDatum { id: trait_id, - binders: make_binders(trait_datum_bound, bound_vars.len(&Interner)), + binders: make_only_type_binders(bound_vars.len(&Interner), trait_datum_bound), flags, associated_ty_ids, well_known, @@ -506,7 +507,7 @@ pub(crate) fn struct_datum_query( // FIXME set ADT kind kind: rust_ir::AdtKind::Struct, id: struct_id, - binders: make_binders(struct_datum_bound, num_params), + binders: make_only_type_binders(num_params, struct_datum_bound), flags, }; Arc::new(struct_datum) @@ -575,7 +576,7 @@ fn impl_def_datum( .collect(); debug!("impl_datum: {:?}", impl_datum_bound); let impl_datum = ImplDatum { - binders: make_binders(impl_datum_bound, bound_vars.len(&Interner)), + binders: make_only_type_binders(bound_vars.len(&Interner), impl_datum_bound), impl_type, polarity, associated_ty_value_ids, @@ -635,13 +636,13 @@ pub(crate) fn fn_def_datum_query( let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); let bound = rust_ir::FnDefDatumBound { // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway - inputs_and_output: make_binders( + inputs_and_output: make_only_type_binders( + 0, rust_ir::FnDefInputsAndOutputDatum { argument_types: sig.params().iter().cloned().collect(), return_type: sig.ret().clone(), } .shifted_in(&Interner), - 0, ), where_clauses, }; diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 54783b58c..4abc0fbf2 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -71,20 +71,6 @@ impl ToChalk for TypeAliasAsValue { } } -pub(super) fn make_binders(value: T, num_vars: usize) -> chalk_ir::Binders -where - T: HasInterner, -{ - chalk_ir::Binders::new( - chalk_ir::VariableKinds::from_iter( - &Interner, - std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General)) - .take(num_vars), - ), - value, - ) -} - pub(super) fn convert_where_clauses( db: &dyn HirDatabase, def: GenericDefId, -- cgit v1.2.3 From d992736e796501b2a5ae232644924a3dfefede92 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 8 Apr 2021 14:35:15 +0200 Subject: Remove unused --- crates/hir_ty/src/builder.rs | 2 +- crates/hir_ty/src/chalk_cast.rs | 20 ++------------------ crates/hir_ty/src/infer.rs | 1 - crates/hir_ty/src/infer/coerce.rs | 4 ++-- crates/hir_ty/src/infer/expr.rs | 1 - crates/hir_ty/src/infer/unify.rs | 2 +- crates/hir_ty/src/lib.rs | 3 --- crates/hir_ty/src/lower.rs | 2 +- crates/hir_ty/src/method_resolution.rs | 10 +++------- crates/hir_ty/src/traits.rs | 12 ++---------- crates/hir_ty/src/traits/chalk.rs | 2 +- crates/hir_ty/src/traits/chalk/mapping.rs | 7 +++---- crates/hir_ty/src/utils.rs | 14 +------------- crates/hir_ty/src/walk.rs | 9 +++------ 14 files changed, 20 insertions(+), 69 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/builder.rs b/crates/hir_ty/src/builder.rs index 9a698898b..e25ef866d 100644 --- a/crates/hir_ty/src/builder.rs +++ b/crates/hir_ty/src/builder.rs @@ -14,7 +14,7 @@ use smallvec::SmallVec; use crate::{ db::HirDatabase, primitive, to_assoc_type_id, to_chalk_trait_id, utils::generics, Binders, CallableSig, FnPointer, FnSig, FnSubst, GenericArg, Interner, ProjectionTy, Substitution, - TraitRef, Ty, TyDefId, TyExt, TyKind, TypeWalk, ValueTyDefId, + TraitRef, Ty, TyDefId, TyExt, TyKind, ValueTyDefId, }; /// This is a builder for `Ty` or anything that needs a `Substitution`. diff --git a/crates/hir_ty/src/chalk_cast.rs b/crates/hir_ty/src/chalk_cast.rs index 8bdd32ad6..f27dee3fd 100644 --- a/crates/hir_ty/src/chalk_cast.rs +++ b/crates/hir_ty/src/chalk_cast.rs @@ -1,14 +1,8 @@ //! Implementations of the Chalk `Cast` trait for our types. -use chalk_ir::{ - cast::{Cast, CastTo}, - interner::HasInterner, -}; +use chalk_ir::interner::HasInterner; -use crate::{ - AliasEq, CallableSig, DomainGoal, GenericArg, GenericArgData, Interner, PolyFnSig, - ReturnTypeImplTraits, TraitRef, Ty, WhereClause, -}; +use crate::{CallableSig, ReturnTypeImplTraits}; macro_rules! has_interner { ($t:ty) => { @@ -18,15 +12,5 @@ macro_rules! has_interner { }; } -macro_rules! transitive_impl { - ($a:ty, $b:ty, $c:ty) => { - impl CastTo<$c> for $a { - fn cast_to(self, interner: &Interner) -> $c { - self.cast::<$b>(interner).cast(interner) - } - } - }; -} - has_interner!(CallableSig); has_interner!(ReturnTypeImplTraits); diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index 1d8184ef5..bf2da2d4a 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -38,7 +38,6 @@ use syntax::SmolStr; use super::{ DomainGoal, Guidance, InEnvironment, ProjectionTy, Solution, TraitEnvironment, TraitRef, Ty, - TypeWalk, }; use crate::{ db::HirDatabase, fold_tys, infer::diagnostics::InferenceDiagnostic, diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index 26dc6ceba..1f463a425 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs @@ -73,11 +73,11 @@ impl<'a> InferenceContext<'a> { // Pointer weakening and function to pointer match (from_ty.kind(&Interner), to_ty.kind(&Interner)) { // `*mut T` -> `*const T` - (TyKind::Raw(m1, inner), TyKind::Raw(m2 @ Mutability::Not, ..)) => { + (TyKind::Raw(_, inner), TyKind::Raw(m2 @ Mutability::Not, ..)) => { from_ty = TyKind::Raw(*m2, inner.clone()).intern(&Interner); } // `&mut T` -> `&T` - (TyKind::Ref(m1, lt, inner), TyKind::Ref(m2 @ Mutability::Not, ..)) => { + (TyKind::Ref(_, lt, inner), TyKind::Ref(m2 @ Mutability::Not, ..)) => { from_ty = TyKind::Ref(*m2, lt.clone(), inner.clone()).intern(&Interner); } // `&T` -> `*const T` diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 5f03079e8..cbbfa8b5c 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -24,7 +24,6 @@ use crate::{ utils::{generics, Generics}, AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner, ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyExt, TyKind, - TypeWalk, }; use super::{ diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 3b9c4a495..a887e20b0 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -12,7 +12,7 @@ use super::{DomainGoal, InferenceContext}; use crate::{ fold_tys, static_lifetime, AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSubst, InEnvironment, InferenceVar, Interner, Scalar, Substitution, - Ty, TyExt, TyKind, TypeWalk, WhereClause, + Ty, TyExt, TyKind, WhereClause, }; impl<'a> InferenceContext<'a> { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index f7af8b8aa..874c95411 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -1,8 +1,6 @@ //! The type system. We currently use this to infer types for completion, hover //! information and various assists. -#![allow(unused)] - #[allow(unused)] macro_rules! eprintln { ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; @@ -34,7 +32,6 @@ use std::sync::Arc; use base_db::salsa; use chalk_ir::{ - cast::{CastTo, Caster}, fold::{Fold, Shift}, interner::HasInterner, UintTy, diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 6584bcf34..8a22d9ea3 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -35,7 +35,7 @@ use crate::{ AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, FnSubst, ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, - TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, TypeWalk, WhereClause, + TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause, }; #[derive(Debug)] diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 766880b14..be3e4f09a 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -6,11 +6,7 @@ use std::{iter, sync::Arc}; use arrayvec::ArrayVec; use base_db::CrateId; -use chalk_ir::{ - cast::Cast, - fold::{Fold, Folder}, - Fallible, Mutability, UniverseIndex, -}; +use chalk_ir::{cast::Cast, Mutability, UniverseIndex}; use hir_def::{ lang_item::LangItemTarget, nameres::DefMap, AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule, ImplId, Lookup, ModuleId, TraitId, @@ -25,9 +21,9 @@ use crate::{ primitive::{self, FloatTy, IntTy, UintTy}, static_lifetime, utils::all_super_traits, - AdtId, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId, + AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId, InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder, - TyExt, TyKind, TypeWalk, + TyExt, TyKind, }; /// This is used as a key for indexing impls. diff --git a/crates/hir_ty/src/traits.rs b/crates/hir_ty/src/traits.rs index 63d87fe61..1cda72d22 100644 --- a/crates/hir_ty/src/traits.rs +++ b/crates/hir_ty/src/traits.rs @@ -12,7 +12,7 @@ use crate::{ Solution, TraitRefExt, Ty, TyKind, WhereClause, }; -use self::chalk::{from_chalk, Interner, ToChalk}; +use self::chalk::Interner; pub(crate) mod chalk; @@ -101,8 +101,7 @@ pub(crate) fn trait_solve_query( // We currently don't deal with universes (I think / hope they're not yet // relevant for our use cases?) let u_canonical = chalk_ir::UCanonical { canonical, universes: 1 }; - let solution = solve(db, krate, &u_canonical); - solution.map(|solution| solution_from_chalk(db, solution)) + solve(db, krate, &u_canonical) } fn solve( @@ -170,13 +169,6 @@ fn is_chalk_print() -> bool { std::env::var("CHALK_PRINT").is_ok() } -fn solution_from_chalk( - db: &dyn HirDatabase, - solution: chalk_solve::Solution, -) -> Solution { - solution -} - #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum FnTrait { FnOnce, diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index e2a54fd92..b8c390b2e 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use log::debug; -use chalk_ir::{fold::shift::Shift, interner::HasInterner, CanonicalVarKinds}; +use chalk_ir::{fold::shift::Shift, CanonicalVarKinds}; use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait}; use base_db::{salsa::InternKey, CrateId}; diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 4abc0fbf2..7818f6387 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -3,16 +3,15 @@ //! Chalk (in both directions); plus some helper functions for more specialized //! conversions. -use chalk_ir::{cast::Cast, interner::HasInterner}; +use chalk_ir::cast::Cast; use chalk_solve::rust_ir; use base_db::salsa::InternKey; use hir_def::{GenericDefId, TypeAliasId}; use crate::{ - db::HirDatabase, static_lifetime, AliasTy, CallableDefId, Canonical, ConstrainedSubst, - DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy, ProjectionTyExt, - QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, WhereClause, + db::HirDatabase, AliasTy, CallableDefId, ProjectionTyExt, QuantifiedWhereClause, Substitution, + Ty, WhereClause, }; use super::interner::*; diff --git a/crates/hir_ty/src/utils.rs b/crates/hir_ty/src/utils.rs index d26f5152c..5f6cb052a 100644 --- a/crates/hir_ty/src/utils.rs +++ b/crates/hir_ty/src/utils.rs @@ -1,6 +1,5 @@ //! Helper functions for working with def, which don't need to be a separate //! query, but can't be computed directly from `*Data` (ie, which need a `db`). -use std::sync::Arc; use chalk_ir::{fold::Shift, BoundVar, DebruijnIndex}; use hir_def::{ @@ -16,9 +15,7 @@ use hir_def::{ }; use hir_expand::name::{name, Name}; -use crate::{ - db::HirDatabase, Interner, Substitution, TraitRef, TraitRefExt, TyKind, TypeWalk, WhereClause, -}; +use crate::{db::HirDatabase, Interner, Substitution, TraitRef, TraitRefExt, TyKind, WhereClause}; fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec { let resolver = trait_.resolver(db); @@ -137,15 +134,6 @@ pub(super) fn associated_type_by_name_including_super_traits( }) } -/// Helper for mutating `Arc<[T]>` (i.e. `Arc::make_mut` for Arc slices). -/// The underlying values are cloned if there are other strong references. -pub(crate) fn make_mut_slice(a: &mut Arc<[T]>) -> &mut [T] { - if Arc::get_mut(a).is_none() { - *a = a.iter().cloned().collect(); - } - Arc::get_mut(a).unwrap() -} - pub(crate) fn generics(db: &dyn DefDatabase, def: GenericDefId) -> Generics { let parent_generics = parent_generic_def(db, def).map(|def| Box::new(generics(db, def))); Generics { def, params: db.generic_params(def), parent_generics } diff --git a/crates/hir_ty/src/walk.rs b/crates/hir_ty/src/walk.rs index 57343fd61..6ef1d5336 100644 --- a/crates/hir_ty/src/walk.rs +++ b/crates/hir_ty/src/walk.rs @@ -1,14 +1,11 @@ //! The `TypeWalk` trait (probably to be replaced by Chalk's `Fold` and //! `Visit`). -use std::mem; - -use chalk_ir::{interner::HasInterner, DebruijnIndex}; +use chalk_ir::interner::HasInterner; use crate::{ - utils::make_mut_slice, AliasEq, AliasTy, Binders, CallableSig, FnSubst, GenericArg, - GenericArgData, Interner, OpaqueTy, ProjectionTy, Substitution, TraitRef, Ty, TyKind, - WhereClause, + AliasEq, AliasTy, Binders, CallableSig, FnSubst, GenericArg, GenericArgData, Interner, + OpaqueTy, ProjectionTy, Substitution, TraitRef, Ty, TyKind, WhereClause, }; /// This allows walking structures that contain types to do something with those -- cgit v1.2.3 From 3634b2145c66316a9413dda93f9081dc32ecbec2 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Thu, 8 Apr 2021 10:57:47 -0400 Subject: Bump lsp-types and syn This lsp-types now supports a default InsertTextMode for completion and a per-completion item commit_characters --- crates/rust-analyzer/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 3130785cc..0571a912c 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -22,7 +22,7 @@ env_logger = { version = "0.8.1", default-features = false } itertools = "0.10.0" jod-thread = "0.1.0" log = "0.4.8" -lsp-types = { version = "0.88.0", features = ["proposed"] } +lsp-types = { version = "0.89.0", features = ["proposed"] } parking_lot = "0.11.0" xflags = "0.2.1" oorandom = "11.1.2" -- cgit v1.2.3