7813: Inline TypeCtor into Ty r=flodiebold a=Veykril

This removes the `ApplicationTy` variant from `Ty` bringing the representation a lot closer to chalk's `TyKind`.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-02-28 19:16:51 +00:00 committed by GitHub
commit 2fc137b70f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 710 additions and 944 deletions

View File

@ -31,9 +31,9 @@
display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter},
method_resolution, method_resolution,
traits::{FnTrait, Solution, SolutionVariables}, traits::{FnTrait, Solution, SolutionVariables},
ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, InEnvironment,
InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, Ty, TyDefId,
Ty, TyDefId, TyKind, TypeCtor, TyKind,
}; };
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use stdx::{format_to, impl_from}; use stdx::{format_to, impl_from};
@ -1547,28 +1547,19 @@ fn from_def(
} }
pub fn is_unit(&self) -> bool { pub fn is_unit(&self) -> bool {
matches!( matches!(self.ty.value, Ty::Tuple { cardinality: 0, .. })
self.ty.value,
Ty::Apply(ApplicationTy { ctor: TypeCtor::Tuple { cardinality: 0 }, .. })
)
} }
pub fn is_bool(&self) -> bool { pub fn is_bool(&self) -> bool {
matches!( matches!(self.ty.value, Ty::Scalar(Scalar::Bool))
self.ty.value,
Ty::Apply(ApplicationTy { ctor: TypeCtor::Scalar(Scalar::Bool), .. })
)
} }
pub fn is_mutable_reference(&self) -> bool { pub fn is_mutable_reference(&self) -> bool {
matches!( matches!(self.ty.value, Ty::Ref(Mutability::Mut, ..))
self.ty.value,
Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(Mutability::Mut), .. })
)
} }
pub fn remove_ref(&self) -> Option<Type> { pub fn remove_ref(&self) -> Option<Type> {
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(_), .. }) = self.ty.value { if let Ty::Ref(.., substs) = &self.ty.value {
self.ty.value.substs().map(|substs| self.derived(substs[0].clone())) Some(self.derived(substs[0].clone()))
} else { } else {
None None
} }
@ -1688,7 +1679,7 @@ pub fn is_copy(&self, db: &dyn HirDatabase) -> bool {
pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> { pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> {
let def = match self.ty.value { let def = match self.ty.value {
Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(def), parameters: _ }) => Some(def), Ty::FnDef(def, _) => Some(def),
_ => None, _ => None,
}; };
@ -1697,20 +1688,16 @@ pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> {
} }
pub fn is_closure(&self) -> bool { pub fn is_closure(&self) -> bool {
matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Closure { .. }, .. })) matches!(&self.ty.value, Ty::Closure { .. })
} }
pub fn is_fn(&self) -> bool { pub fn is_fn(&self) -> bool {
matches!( matches!(&self.ty.value, Ty::FnDef(..) | Ty::FnPtr { .. })
&self.ty.value,
Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(..), .. })
| Ty::Apply(ApplicationTy { ctor: TypeCtor::FnPtr { .. }, .. })
)
} }
pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
let adt_id = match self.ty.value { let adt_id = match self.ty.value {
Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_id), .. }) => adt_id, Ty::Adt(adt_id, ..) => adt_id,
_ => return false, _ => return false,
}; };
@ -1722,7 +1709,7 @@ pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
} }
pub fn is_raw_ptr(&self) -> bool { pub fn is_raw_ptr(&self) -> bool {
matches!(&self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. })) matches!(&self.ty.value, Ty::RawPtr(..))
} }
pub fn contains_unknown(&self) -> bool { pub fn contains_unknown(&self) -> bool {
@ -1731,44 +1718,34 @@ pub fn contains_unknown(&self) -> bool {
fn go(ty: &Ty) -> bool { fn go(ty: &Ty) -> bool {
match ty { match ty {
Ty::Unknown => true, Ty::Unknown => true,
Ty::Apply(a_ty) => a_ty.parameters.iter().any(go), _ => ty.substs().map_or(false, |substs| substs.iter().any(go)),
_ => false,
} }
} }
} }
pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
if let Ty::Apply(a_ty) = &self.ty.value { let (variant_id, substs) = match self.ty.value {
let variant_id = match a_ty.ctor { Ty::Adt(AdtId::StructId(s), ref substs) => (s.into(), substs),
TypeCtor::Adt(AdtId::StructId(s)) => s.into(), Ty::Adt(AdtId::UnionId(u), ref substs) => (u.into(), substs),
TypeCtor::Adt(AdtId::UnionId(u)) => u.into(), _ => return Vec::new(),
_ => return Vec::new(),
};
return db
.field_types(variant_id)
.iter()
.map(|(local_id, ty)| {
let def = Field { parent: variant_id.into(), id: local_id };
let ty = ty.clone().subst(&a_ty.parameters);
(def, self.derived(ty))
})
.collect();
}; };
Vec::new()
db.field_types(variant_id)
.iter()
.map(|(local_id, ty)| {
let def = Field { parent: variant_id.into(), id: local_id };
let ty = ty.clone().subst(substs);
(def, self.derived(ty))
})
.collect()
} }
pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> {
let mut res = Vec::new(); if let Ty::Tuple { substs, .. } = &self.ty.value {
if let Ty::Apply(a_ty) = &self.ty.value { substs.iter().map(|ty| self.derived(ty.clone())).collect()
if let TypeCtor::Tuple { .. } = a_ty.ctor { } else {
for ty in a_ty.parameters.iter() { Vec::new()
let ty = ty.clone(); }
res.push(self.derived(ty));
}
}
};
res
} }
pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a { pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a {
@ -1805,15 +1782,13 @@ pub fn iterate_assoc_items<T>(
} }
pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ {
let ty = self.ty.value.strip_references(); self.ty
let substs = match ty { .value
Ty::Apply(apply_ty) => &apply_ty.parameters, .strip_references()
Ty::Opaque(opaque_ty) => &opaque_ty.parameters, .substs()
_ => return Either::Left(iter::empty()), .into_iter()
}; .flat_map(|substs| substs.iter())
.map(move |ty| self.derived(ty.clone()))
let iter = substs.iter().map(move |ty| self.derived(ty.clone()));
Either::Right(iter)
} }
pub fn iterate_method_candidates<T>( pub fn iterate_method_candidates<T>(
@ -1903,17 +1878,8 @@ pub fn as_associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<Tr
// FIXME: provide required accessors such that it becomes implementable from outside. // FIXME: provide required accessors such that it becomes implementable from outside.
pub fn is_equal_for_find_impls(&self, other: &Type) -> bool { pub fn is_equal_for_find_impls(&self, other: &Type) -> bool {
match (&self.ty.value, &other.ty.value) { let rref = other.remove_ref();
(Ty::Apply(a_original_ty), Ty::Apply(ApplicationTy { ctor, parameters })) => match ctor self.ty.value.equals_ctor(rref.as_ref().map_or(&other.ty.value, |it| &it.ty.value))
{
TypeCtor::Ref(..) => match parameters.as_single() {
Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor,
_ => false,
},
_ => a_original_ty.ctor == *ctor,
},
_ => false,
}
} }
fn derived(&self, ty: Ty) -> Type { fn derived(&self, ty: Ty) -> Type {
@ -1958,26 +1924,18 @@ fn walk_bounds(
fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) {
let ty = type_.ty.value.strip_references(); let ty = type_.ty.value.strip_references();
match ty { match ty {
Ty::Apply(ApplicationTy { ctor, parameters }) => { Ty::Adt(..) => {
match ctor { cb(type_.derived(ty.clone()));
TypeCtor::Adt(_) => { }
cb(type_.derived(ty.clone())); Ty::AssociatedType(..) => {
} if let Some(_) = ty.associated_type_parent_trait(db) {
TypeCtor::AssociatedType(_) => { cb(type_.derived(ty.clone()));
if let Some(_) = ty.associated_type_parent_trait(db) { }
cb(type_.derived(ty.clone())); }
} Ty::OpaqueType(..) => {
} if let Some(bounds) = ty.impl_trait_bounds(db) {
TypeCtor::OpaqueType(..) => { walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
if let Some(bounds) = ty.impl_trait_bounds(db) {
walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
}
}
_ => (),
} }
// adt params, tuples, etc...
walk_substs(db, type_, parameters, cb);
} }
Ty::Opaque(opaque_ty) => { Ty::Opaque(opaque_ty) => {
if let Some(bounds) = ty.impl_trait_bounds(db) { if let Some(bounds) = ty.impl_trait_bounds(db) {
@ -1995,7 +1953,10 @@ fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) {
walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb); walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb);
} }
_ => (), _ => {}
}
if let Some(substs) = ty.substs() {
walk_substs(db, type_, &substs, cb);
} }
} }

View File

@ -20,7 +20,7 @@
use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
use hir_ty::{ use hir_ty::{
diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, diagnostics::{record_literal_missing_fields, record_pattern_missing_fields},
InferenceResult, Substs, Ty, InferenceResult, Substs,
}; };
use syntax::{ use syntax::{
ast::{self, AstNode}, ast::{self, AstNode},
@ -299,14 +299,11 @@ pub(crate) fn record_literal_missing_fields(
let infer = self.infer.as_ref()?; let infer = self.infer.as_ref()?;
let expr_id = self.expr_id(db, &literal.clone().into())?; let expr_id = self.expr_id(db, &literal.clone().into())?;
let substs = match &infer.type_of_expr[expr_id] { let substs = infer.type_of_expr[expr_id].substs()?;
Ty::Apply(a_ty) => &a_ty.parameters,
_ => return None,
};
let (variant, missing_fields, _exhaustive) = let (variant, missing_fields, _exhaustive) =
record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?;
let res = self.missing_fields(db, krate, substs, variant, missing_fields); let res = self.missing_fields(db, krate, &substs, variant, missing_fields);
Some(res) Some(res)
} }
@ -320,14 +317,11 @@ pub(crate) fn record_pattern_missing_fields(
let infer = self.infer.as_ref()?; let infer = self.infer.as_ref()?;
let pat_id = self.pat_id(&pattern.clone().into())?; let pat_id = self.pat_id(&pattern.clone().into())?;
let substs = match &infer.type_of_pat[pat_id] { let substs = infer.type_of_pat[pat_id].substs()?;
Ty::Apply(a_ty) => &a_ty.parameters,
_ => return None,
};
let (variant, missing_fields, _exhaustive) = let (variant, missing_fields, _exhaustive) =
record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?;
let res = self.missing_fields(db, krate, substs, variant, missing_fields); let res = self.missing_fields(db, krate, &substs, variant, missing_fields);
Some(res) Some(res)
} }

View File

@ -17,7 +17,7 @@
MissingPatFields, RemoveThisSemicolon, MissingPatFields, RemoveThisSemicolon,
}, },
utils::variant_data, utils::variant_data,
ApplicationTy, InferenceResult, Ty, TypeCtor, InferenceResult, Ty,
}; };
pub(crate) use hir_def::{ pub(crate) use hir_def::{
@ -381,14 +381,11 @@ fn validate_results_in_tail_expr(&mut self, body_id: ExprId, id: ExprId, db: &dy
_ => return, _ => return,
}; };
let core_result_ctor = TypeCtor::Adt(AdtId::EnumId(core_result_enum)); let (params, required) = match mismatch.expected {
let core_option_ctor = TypeCtor::Adt(AdtId::EnumId(core_option_enum)); Ty::Adt(AdtId::EnumId(enum_id), ref parameters) if enum_id == core_result_enum => {
let (params, required) = match &mismatch.expected {
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &core_result_ctor => {
(parameters, "Ok".to_string()) (parameters, "Ok".to_string())
} }
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &core_option_ctor => { Ty::Adt(AdtId::EnumId(enum_id), ref parameters) if enum_id == core_option_enum => {
(parameters, "Some".to_string()) (parameters, "Some".to_string())
} }
_ => return, _ => return,

View File

@ -227,7 +227,7 @@
use la_arena::Idx; use la_arena::Idx;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use crate::{db::HirDatabase, ApplicationTy, InferenceResult, Ty, TypeCtor}; use crate::{db::HirDatabase, InferenceResult, Ty};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
/// Either a pattern from the source code being analyzed, represented as /// Either a pattern from the source code being analyzed, represented as
@ -627,14 +627,12 @@ pub(super) fn is_useful(
// - `!` type // - `!` type
// In those cases, no match arm is useful. // In those cases, no match arm is useful.
match cx.infer[cx.match_expr].strip_references() { match cx.infer[cx.match_expr].strip_references() {
Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(AdtId::EnumId(enum_id)), .. }) => { Ty::Adt(AdtId::EnumId(enum_id), ..) => {
if cx.db.enum_data(*enum_id).variants.is_empty() { if cx.db.enum_data(*enum_id).variants.is_empty() {
return Ok(Usefulness::NotUseful); return Ok(Usefulness::NotUseful);
} }
} }
Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }) => { Ty::Never => return Ok(Usefulness::NotUseful),
return Ok(Usefulness::NotUseful);
}
_ => (), _ => (),
} }

View File

@ -11,9 +11,7 @@
}; };
use hir_expand::diagnostics::DiagnosticSink; use hir_expand::diagnostics::DiagnosticSink;
use crate::{ use crate::{db::HirDatabase, diagnostics::MissingUnsafe, InferenceResult, Ty};
db::HirDatabase, diagnostics::MissingUnsafe, ApplicationTy, InferenceResult, Ty, TypeCtor,
};
pub(super) struct UnsafeValidator<'a, 'b: 'a> { pub(super) struct UnsafeValidator<'a, 'b: 'a> {
owner: DefWithBodyId, owner: DefWithBodyId,
@ -112,7 +110,7 @@ fn walk_unsafe(
} }
} }
Expr::UnaryOp { expr, op: UnaryOp::Deref } => { Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] { if let Ty::RawPtr(..) = &infer[*expr] {
unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block });
} }
} }

View File

@ -3,9 +3,8 @@
use std::{borrow::Cow, fmt}; use std::{borrow::Cow, fmt};
use crate::{ use crate::{
db::HirDatabase, primitive, utils::generics, ApplicationTy, CallableDefId, FnSig, db::HirDatabase, primitive, utils::generics, CallableDefId, FnSig, GenericPredicate, Lifetime,
GenericPredicate, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty,
TraitRef, Ty, TypeCtor,
}; };
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use hir_def::{ use hir_def::{
@ -235,39 +234,62 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
} }
} }
impl HirDisplay for ApplicationTy { impl HirDisplay for ProjectionTy {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
if f.should_truncate() { if f.should_truncate() {
return write!(f, "{}", TYPE_HINT_TRUNCATION); return write!(f, "{}", TYPE_HINT_TRUNCATION);
} }
match self.ctor { let trait_ = f.db.trait_data(self.trait_(f.db));
TypeCtor::Scalar(Scalar::Bool) => write!(f, "bool")?, let first_parameter = self.parameters[0].into_displayable(
TypeCtor::Scalar(Scalar::Char) => write!(f, "char")?, f.db,
TypeCtor::Scalar(Scalar::Float(t)) => { f.max_size,
write!(f, "{}", primitive::float_ty_to_string(t))? f.omit_verbose_types,
} f.display_target,
TypeCtor::Scalar(Scalar::Int(t)) => write!(f, "{}", primitive::int_ty_to_string(t))?, );
TypeCtor::Scalar(Scalar::Uint(t)) => write!(f, "{}", primitive::uint_ty_to_string(t))?, write!(f, "<{} as {}", first_parameter, trait_.name)?;
TypeCtor::Str => write!(f, "str")?, if self.parameters.len() > 1 {
TypeCtor::Slice => { write!(f, "<")?;
let t = self.parameters.as_single(); f.write_joined(&self.parameters[1..], ", ")?;
write!(f, ">")?;
}
write!(f, ">::{}", f.db.type_alias_data(self.associated_ty).name)?;
Ok(())
}
}
impl HirDisplay for Ty {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
if f.should_truncate() {
return write!(f, "{}", TYPE_HINT_TRUNCATION);
}
match self {
Ty::Never => write!(f, "!")?,
Ty::Str => write!(f, "str")?,
Ty::Scalar(Scalar::Bool) => write!(f, "bool")?,
Ty::Scalar(Scalar::Char) => write!(f, "char")?,
&Ty::Scalar(Scalar::Float(t)) => write!(f, "{}", primitive::float_ty_to_string(t))?,
&Ty::Scalar(Scalar::Int(t)) => write!(f, "{}", primitive::int_ty_to_string(t))?,
&Ty::Scalar(Scalar::Uint(t)) => write!(f, "{}", primitive::uint_ty_to_string(t))?,
Ty::Slice(parameters) => {
let t = parameters.as_single();
write!(f, "[")?; write!(f, "[")?;
t.hir_fmt(f)?; t.hir_fmt(f)?;
write!(f, "]")?; write!(f, "]")?;
} }
TypeCtor::Array => { Ty::Array(parameters) => {
let t = self.parameters.as_single(); let t = parameters.as_single();
write!(f, "[")?; write!(f, "[")?;
t.hir_fmt(f)?; t.hir_fmt(f)?;
write!(f, "; _]")?; write!(f, "; _]")?;
} }
TypeCtor::RawPtr(m) | TypeCtor::Ref(m) => { Ty::RawPtr(m, parameters) | Ty::Ref(m, parameters) => {
let t = self.parameters.as_single(); let t = parameters.as_single();
let ty_display = let ty_display =
t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target); t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target);
if matches!(self.ctor, TypeCtor::RawPtr(_)) { if matches!(self, Ty::RawPtr(..)) {
write!(f, "*{}", m.as_keyword_for_ptr())?; write!(f, "*{}", m.as_keyword_for_ptr())?;
} else { } else {
write!(f, "&{}", m.as_keyword_for_ref())?; write!(f, "&{}", m.as_keyword_for_ref())?;
@ -308,25 +330,24 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write!(f, "{}", ty_display)?; write!(f, "{}", ty_display)?;
} }
} }
TypeCtor::Never => write!(f, "!")?, Ty::Tuple { substs, .. } => {
TypeCtor::Tuple { .. } => { if substs.len() == 1 {
let ts = &self.parameters;
if ts.len() == 1 {
write!(f, "(")?; write!(f, "(")?;
ts[0].hir_fmt(f)?; substs[0].hir_fmt(f)?;
write!(f, ",)")?; write!(f, ",)")?;
} else { } else {
write!(f, "(")?; write!(f, "(")?;
f.write_joined(&*ts.0, ", ")?; f.write_joined(&*substs.0, ", ")?;
write!(f, ")")?; write!(f, ")")?;
} }
} }
TypeCtor::FnPtr { is_varargs, .. } => { Ty::FnPtr { is_varargs, substs, .. } => {
let sig = FnSig::from_fn_ptr_substs(&self.parameters, is_varargs); let sig = FnSig::from_fn_ptr_substs(&substs, *is_varargs);
sig.hir_fmt(f)?; sig.hir_fmt(f)?;
} }
TypeCtor::FnDef(def) => { Ty::FnDef(def, parameters) => {
let sig = f.db.callable_item_signature(def).subst(&self.parameters); let def = *def;
let sig = f.db.callable_item_signature(def).subst(parameters);
match def { match def {
CallableDefId::FunctionId(ff) => { CallableDefId::FunctionId(ff) => {
write!(f, "fn {}", f.db.function_data(ff).name)? write!(f, "fn {}", f.db.function_data(ff).name)?
@ -336,7 +357,7 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write!(f, "{}", f.db.enum_data(e.parent).variants[e.local_id].name)? write!(f, "{}", f.db.enum_data(e.parent).variants[e.local_id].name)?
} }
}; };
if self.parameters.len() > 0 { if parameters.len() > 0 {
let generics = generics(f.db.upcast(), def.into()); let generics = generics(f.db.upcast(), def.into());
let (parent_params, self_param, type_params, _impl_trait_params) = let (parent_params, self_param, type_params, _impl_trait_params) =
generics.provenance_split(); generics.provenance_split();
@ -344,7 +365,7 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
// We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self? // We print all params except implicit impl Trait params. Still a bit weird; should we leave out parent and self?
if total_len > 0 { if total_len > 0 {
write!(f, "<")?; write!(f, "<")?;
f.write_joined(&self.parameters.0[..total_len], ", ")?; f.write_joined(&parameters.0[..total_len], ", ")?;
write!(f, ">")?; write!(f, ">")?;
} }
} }
@ -363,10 +384,10 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write!(f, " -> {}", ret_display)?; write!(f, " -> {}", ret_display)?;
} }
} }
TypeCtor::Adt(def_id) => { Ty::Adt(def_id, parameters) => {
match f.display_target { match f.display_target {
DisplayTarget::Diagnostics | DisplayTarget::Test => { DisplayTarget::Diagnostics | DisplayTarget::Test => {
let name = match def_id { let name = match *def_id {
AdtId::StructId(it) => f.db.struct_data(it).name.clone(), AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
AdtId::UnionId(it) => f.db.union_data(it).name.clone(), AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
AdtId::EnumId(it) => f.db.enum_data(it).name.clone(), AdtId::EnumId(it) => f.db.enum_data(it).name.clone(),
@ -376,7 +397,7 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
DisplayTarget::SourceCode { module_id } => { DisplayTarget::SourceCode { module_id } => {
if let Some(path) = find_path::find_path( if let Some(path) = find_path::find_path(
f.db.upcast(), f.db.upcast(),
ItemInNs::Types(def_id.into()), ItemInNs::Types((*def_id).into()),
module_id, module_id,
) { ) {
write!(f, "{}", path)?; write!(f, "{}", path)?;
@ -388,19 +409,18 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
} }
} }
if self.parameters.len() > 0 { if parameters.len() > 0 {
let parameters_to_write = let parameters_to_write =
if f.display_target.is_source_code() || f.omit_verbose_types() { if f.display_target.is_source_code() || f.omit_verbose_types() {
match self match self
.ctor
.as_generic_def() .as_generic_def()
.map(|generic_def_id| f.db.generic_defaults(generic_def_id)) .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
.filter(|defaults| !defaults.is_empty()) .filter(|defaults| !defaults.is_empty())
{ {
None => self.parameters.0.as_ref(), None => parameters.0.as_ref(),
Some(default_parameters) => { Some(default_parameters) => {
let mut default_from = 0; let mut default_from = 0;
for (i, parameter) in self.parameters.iter().enumerate() { for (i, parameter) in parameters.iter().enumerate() {
match (parameter, default_parameters.get(i)) { match (parameter, default_parameters.get(i)) {
(&Ty::Unknown, _) | (_, None) => { (&Ty::Unknown, _) | (_, None) => {
default_from = i + 1; default_from = i + 1;
@ -408,18 +428,18 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
(_, Some(default_parameter)) => { (_, Some(default_parameter)) => {
let actual_default = default_parameter let actual_default = default_parameter
.clone() .clone()
.subst(&self.parameters.prefix(i)); .subst(&parameters.prefix(i));
if parameter != &actual_default { if parameter != &actual_default {
default_from = i + 1; default_from = i + 1;
} }
} }
} }
} }
&self.parameters.0[0..default_from] &parameters.0[0..default_from]
} }
} }
} else { } else {
self.parameters.0.as_ref() parameters.0.as_ref()
}; };
if !parameters_to_write.is_empty() { if !parameters_to_write.is_empty() {
write!(f, "<")?; write!(f, "<")?;
@ -428,61 +448,59 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
} }
} }
} }
TypeCtor::AssociatedType(type_alias) => { Ty::AssociatedType(type_alias, parameters) => {
let trait_ = match type_alias.lookup(f.db.upcast()).container { let trait_ = match type_alias.lookup(f.db.upcast()).container {
AssocContainerId::TraitId(it) => it, AssocContainerId::TraitId(it) => it,
_ => panic!("not an associated type"), _ => panic!("not an associated type"),
}; };
let trait_ = f.db.trait_data(trait_); let trait_ = f.db.trait_data(trait_);
let type_alias_data = f.db.type_alias_data(type_alias); let type_alias_data = f.db.type_alias_data(*type_alias);
// Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
if f.display_target.is_test() { if f.display_target.is_test() {
write!(f, "{}::{}", trait_.name, type_alias_data.name)?; write!(f, "{}::{}", trait_.name, type_alias_data.name)?;
if self.parameters.len() > 0 { if parameters.len() > 0 {
write!(f, "<")?; write!(f, "<")?;
f.write_joined(&*self.parameters.0, ", ")?; f.write_joined(&*parameters.0, ", ")?;
write!(f, ">")?; write!(f, ">")?;
} }
} else { } else {
let projection_ty = ProjectionTy { let projection_ty =
associated_ty: type_alias, ProjectionTy { associated_ty: *type_alias, parameters: parameters.clone() };
parameters: self.parameters.clone(),
};
projection_ty.hir_fmt(f)?; projection_ty.hir_fmt(f)?;
} }
} }
TypeCtor::ForeignType(type_alias) => { Ty::ForeignType(type_alias, parameters) => {
let type_alias = f.db.type_alias_data(type_alias); let type_alias = f.db.type_alias_data(*type_alias);
write!(f, "{}", type_alias.name)?; write!(f, "{}", type_alias.name)?;
if self.parameters.len() > 0 { if parameters.len() > 0 {
write!(f, "<")?; write!(f, "<")?;
f.write_joined(&*self.parameters.0, ", ")?; f.write_joined(&*parameters.0, ", ")?;
write!(f, ">")?; write!(f, ">")?;
} }
} }
TypeCtor::OpaqueType(opaque_ty_id) => { Ty::OpaqueType(opaque_ty_id, parameters) => {
match opaque_ty_id { match opaque_ty_id {
OpaqueTyId::ReturnTypeImplTrait(func, idx) => { &OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
let datas = let datas =
f.db.return_type_impl_traits(func).expect("impl trait id without data"); f.db.return_type_impl_traits(func).expect("impl trait id without data");
let data = (*datas) let data = (*datas)
.as_ref() .as_ref()
.map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
let bounds = data.subst(&self.parameters); let bounds = data.subst(&parameters);
write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?; write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?;
// FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution // FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution
} }
OpaqueTyId::AsyncBlockTypeImplTrait(..) => { OpaqueTyId::AsyncBlockTypeImplTrait(..) => {
write!(f, "impl Future<Output = ")?; write!(f, "impl Future<Output = ")?;
self.parameters[0].hir_fmt(f)?; parameters[0].hir_fmt(f)?;
write!(f, ">")?; write!(f, ">")?;
} }
} }
} }
TypeCtor::Closure { .. } => { Ty::Closure { substs, .. } => {
let sig = self.parameters[0].callable_sig(f.db); let sig = substs[0].callable_sig(f.db);
if let Some(sig) = sig { if let Some(sig) = sig {
if sig.params().is_empty() { if sig.params().is_empty() {
write!(f, "||")?; write!(f, "||")?;
@ -505,43 +523,6 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write!(f, "{{closure}}")?; write!(f, "{{closure}}")?;
} }
} }
}
Ok(())
}
}
impl HirDisplay for ProjectionTy {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
if f.should_truncate() {
return write!(f, "{}", TYPE_HINT_TRUNCATION);
}
let trait_ = f.db.trait_data(self.trait_(f.db));
let first_parameter = self.parameters[0].into_displayable(
f.db,
f.max_size,
f.omit_verbose_types,
f.display_target,
);
write!(f, "<{} as {}", first_parameter, trait_.name)?;
if self.parameters.len() > 1 {
write!(f, "<")?;
f.write_joined(&self.parameters[1..], ", ")?;
write!(f, ">")?;
}
write!(f, ">::{}", f.db.type_alias_data(self.associated_ty).name)?;
Ok(())
}
}
impl HirDisplay for Ty {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
if f.should_truncate() {
return write!(f, "{}", TYPE_HINT_TRUNCATION);
}
match self {
Ty::Apply(a_ty) => a_ty.hir_fmt(f)?,
Ty::Projection(p_ty) => p_ty.hir_fmt(f)?, Ty::Projection(p_ty) => p_ty.hir_fmt(f)?,
Ty::Placeholder(id) => { Ty::Placeholder(id) => {
let generics = generics(f.db.upcast(), id.parent); let generics = generics(f.db.upcast(), id.parent);

View File

@ -38,7 +38,7 @@
use super::{ use super::{
primitive::{FloatTy, IntTy}, primitive::{FloatTy, IntTy},
traits::{Guidance, Obligation, ProjectionPredicate, Solution}, traits::{Guidance, Obligation, ProjectionPredicate, Solution},
InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk,
}; };
use crate::{ use crate::{
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, Scalar, db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, Scalar,
@ -46,15 +46,6 @@
pub(crate) use unify::unify; pub(crate) use unify::unify;
macro_rules! ty_app {
($ctor:pat, $param:pat) => {
crate::Ty::Apply(crate::ApplicationTy { ctor: $ctor, parameters: $param })
};
($ctor:pat) => {
ty_app!($ctor, _)
};
}
mod unify; mod unify;
mod path; mod path;
mod expr; mod expr;
@ -684,9 +675,9 @@ fn to_inner(self) -> unify::TypeVarId {
fn fallback_value(self) -> Ty { fn fallback_value(self) -> Ty {
match self { match self {
InferTy::TypeVar(..) => Ty::Unknown, InferTy::TypeVar(..) => Ty::Unknown,
InferTy::IntVar(..) => Ty::simple(TypeCtor::Scalar(Scalar::Int(IntTy::I32))), InferTy::IntVar(..) => Ty::Scalar(Scalar::Int(IntTy::I32)),
InferTy::FloatVar(..) => Ty::simple(TypeCtor::Scalar(Scalar::Float(FloatTy::F64))), InferTy::FloatVar(..) => Ty::Scalar(Scalar::Float(FloatTy::F64)),
InferTy::MaybeNeverTypeVar(..) => Ty::simple(TypeCtor::Never), InferTy::MaybeNeverTypeVar(..) => Ty::Never,
} }
} }
} }

View File

@ -7,7 +7,7 @@
use hir_def::{lang_item::LangItemTarget, type_ref::Mutability}; use hir_def::{lang_item::LangItemTarget, type_ref::Mutability};
use test_utils::mark; use test_utils::mark;
use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty, TypeCtor}; use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty};
use super::{unify::TypeVarValue, InEnvironment, InferTy, InferenceContext}; use super::{unify::TypeVarValue, InEnvironment, InferTy, InferenceContext};
@ -33,7 +33,7 @@ pub(super) fn coerce_merge_branch(&mut self, ty1: &Ty, ty2: &Ty) -> Ty {
} else if self.coerce(ty2, ty1) { } else if self.coerce(ty2, ty1) {
ty1.clone() ty1.clone()
} else { } else {
if let (ty_app!(TypeCtor::FnDef(_)), ty_app!(TypeCtor::FnDef(_))) = (ty1, ty2) { if let (Ty::FnDef(..), Ty::FnDef(..)) = (ty1, ty2) {
mark::hit!(coerce_fn_reification); mark::hit!(coerce_fn_reification);
// Special case: two function types. Try to coerce both to // Special case: two function types. Try to coerce both to
// pointers to have a chance at getting a match. See // pointers to have a chance at getting a match. See
@ -53,12 +53,12 @@ pub(super) fn coerce_merge_branch(&mut self, ty1: &Ty, ty2: &Ty) -> Ty {
fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool { fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool {
match (&from_ty, to_ty) { match (&from_ty, to_ty) {
// Never type will make type variable to fallback to Never Type instead of Unknown. // Never type will make type variable to fallback to Never Type instead of Unknown.
(ty_app!(TypeCtor::Never), Ty::Infer(InferTy::TypeVar(tv))) => { (Ty::Never, Ty::Infer(InferTy::TypeVar(tv))) => {
let var = self.table.new_maybe_never_type_var(); let var = self.table.new_maybe_never_type_var();
self.table.var_unification_table.union_value(*tv, TypeVarValue::Known(var)); self.table.var_unification_table.union_value(*tv, TypeVarValue::Known(var));
return true; return true;
} }
(ty_app!(TypeCtor::Never), _) => return true, (Ty::Never, _) => return true,
// Trivial cases, this should go after `never` check to // Trivial cases, this should go after `never` check to
// avoid infer result type to be never // avoid infer result type to be never
@ -71,38 +71,33 @@ fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool {
// Pointer weakening and function to pointer // Pointer weakening and function to pointer
match (&mut from_ty, to_ty) { match (&mut from_ty, to_ty) {
// `*mut T`, `&mut T, `&T`` -> `*const T` // `*mut T` -> `*const T`
// `&mut T` -> `&T` // `&mut T` -> `&T`
// `&mut T` -> `*mut T` (Ty::RawPtr(m1, ..), Ty::RawPtr(m2 @ Mutability::Shared, ..))
(ty_app!(c1@TypeCtor::RawPtr(_)), ty_app!(c2@TypeCtor::RawPtr(Mutability::Shared))) | (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Shared, ..)) => {
| (ty_app!(c1@TypeCtor::Ref(_)), ty_app!(c2@TypeCtor::RawPtr(Mutability::Shared))) *m1 = *m2;
| (ty_app!(c1@TypeCtor::Ref(_)), ty_app!(c2@TypeCtor::Ref(Mutability::Shared))) }
| (ty_app!(c1@TypeCtor::Ref(Mutability::Mut)), ty_app!(c2@TypeCtor::RawPtr(_))) => { // `&T` -> `*const T`
*c1 = *c2; // `&mut T` -> `*mut T`/`*const T`
(Ty::Ref(.., substs), &Ty::RawPtr(m2 @ Mutability::Shared, ..))
| (Ty::Ref(Mutability::Mut, substs), &Ty::RawPtr(m2, ..)) => {
from_ty = Ty::RawPtr(m2, substs.clone());
} }
// Illegal mutablity conversion // Illegal mutability conversion
( (Ty::RawPtr(Mutability::Shared, ..), Ty::RawPtr(Mutability::Mut, ..))
ty_app!(TypeCtor::RawPtr(Mutability::Shared)), | (Ty::Ref(Mutability::Shared, ..), Ty::Ref(Mutability::Mut, ..)) => return false,
ty_app!(TypeCtor::RawPtr(Mutability::Mut)),
)
| (
ty_app!(TypeCtor::Ref(Mutability::Shared)),
ty_app!(TypeCtor::Ref(Mutability::Mut)),
) => return false,
// `{function_type}` -> `fn()` // `{function_type}` -> `fn()`
(ty_app!(TypeCtor::FnDef(_)), ty_app!(TypeCtor::FnPtr { .. })) => { (Ty::FnDef(..), Ty::FnPtr { .. }) => match from_ty.callable_sig(self.db) {
match from_ty.callable_sig(self.db) { None => return false,
None => return false, Some(sig) => {
Some(sig) => { from_ty = Ty::fn_ptr(sig);
from_ty = Ty::fn_ptr(sig);
}
} }
} },
(ty_app!(TypeCtor::Closure { .. }, params), ty_app!(TypeCtor::FnPtr { .. })) => { (Ty::Closure { substs, .. }, Ty::FnPtr { .. }) => {
from_ty = params[0].clone(); from_ty = substs[0].clone();
} }
_ => {} _ => {}
@ -115,9 +110,7 @@ fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool {
// Auto Deref if cannot coerce // Auto Deref if cannot coerce
match (&from_ty, to_ty) { match (&from_ty, to_ty) {
// FIXME: DerefMut // FIXME: DerefMut
(ty_app!(TypeCtor::Ref(_), st1), ty_app!(TypeCtor::Ref(_), st2)) => { (Ty::Ref(_, st1), Ty::Ref(_, st2)) => self.unify_autoderef_behind_ref(&st1[0], &st2[0]),
self.unify_autoderef_behind_ref(&st1[0], &st2[0])
}
// Otherwise, normal unify // Otherwise, normal unify
_ => self.unify(&from_ty, to_ty), _ => self.unify(&from_ty, to_ty),
@ -178,17 +171,17 @@ fn unify_autoderef_behind_ref(&mut self, from_ty: &Ty, to_ty: &Ty) -> bool {
}, },
) { ) {
let derefed_ty = canonicalized.decanonicalize_ty(derefed_ty.value); let derefed_ty = canonicalized.decanonicalize_ty(derefed_ty.value);
match (&*self.resolve_ty_shallow(&derefed_ty), &*to_ty) { let from_ty = self.resolve_ty_shallow(&derefed_ty);
// Stop when constructor matches. // Stop when constructor matches.
(ty_app!(from_ctor, st1), ty_app!(to_ctor, st2)) if from_ctor == to_ctor => { if from_ty.equals_ctor(&to_ty) {
// It will not recurse to `coerce`. // It will not recurse to `coerce`.
return self.table.unify_substs(st1, st2, 0); return match (from_ty.substs(), to_ty.substs()) {
} (Some(st1), Some(st2)) => self.table.unify_substs(st1, st2, 0),
_ => { (None, None) => true,
if self.table.unify_inner_trivial(&derefed_ty, &to_ty, 0) { _ => false,
return true; };
} } else if self.table.unify_inner_trivial(&derefed_ty, &to_ty, 0) {
} return true;
} }
} }

View File

@ -18,8 +18,8 @@
primitive::{self, UintTy}, primitive::{self, UintTy},
traits::{FnTrait, InEnvironment}, traits::{FnTrait, InEnvironment},
utils::{generics, variant_data, Generics}, utils::{generics, variant_data, Generics},
ApplicationTy, Binders, CallableDefId, InferTy, Mutability, Obligation, OpaqueTyId, Rawness, Binders, CallableDefId, InferTy, Mutability, Obligation, OpaqueTyId, Rawness, Scalar, Substs,
Scalar, Substs, TraitRef, Ty, TypeCtor, TraitRef, Ty,
}; };
use super::{ use super::{
@ -82,10 +82,7 @@ fn callable_sig_from_fn_trait(&mut self, ty: &Ty, num_args: usize) -> Option<(Ve
arg_tys.push(arg); arg_tys.push(arg);
} }
let parameters = param_builder.build(); let parameters = param_builder.build();
let arg_ty = Ty::Apply(ApplicationTy { let arg_ty = Ty::Tuple { cardinality: num_args as u16, substs: parameters };
ctor: TypeCtor::Tuple { cardinality: num_args as u16 },
parameters,
});
let substs = let substs =
Substs::build_for_generics(&generic_params).push(ty.clone()).push(arg_ty).build(); Substs::build_for_generics(&generic_params).push(ty.clone()).push(arg_ty).build();
@ -120,10 +117,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
Expr::Missing => Ty::Unknown, Expr::Missing => Ty::Unknown,
Expr::If { condition, then_branch, else_branch } => { Expr::If { condition, then_branch, else_branch } => {
// if let is desugared to match, so this is always simple if // if let is desugared to match, so this is always simple if
self.infer_expr( self.infer_expr(*condition, &Expectation::has_type(Ty::Scalar(Scalar::Bool)));
*condition,
&Expectation::has_type(Ty::simple(TypeCtor::Scalar(Scalar::Bool))),
);
let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
let mut both_arms_diverge = Diverges::Always; let mut both_arms_diverge = Diverges::Always;
@ -178,7 +172,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
// existenail type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType> // existenail type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType>
let inner_ty = self.infer_expr(*body, &Expectation::none()); let inner_ty = self.infer_expr(*body, &Expectation::none());
let opaque_ty_id = OpaqueTyId::AsyncBlockTypeImplTrait(self.owner, *body); let opaque_ty_id = OpaqueTyId::AsyncBlockTypeImplTrait(self.owner, *body);
Ty::apply_one(TypeCtor::OpaqueType(opaque_ty_id), inner_ty) Ty::OpaqueType(opaque_ty_id, Substs::single(inner_ty))
} }
Expr::Loop { body, label } => { Expr::Loop { body, label } => {
self.breakables.push(BreakableContext { self.breakables.push(BreakableContext {
@ -196,7 +190,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
if ctxt.may_break { if ctxt.may_break {
ctxt.break_ty ctxt.break_ty
} else { } else {
Ty::simple(TypeCtor::Never) Ty::Never
} }
} }
Expr::While { condition, body, label } => { Expr::While { condition, body, label } => {
@ -206,10 +200,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
label: label.map(|label| self.body[label].name.clone()), label: label.map(|label| self.body[label].name.clone()),
}); });
// while let is desugared to a match loop, so this is always simple while // while let is desugared to a match loop, so this is always simple while
self.infer_expr( self.infer_expr(*condition, &Expectation::has_type(Ty::Scalar(Scalar::Bool)));
*condition,
&Expectation::has_type(Ty::simple(TypeCtor::Scalar(Scalar::Bool))),
);
self.infer_expr(*body, &Expectation::has_type(Ty::unit())); self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
let _ctxt = self.breakables.pop().expect("breakable stack broken"); let _ctxt = self.breakables.pop().expect("breakable stack broken");
// the body may not run, so it diverging doesn't mean we diverge // the body may not run, so it diverging doesn't mean we diverge
@ -256,12 +247,13 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
None => self.table.new_type_var(), None => self.table.new_type_var(),
}; };
sig_tys.push(ret_ty.clone()); sig_tys.push(ret_ty.clone());
let sig_ty = Ty::apply( let sig_ty = Ty::FnPtr {
TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1, is_varargs: false }, num_args: sig_tys.len() as u16 - 1,
Substs(sig_tys.clone().into()), is_varargs: false,
); substs: Substs(sig_tys.clone().into()),
};
let closure_ty = let closure_ty =
Ty::apply_one(TypeCtor::Closure { def: self.owner, expr: tgt_expr }, sig_ty); Ty::Closure { def: self.owner, expr: tgt_expr, substs: Substs::single(sig_ty) };
// Eagerly try to relate the closure type with the expected // Eagerly try to relate the closure type with the expected
// type, otherwise we often won't have enough information to // type, otherwise we often won't have enough information to
@ -312,11 +304,8 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
Expr::Match { expr, arms } => { Expr::Match { expr, arms } => {
let input_ty = self.infer_expr(*expr, &Expectation::none()); let input_ty = self.infer_expr(*expr, &Expectation::none());
let mut result_ty = if arms.is_empty() { let mut result_ty =
Ty::simple(TypeCtor::Never) if arms.is_empty() { Ty::Never } else { self.table.new_type_var() };
} else {
self.table.new_type_var()
};
let matchee_diverges = self.diverges; let matchee_diverges = self.diverges;
let mut all_arms_diverge = Diverges::Always; let mut all_arms_diverge = Diverges::Always;
@ -327,7 +316,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
if let Some(guard_expr) = arm.guard { if let Some(guard_expr) = arm.guard {
self.infer_expr( self.infer_expr(
guard_expr, guard_expr,
&Expectation::has_type(Ty::simple(TypeCtor::Scalar(Scalar::Bool))), &Expectation::has_type(Ty::Scalar(Scalar::Bool)),
); );
} }
@ -345,7 +334,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr); let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr);
self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown) self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown)
} }
Expr::Continue { .. } => Ty::simple(TypeCtor::Never), Expr::Continue { .. } => Ty::Never,
Expr::Break { expr, label } => { Expr::Break { expr, label } => {
let val_ty = if let Some(expr) = expr { let val_ty = if let Some(expr) = expr {
self.infer_expr(*expr, &Expectation::none()) self.infer_expr(*expr, &Expectation::none())
@ -370,8 +359,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
expr: tgt_expr, expr: tgt_expr,
}); });
} }
Ty::Never
Ty::simple(TypeCtor::Never)
} }
Expr::Return { expr } => { Expr::Return { expr } => {
if let Some(expr) = expr { if let Some(expr) = expr {
@ -380,14 +368,14 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
let unit = Ty::unit(); let unit = Ty::unit();
self.coerce(&unit, &self.return_ty.clone()); self.coerce(&unit, &self.return_ty.clone());
} }
Ty::simple(TypeCtor::Never) Ty::Never
} }
Expr::Yield { expr } => { Expr::Yield { expr } => {
// FIXME: track yield type for coercion // FIXME: track yield type for coercion
if let Some(expr) = expr { if let Some(expr) = expr {
self.infer_expr(*expr, &Expectation::none()); self.infer_expr(*expr, &Expectation::none());
} }
Ty::simple(TypeCtor::Never) Ty::Never
} }
Expr::RecordLit { path, fields, spread } => { Expr::RecordLit { path, fields, spread } => {
let (ty, def_id) = self.resolve_variant(path.as_ref()); let (ty, def_id) = self.resolve_variant(path.as_ref());
@ -397,7 +385,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
self.unify(&ty, &expected.ty); self.unify(&ty, &expected.ty);
let substs = ty.substs().unwrap_or_else(Substs::empty); let substs = ty.substs().cloned().unwrap_or_else(Substs::empty);
let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default();
let variant_data = def_id.map(|it| variant_data(self.db.upcast(), it)); let variant_data = def_id.map(|it| variant_data(self.db.upcast(), it));
for (field_idx, field) in fields.iter().enumerate() { for (field_idx, field) in fields.iter().enumerate() {
@ -436,30 +424,23 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
}, },
) )
.find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) { .find_map(|derefed_ty| match canonicalized.decanonicalize_ty(derefed_ty.value) {
Ty::Apply(a_ty) => match a_ty.ctor { Ty::Tuple { substs, .. } => {
TypeCtor::Tuple { .. } => name name.as_tuple_index().and_then(|idx| substs.0.get(idx).cloned())
.as_tuple_index() }
.and_then(|idx| a_ty.parameters.0.get(idx).cloned()), Ty::Adt(AdtId::StructId(s), parameters) => {
TypeCtor::Adt(AdtId::StructId(s)) => { self.db.struct_data(s).variant_data.field(name).map(|local_id| {
self.db.struct_data(s).variant_data.field(name).map(|local_id| { let field = FieldId { parent: s.into(), local_id };
let field = FieldId { parent: s.into(), local_id }; self.write_field_resolution(tgt_expr, field);
self.write_field_resolution(tgt_expr, field); self.db.field_types(s.into())[field.local_id].clone().subst(&parameters)
self.db.field_types(s.into())[field.local_id] })
.clone() }
.subst(&a_ty.parameters) Ty::Adt(AdtId::UnionId(u), parameters) => {
}) self.db.union_data(u).variant_data.field(name).map(|local_id| {
} let field = FieldId { parent: u.into(), local_id };
TypeCtor::Adt(AdtId::UnionId(u)) => { self.write_field_resolution(tgt_expr, field);
self.db.union_data(u).variant_data.field(name).map(|local_id| { self.db.field_types(u.into())[field.local_id].clone().subst(&parameters)
let field = FieldId { parent: u.into(), local_id }; })
self.write_field_resolution(tgt_expr, field); }
self.db.field_types(u.into())[field.local_id]
.clone()
.subst(&a_ty.parameters)
})
}
_ => None,
},
_ => None, _ => None,
}) })
.unwrap_or(Ty::Unknown); .unwrap_or(Ty::Unknown);
@ -497,19 +478,18 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
Expectation::none() Expectation::none()
}; };
let inner_ty = self.infer_expr_inner(*expr, &expectation); let inner_ty = self.infer_expr_inner(*expr, &expectation);
let ty = match rawness { match rawness {
Rawness::RawPtr => TypeCtor::RawPtr(*mutability), Rawness::RawPtr => Ty::RawPtr(*mutability, Substs::single(inner_ty)),
Rawness::Ref => TypeCtor::Ref(*mutability), Rawness::Ref => Ty::Ref(*mutability, Substs::single(inner_ty)),
}; }
Ty::apply_one(ty, inner_ty)
} }
Expr::Box { expr } => { Expr::Box { expr } => {
let inner_ty = self.infer_expr_inner(*expr, &Expectation::none()); let inner_ty = self.infer_expr_inner(*expr, &Expectation::none());
if let Some(box_) = self.resolve_boxed_box() { if let Some(box_) = self.resolve_boxed_box() {
let mut sb = Substs::build_for_type_ctor(self.db, TypeCtor::Adt(box_)); let mut sb = Substs::builder(generics(self.db.upcast(), box_.into()).len());
sb = sb.push(inner_ty); sb = sb.push(inner_ty);
sb = sb.fill(repeat_with(|| self.table.new_type_var())); sb = sb.fill(repeat_with(|| self.table.new_type_var()));
Ty::apply(TypeCtor::Adt(box_), sb.build()) Ty::Adt(box_, sb.build())
} else { } else {
Ty::Unknown Ty::Unknown
} }
@ -539,14 +519,9 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
UnaryOp::Neg => { UnaryOp::Neg => {
match &inner_ty { match &inner_ty {
// Fast path for builtins // Fast path for builtins
Ty::Apply(ApplicationTy { Ty::Scalar(Scalar::Int(_))
ctor: TypeCtor::Scalar(Scalar::Int(_)), | Ty::Scalar(Scalar::Uint(_))
.. | Ty::Scalar(Scalar::Float(_))
})
| Ty::Apply(ApplicationTy {
ctor: TypeCtor::Scalar(Scalar::Float(_)),
..
})
| Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::IntVar(..))
| Ty::Infer(InferTy::FloatVar(..)) => inner_ty, | Ty::Infer(InferTy::FloatVar(..)) => inner_ty,
// Otherwise we resolve via the std::ops::Neg trait // Otherwise we resolve via the std::ops::Neg trait
@ -557,18 +532,9 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
UnaryOp::Not => { UnaryOp::Not => {
match &inner_ty { match &inner_ty {
// Fast path for builtins // Fast path for builtins
Ty::Apply(ApplicationTy { Ty::Scalar(Scalar::Bool)
ctor: TypeCtor::Scalar(Scalar::Bool), | Ty::Scalar(Scalar::Int(_))
.. | Ty::Scalar(Scalar::Uint(_))
})
| Ty::Apply(ApplicationTy {
ctor: TypeCtor::Scalar(Scalar::Int(_)),
..
})
| Ty::Apply(ApplicationTy {
ctor: TypeCtor::Scalar(Scalar::Uint(_)),
..
})
| Ty::Infer(InferTy::IntVar(..)) => inner_ty, | Ty::Infer(InferTy::IntVar(..)) => inner_ty,
// Otherwise we resolve via the std::ops::Not trait // Otherwise we resolve via the std::ops::Not trait
_ => self _ => self
@ -580,9 +546,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
Expr::BinaryOp { lhs, rhs, op } => match op { Expr::BinaryOp { lhs, rhs, op } => match op {
Some(op) => { Some(op) => {
let lhs_expectation = match op { let lhs_expectation = match op {
BinaryOp::LogicOp(..) => { BinaryOp::LogicOp(..) => Expectation::has_type(Ty::Scalar(Scalar::Bool)),
Expectation::has_type(Ty::simple(TypeCtor::Scalar(Scalar::Bool)))
}
_ => Expectation::none(), _ => Expectation::none(),
}; };
let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); let lhs_ty = self.infer_expr(*lhs, &lhs_expectation);
@ -613,31 +577,31 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect));
match (range_type, lhs_ty, rhs_ty) { match (range_type, lhs_ty, rhs_ty) {
(RangeOp::Exclusive, None, None) => match self.resolve_range_full() { (RangeOp::Exclusive, None, None) => match self.resolve_range_full() {
Some(adt) => Ty::simple(TypeCtor::Adt(adt)), Some(adt) => Ty::Adt(adt, Substs::empty()),
None => Ty::Unknown, None => Ty::Unknown,
}, },
(RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() {
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), Some(adt) => Ty::Adt(adt, Substs::single(ty)),
None => Ty::Unknown, None => Ty::Unknown,
}, },
(RangeOp::Inclusive, None, Some(ty)) => { (RangeOp::Inclusive, None, Some(ty)) => {
match self.resolve_range_to_inclusive() { match self.resolve_range_to_inclusive() {
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), Some(adt) => Ty::Adt(adt, Substs::single(ty)),
None => Ty::Unknown, None => Ty::Unknown,
} }
} }
(RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() {
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), Some(adt) => Ty::Adt(adt, Substs::single(ty)),
None => Ty::Unknown, None => Ty::Unknown,
}, },
(RangeOp::Inclusive, Some(_), Some(ty)) => { (RangeOp::Inclusive, Some(_), Some(ty)) => {
match self.resolve_range_inclusive() { match self.resolve_range_inclusive() {
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), Some(adt) => Ty::Adt(adt, Substs::single(ty)),
None => Ty::Unknown, None => Ty::Unknown,
} }
} }
(RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() {
Some(adt) => Ty::apply_one(TypeCtor::Adt(adt), ty), Some(adt) => Ty::Adt(adt, Substs::single(ty)),
None => Ty::Unknown, None => Ty::Unknown,
}, },
(RangeOp::Inclusive, _, None) => Ty::Unknown, (RangeOp::Inclusive, _, None) => Ty::Unknown,
@ -671,7 +635,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
} }
Expr::Tuple { exprs } => { Expr::Tuple { exprs } => {
let mut tys = match &expected.ty { let mut tys = match &expected.ty {
ty_app!(TypeCtor::Tuple { .. }, st) => st Ty::Tuple { substs, .. } => substs
.iter() .iter()
.cloned() .cloned()
.chain(repeat_with(|| self.table.new_type_var())) .chain(repeat_with(|| self.table.new_type_var()))
@ -684,15 +648,11 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone())); self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone()));
} }
Ty::apply(TypeCtor::Tuple { cardinality: tys.len() as u16 }, Substs(tys.into())) Ty::Tuple { cardinality: tys.len() as u16, substs: Substs(tys.into()) }
} }
Expr::Array(array) => { Expr::Array(array) => {
let elem_ty = match &expected.ty { let elem_ty = match &expected.ty {
// FIXME: remove when https://github.com/rust-lang/rust/issues/80501 is fixed Ty::Array(st) | Ty::Slice(st) => st.as_single().clone(),
#[allow(unreachable_patterns)]
ty_app!(TypeCtor::Array, st) | ty_app!(TypeCtor::Slice, st) => {
st.as_single().clone()
}
_ => self.table.new_type_var(), _ => self.table.new_type_var(),
}; };
@ -709,42 +669,38 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
); );
self.infer_expr( self.infer_expr(
*repeat, *repeat,
&Expectation::has_type(Ty::simple(TypeCtor::Scalar(Scalar::Uint( &Expectation::has_type(Ty::Scalar(Scalar::Uint(UintTy::Usize))),
UintTy::Usize,
)))),
); );
} }
} }
Ty::apply_one(TypeCtor::Array, elem_ty) Ty::Array(Substs::single(elem_ty))
} }
Expr::Literal(lit) => match lit { Expr::Literal(lit) => match lit {
Literal::Bool(..) => Ty::simple(TypeCtor::Scalar(Scalar::Bool)), Literal::Bool(..) => Ty::Scalar(Scalar::Bool),
Literal::String(..) => { Literal::String(..) => Ty::Ref(Mutability::Shared, Substs::single(Ty::Str)),
Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
}
Literal::ByteString(..) => { Literal::ByteString(..) => {
let byte_type = Ty::simple(TypeCtor::Scalar(Scalar::Uint(UintTy::U8))); let byte_type = Ty::Scalar(Scalar::Uint(UintTy::U8));
let array_type = Ty::apply_one(TypeCtor::Array, byte_type); let array_type = Ty::Array(Substs::single(byte_type));
Ty::apply_one(TypeCtor::Ref(Mutability::Shared), array_type) Ty::Ref(Mutability::Shared, Substs::single(array_type))
} }
Literal::Char(..) => Ty::simple(TypeCtor::Scalar(Scalar::Char)), Literal::Char(..) => Ty::Scalar(Scalar::Char),
Literal::Int(_v, ty) => match ty { Literal::Int(_v, ty) => match ty {
Some(int_ty) => Ty::simple(TypeCtor::Scalar(Scalar::Int( Some(int_ty) => {
primitive::int_ty_from_builtin(*int_ty), Ty::Scalar(Scalar::Int(primitive::int_ty_from_builtin(*int_ty)))
))), }
None => self.table.new_integer_var(), None => self.table.new_integer_var(),
}, },
Literal::Uint(_v, ty) => match ty { Literal::Uint(_v, ty) => match ty {
Some(int_ty) => Ty::simple(TypeCtor::Scalar(Scalar::Uint( Some(int_ty) => {
primitive::uint_ty_from_builtin(*int_ty), Ty::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(*int_ty)))
))), }
None => self.table.new_integer_var(), None => self.table.new_integer_var(),
}, },
Literal::Float(_v, ty) => match ty { Literal::Float(_v, ty) => match ty {
Some(float_ty) => Ty::simple(TypeCtor::Scalar(Scalar::Float( Some(float_ty) => {
primitive::float_ty_from_builtin(*float_ty), Ty::Scalar(Scalar::Float(primitive::float_ty_from_builtin(*float_ty)))
))), }
None => self.table.new_float_var(), None => self.table.new_float_var(),
}, },
}, },
@ -857,7 +813,7 @@ fn infer_method_call(
// Apply autoref so the below unification works correctly // Apply autoref so the below unification works correctly
// FIXME: return correct autorefs from lookup_method // FIXME: return correct autorefs from lookup_method
let actual_receiver_ty = match expected_receiver_ty.as_reference() { let actual_receiver_ty = match expected_receiver_ty.as_reference() {
Some((_, mutability)) => Ty::apply_one(TypeCtor::Ref(mutability), derefed_receiver_ty), Some((_, mutability)) => Ty::Ref(mutability, Substs::single(derefed_receiver_ty)),
_ => derefed_receiver_ty, _ => derefed_receiver_ty,
}; };
self.unify(&expected_receiver_ty, &actual_receiver_ty); self.unify(&expected_receiver_ty, &actual_receiver_ty);
@ -934,30 +890,26 @@ fn substs_for_method_call(
} }
fn register_obligations_for_call(&mut self, callable_ty: &Ty) { fn register_obligations_for_call(&mut self, callable_ty: &Ty) {
if let Ty::Apply(a_ty) = callable_ty { if let &Ty::FnDef(def, ref parameters) = callable_ty {
if let TypeCtor::FnDef(def) = a_ty.ctor { let generic_predicates = self.db.generic_predicates(def.into());
let generic_predicates = self.db.generic_predicates(def.into()); for predicate in generic_predicates.iter() {
for predicate in generic_predicates.iter() { let predicate = predicate.clone().subst(parameters);
let predicate = predicate.clone().subst(&a_ty.parameters); if let Some(obligation) = Obligation::from_predicate(predicate) {
if let Some(obligation) = Obligation::from_predicate(predicate) { self.obligations.push(obligation);
self.obligations.push(obligation); }
}
// add obligation for trait implementation, if this is a trait method
match def {
CallableDefId::FunctionId(f) => {
if let AssocContainerId::TraitId(trait_) = f.lookup(self.db.upcast()).container
{
// construct a TraitDef
let substs =
parameters.prefix(generics(self.db.upcast(), trait_.into()).len());
self.obligations.push(Obligation::Trait(TraitRef { trait_, substs }));
} }
} }
// add obligation for trait implementation, if this is a trait method CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {}
match def {
CallableDefId::FunctionId(f) => {
if let AssocContainerId::TraitId(trait_) =
f.lookup(self.db.upcast()).container
{
// construct a TraitDef
let substs = a_ty
.parameters
.prefix(generics(self.db.upcast(), trait_.into()).len());
self.obligations.push(Obligation::Trait(TraitRef { trait_, substs }));
}
}
CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {}
}
} }
} }
} }

View File

@ -13,7 +13,7 @@
use test_utils::mark; use test_utils::mark;
use super::{BindingMode, Expectation, InferenceContext}; use super::{BindingMode, Expectation, InferenceContext};
use crate::{utils::variant_data, Substs, Ty, TypeCtor}; use crate::{utils::variant_data, Substs, Ty};
impl<'a> InferenceContext<'a> { impl<'a> InferenceContext<'a> {
fn infer_tuple_struct_pat( fn infer_tuple_struct_pat(
@ -32,7 +32,7 @@ fn infer_tuple_struct_pat(
} }
self.unify(&ty, expected); self.unify(&ty, expected);
let substs = ty.substs().unwrap_or_else(Substs::empty); let substs = ty.substs().cloned().unwrap_or_else(Substs::empty);
let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
let (pre, post) = match ellipsis { let (pre, post) = match ellipsis {
@ -71,7 +71,7 @@ fn infer_record_pat(
self.unify(&ty, expected); self.unify(&ty, expected);
let substs = ty.substs().unwrap_or_else(Substs::empty); let substs = ty.substs().cloned().unwrap_or_else(Substs::empty);
let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
for subpat in subpats { for subpat in subpats {
@ -138,10 +138,7 @@ pub(super) fn infer_pat(
inner_tys.extend(expectations_iter.by_ref().take(n_uncovered_patterns).cloned()); inner_tys.extend(expectations_iter.by_ref().take(n_uncovered_patterns).cloned());
inner_tys.extend(post.iter().zip(expectations_iter).map(infer_pat)); inner_tys.extend(post.iter().zip(expectations_iter).map(infer_pat));
Ty::apply( Ty::Tuple { cardinality: inner_tys.len() as u16, substs: Substs(inner_tys.into()) }
TypeCtor::Tuple { cardinality: inner_tys.len() as u16 },
Substs(inner_tys.into()),
)
} }
Pat::Or(ref pats) => { Pat::Or(ref pats) => {
if let Some((first_pat, rest)) = pats.split_first() { if let Some((first_pat, rest)) = pats.split_first() {
@ -165,7 +162,7 @@ pub(super) fn infer_pat(
_ => &Ty::Unknown, _ => &Ty::Unknown,
}; };
let subty = self.infer_pat(*pat, expectation, default_bm); let subty = self.infer_pat(*pat, expectation, default_bm);
Ty::apply_one(TypeCtor::Ref(*mutability), subty) Ty::Ref(*mutability, Substs::single(subty))
} }
Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
p.as_ref(), p.as_ref(),
@ -198,7 +195,7 @@ pub(super) fn infer_pat(
let bound_ty = match mode { let bound_ty = match mode {
BindingMode::Ref(mutability) => { BindingMode::Ref(mutability) => {
Ty::apply_one(TypeCtor::Ref(mutability), inner_ty.clone()) Ty::Ref(mutability, Substs::single(inner_ty.clone()))
} }
BindingMode::Move => inner_ty.clone(), BindingMode::Move => inner_ty.clone(),
}; };
@ -207,17 +204,17 @@ pub(super) fn infer_pat(
return inner_ty; return inner_ty;
} }
Pat::Slice { prefix, slice, suffix } => { Pat::Slice { prefix, slice, suffix } => {
let (container_ty, elem_ty) = match &expected { let (container_ty, elem_ty): (fn(_) -> _, _) = match &expected {
ty_app!(TypeCtor::Array, st) => (TypeCtor::Array, st.as_single().clone()), Ty::Array(st) => (Ty::Array, st.as_single().clone()),
ty_app!(TypeCtor::Slice, st) => (TypeCtor::Slice, st.as_single().clone()), Ty::Slice(st) => (Ty::Slice, st.as_single().clone()),
_ => (TypeCtor::Slice, Ty::Unknown), _ => (Ty::Slice, Ty::Unknown),
}; };
for pat_id in prefix.iter().chain(suffix) { for pat_id in prefix.iter().chain(suffix) {
self.infer_pat(*pat_id, &elem_ty, default_bm); self.infer_pat(*pat_id, &elem_ty, default_bm);
} }
let pat_ty = Ty::apply_one(container_ty, elem_ty); let pat_ty = container_ty(Substs::single(elem_ty));
if let Some(slice_pat_id) = slice { if let Some(slice_pat_id) = slice {
self.infer_pat(*slice_pat_id, &pat_ty, default_bm); self.infer_pat(*slice_pat_id, &pat_ty, default_bm);
} }
@ -239,7 +236,7 @@ pub(super) fn infer_pat(
}; };
let inner_ty = self.infer_pat(*inner, inner_expected, default_bm); let inner_ty = self.infer_pat(*inner, inner_expected, default_bm);
Ty::apply_one(TypeCtor::Adt(box_adt), inner_ty) Ty::Adt(box_adt, Substs::single(inner_ty))
} }
None => Ty::Unknown, None => Ty::Unknown,
}, },

View File

@ -9,7 +9,7 @@
use super::{InferenceContext, Obligation}; use super::{InferenceContext, Obligation};
use crate::{ use crate::{
BoundVar, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, InferTy, Scalar, Substs, BoundVar, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, InferTy, Scalar, Substs,
Ty, TyKind, TypeCtor, TypeWalk, Ty, TyKind, TypeWalk,
}; };
impl<'a> InferenceContext<'a> { impl<'a> InferenceContext<'a> {
@ -257,12 +257,14 @@ fn unify_inner(&mut self, ty1: &Ty, ty2: &Ty, depth: usize) -> bool {
// try to resolve type vars first // try to resolve type vars first
let ty1 = self.resolve_ty_shallow(ty1); let ty1 = self.resolve_ty_shallow(ty1);
let ty2 = self.resolve_ty_shallow(ty2); let ty2 = self.resolve_ty_shallow(ty2);
match (&*ty1, &*ty2) { if ty1.equals_ctor(&ty2) {
(Ty::Apply(a_ty1), Ty::Apply(a_ty2)) if a_ty1.ctor == a_ty2.ctor => { match (ty1.substs(), ty2.substs()) {
self.unify_substs(&a_ty1.parameters, &a_ty2.parameters, depth + 1) (Some(st1), Some(st2)) => self.unify_substs(st1, st2, depth + 1),
(None, None) => true,
_ => false,
} }
} else {
_ => self.unify_inner_trivial(&ty1, &ty2, depth), self.unify_inner_trivial(&ty1, &ty2, depth)
} }
} }
@ -300,24 +302,12 @@ pub(super) fn unify_inner_trivial(&mut self, ty1: &Ty, ty2: &Ty, depth: usize) -
| (other, Ty::Infer(InferTy::TypeVar(tv))) | (other, Ty::Infer(InferTy::TypeVar(tv)))
| (Ty::Infer(InferTy::MaybeNeverTypeVar(tv)), other) | (Ty::Infer(InferTy::MaybeNeverTypeVar(tv)), other)
| (other, Ty::Infer(InferTy::MaybeNeverTypeVar(tv))) | (other, Ty::Infer(InferTy::MaybeNeverTypeVar(tv)))
| (Ty::Infer(InferTy::IntVar(tv)), other @ ty_app!(TypeCtor::Scalar(Scalar::Int(_)))) | (Ty::Infer(InferTy::IntVar(tv)), other @ Ty::Scalar(Scalar::Int(_)))
| (other @ ty_app!(TypeCtor::Scalar(Scalar::Int(_))), Ty::Infer(InferTy::IntVar(tv))) | (other @ Ty::Scalar(Scalar::Int(_)), Ty::Infer(InferTy::IntVar(tv)))
| ( | (Ty::Infer(InferTy::IntVar(tv)), other @ Ty::Scalar(Scalar::Uint(_)))
Ty::Infer(InferTy::IntVar(tv)), | (other @ Ty::Scalar(Scalar::Uint(_)), Ty::Infer(InferTy::IntVar(tv)))
other @ ty_app!(TypeCtor::Scalar(Scalar::Uint(_))), | (Ty::Infer(InferTy::FloatVar(tv)), other @ Ty::Scalar(Scalar::Float(_)))
) | (other @ Ty::Scalar(Scalar::Float(_)), Ty::Infer(InferTy::FloatVar(tv))) => {
| (
other @ ty_app!(TypeCtor::Scalar(Scalar::Uint(_))),
Ty::Infer(InferTy::IntVar(tv)),
)
| (
Ty::Infer(InferTy::FloatVar(tv)),
other @ ty_app!(TypeCtor::Scalar(Scalar::Float(_))),
)
| (
other @ ty_app!(TypeCtor::Scalar(Scalar::Float(_))),
Ty::Infer(InferTy::FloatVar(tv)),
) => {
// the type var is unknown since we tried to resolve it // the type var is unknown since we tried to resolve it
self.var_unification_table.union_value(*tv, TypeVarValue::Known(other.clone())); self.var_unification_table.union_value(*tv, TypeVarValue::Known(other.clone()));
true true

View File

@ -25,7 +25,7 @@ macro_rules! eprintln {
use std::{iter, mem, ops::Deref, sync::Arc}; use std::{iter, mem, ops::Deref, sync::Arc};
use base_db::{salsa, CrateId}; use base_db::salsa;
use hir_def::{ use hir_def::{
builtin_type::BuiltinType, builtin_type::BuiltinType,
expr::ExprId, expr::ExprId,
@ -57,192 +57,6 @@ pub enum Lifetime {
Static, Static,
} }
/// A type constructor or type name: this might be something like the primitive
/// type `bool`, a struct like `Vec`, or things like function pointers or
/// tuples.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum TypeCtor {
/// a scalar type like `bool` or `u32`
Scalar(Scalar),
/// Structures, enumerations and unions.
Adt(AdtId),
/// The pointee of a string slice. Written as `str`.
Str,
/// The pointee of an array slice. Written as `[T]`.
Slice,
/// An array with the given length. Written as `[T; n]`.
Array,
/// A raw pointer. Written as `*mut T` or `*const T`
RawPtr(Mutability),
/// A reference; a pointer with an associated lifetime. Written as
/// `&'a mut T` or `&'a T`.
Ref(Mutability),
/// 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(CallableDefId),
/// 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;
/// ```
// FIXME make this a Ty variant like in Chalk
FnPtr { num_args: u16, is_varargs: bool },
/// The never type `!`.
Never,
/// A tuple type. For example, `(i32, bool)`.
Tuple { cardinality: u16 },
/// 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)<T>`.
AssociatedType(TypeAliasId),
/// 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),
/// Represents a foreign type declared in external blocks.
ForeignType(TypeAliasId),
/// The type of a specific closure.
///
/// The closure signature is stored in a `FnPtr` type in the first type
/// parameter.
Closure { def: DefWithBodyId, expr: ExprId },
}
impl TypeCtor {
pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize {
match self {
TypeCtor::Scalar(_)
| TypeCtor::Str
| TypeCtor::Never => 0,
TypeCtor::Slice
| TypeCtor::Array
| TypeCtor::RawPtr(_)
| TypeCtor::Ref(_)
| TypeCtor::Closure { .. } // 1 param representing the signature of the closure
=> 1,
TypeCtor::Adt(adt) => {
let generic_params = generics(db.upcast(), adt.into());
generic_params.len()
}
TypeCtor::FnDef(callable) => {
let generic_params = generics(db.upcast(), callable.into());
generic_params.len()
}
TypeCtor::AssociatedType(type_alias) => {
let generic_params = generics(db.upcast(), type_alias.into());
generic_params.len()
}
TypeCtor::ForeignType(type_alias) => {
let generic_params = generics(db.upcast(), type_alias.into());
generic_params.len()
}
TypeCtor::OpaqueType(opaque_ty_id) => {
match opaque_ty_id {
OpaqueTyId::ReturnTypeImplTrait(func, _) => {
let generic_params = generics(db.upcast(), func.into());
generic_params.len()
}
// 1 param representing Future::Output type.
OpaqueTyId::AsyncBlockTypeImplTrait(..) => 1,
}
}
TypeCtor::FnPtr { num_args, is_varargs: _ } => num_args as usize + 1,
TypeCtor::Tuple { cardinality } => cardinality as usize,
}
}
pub fn krate(self, db: &dyn HirDatabase) -> Option<CrateId> {
match self {
TypeCtor::Scalar(_)
| TypeCtor::Str
| TypeCtor::Never
| TypeCtor::Slice
| TypeCtor::Array
| TypeCtor::RawPtr(_)
| TypeCtor::Ref(_)
| TypeCtor::FnPtr { .. }
| TypeCtor::Tuple { .. } => None,
// Closure's krate is irrelevant for coherence I would think?
TypeCtor::Closure { .. } => None,
TypeCtor::Adt(adt) => Some(adt.module(db.upcast()).krate()),
TypeCtor::FnDef(callable) => Some(callable.krate(db)),
TypeCtor::AssociatedType(type_alias) => {
Some(type_alias.lookup(db.upcast()).module(db.upcast()).krate())
}
TypeCtor::ForeignType(type_alias) => {
Some(type_alias.lookup(db.upcast()).module(db.upcast()).krate())
}
TypeCtor::OpaqueType(opaque_ty_id) => match opaque_ty_id {
OpaqueTyId::ReturnTypeImplTrait(func, _) => {
Some(func.lookup(db.upcast()).module(db.upcast()).krate())
}
OpaqueTyId::AsyncBlockTypeImplTrait(def, _) => {
Some(def.module(db.upcast()).krate())
}
},
}
}
pub fn as_generic_def(self) -> Option<GenericDefId> {
match self {
TypeCtor::Scalar(_)
| TypeCtor::Str
| TypeCtor::Never
| TypeCtor::Slice
| TypeCtor::Array
| TypeCtor::RawPtr(_)
| TypeCtor::Ref(_)
| TypeCtor::FnPtr { .. }
| TypeCtor::Tuple { .. }
| TypeCtor::Closure { .. } => None,
TypeCtor::Adt(adt) => Some(adt.into()),
TypeCtor::FnDef(callable) => Some(callable.into()),
TypeCtor::AssociatedType(type_alias) => Some(type_alias.into()),
TypeCtor::ForeignType(type_alias) => Some(type_alias.into()),
TypeCtor::OpaqueType(_impl_trait_id) => None,
}
}
}
/// A nominal type with (maybe 0) type parameters. This might be a primitive
/// type like `bool`, a struct, tuple, function pointer, reference or
/// several other things.
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct ApplicationTy {
pub ctor: TypeCtor,
pub parameters: Substs,
}
#[derive(Clone, PartialEq, Eq, Debug, Hash)] #[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct OpaqueTy { pub struct OpaqueTy {
pub opaque_ty_id: OpaqueTyId, pub opaque_ty_id: OpaqueTyId,
@ -293,10 +107,80 @@ fn walk_mut_binders(
/// This should be cheap to clone. /// This should be cheap to clone.
#[derive(Clone, PartialEq, Eq, Debug, Hash)] #[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum Ty { pub enum Ty {
/// A nominal type with (maybe 0) type parameters. This might be a primitive /// Structures, enumerations and unions.
/// type like `bool`, a struct, tuple, function pointer, reference or Adt(AdtId, Substs),
/// several other things.
Apply(ApplicationTy), /// 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)<T>`.
AssociatedType(TypeAliasId, Substs),
/// a scalar type like `bool` or `u32`
Scalar(Scalar),
/// A tuple type. For example, `(i32, bool)`.
Tuple { cardinality: u16, substs: Substs },
/// An array with the given length. Written as `[T; n]`.
Array(Substs),
/// The pointee of an array slice. Written as `[T]`.
Slice(Substs),
/// A raw pointer. Written as `*mut T` or `*const T`
RawPtr(Mutability, Substs),
/// A reference; a pointer with an associated lifetime. Written as
/// `&'a mut T` or `&'a T`.
Ref(Mutability, Substs),
/// 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, Substs),
/// 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(CallableDefId, Substs),
/// 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 { def: DefWithBodyId, expr: ExprId, substs: Substs },
/// Represents a foreign type declared in external blocks.
ForeignType(TypeAliasId, Substs),
/// 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;
/// ```
// FIXME make this a Ty variant like in Chalk
FnPtr { num_args: u16, is_varargs: bool, substs: Substs },
/// A "projection" type corresponds to an (unnormalized) /// A "projection" type corresponds to an (unnormalized)
/// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the /// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the
@ -420,10 +304,6 @@ pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder {
Substs::builder(generic_params.len()) Substs::builder(generic_params.len())
} }
pub fn build_for_type_ctor(db: &dyn HirDatabase, type_ctor: TypeCtor) -> SubstsBuilder {
Substs::builder(type_ctor.num_ty_params(db))
}
fn builder(param_count: usize) -> SubstsBuilder { fn builder(param_count: usize) -> SubstsBuilder {
SubstsBuilder { vec: Vec::with_capacity(param_count), param_count } SubstsBuilder { vec: Vec::with_capacity(param_count), param_count }
} }
@ -701,54 +581,42 @@ fn walk_mut_binders(
} }
impl Ty { impl Ty {
pub fn simple(ctor: TypeCtor) -> Ty {
Ty::Apply(ApplicationTy { ctor, parameters: Substs::empty() })
}
pub fn apply_one(ctor: TypeCtor, param: Ty) -> Ty {
Ty::Apply(ApplicationTy { ctor, parameters: Substs::single(param) })
}
pub fn apply(ctor: TypeCtor, parameters: Substs) -> Ty {
Ty::Apply(ApplicationTy { ctor, parameters })
}
pub fn unit() -> Self { pub fn unit() -> Self {
Ty::apply(TypeCtor::Tuple { cardinality: 0 }, Substs::empty()) Ty::Tuple { cardinality: 0, substs: Substs::empty() }
} }
pub fn fn_ptr(sig: FnSig) -> Self { pub fn fn_ptr(sig: FnSig) -> Self {
Ty::apply( Ty::FnPtr {
TypeCtor::FnPtr { num_args: sig.params().len() as u16, is_varargs: sig.is_varargs }, num_args: sig.params().len() as u16,
Substs(sig.params_and_return), is_varargs: sig.is_varargs,
) substs: Substs(sig.params_and_return),
}
} }
pub fn builtin(builtin: BuiltinType) -> Self { pub fn builtin(builtin: BuiltinType) -> Self {
Ty::simple(match builtin { match builtin {
BuiltinType::Char => TypeCtor::Scalar(Scalar::Char), BuiltinType::Char => Ty::Scalar(Scalar::Char),
BuiltinType::Bool => TypeCtor::Scalar(Scalar::Bool), BuiltinType::Bool => Ty::Scalar(Scalar::Bool),
BuiltinType::Str => TypeCtor::Str, BuiltinType::Str => Ty::Str,
BuiltinType::Int(t) => TypeCtor::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))), BuiltinType::Int(t) => Ty::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))),
BuiltinType::Uint(t) => { BuiltinType::Uint(t) => Ty::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t))),
TypeCtor::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t))) BuiltinType::Float(t) => Ty::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t))),
} }
BuiltinType::Float(t) => {
TypeCtor::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t)))
}
})
} }
pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
match self { match self {
Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(mutability), parameters }) => { Ty::Ref(mutability, parameters) => Some((parameters.as_single(), *mutability)),
Some((parameters.as_single(), *mutability))
}
_ => None, _ => None,
} }
} }
pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> { pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> {
match self { match self {
Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(mutability), parameters }) => { Ty::Ref(mutability, parameters) => {
Some((parameters.as_single(), Rawness::Ref, *mutability)) Some((parameters.as_single(), Rawness::Ref, *mutability))
} }
Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(mutability), parameters }) => { Ty::RawPtr(mutability, parameters) => {
Some((parameters.as_single(), Rawness::RawPtr, *mutability)) Some((parameters.as_single(), Rawness::RawPtr, *mutability))
} }
_ => None, _ => None,
@ -758,7 +626,7 @@ pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> {
pub fn strip_references(&self) -> &Ty { pub fn strip_references(&self) -> &Ty {
let mut t: &Ty = self; let mut t: &Ty = self;
while let Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(_mutability), parameters }) = t { while let Ty::Ref(_mutability, parameters) = t {
t = parameters.as_single(); t = parameters.as_single();
} }
@ -767,30 +635,64 @@ pub fn strip_references(&self) -> &Ty {
pub fn as_adt(&self) -> Option<(AdtId, &Substs)> { pub fn as_adt(&self) -> Option<(AdtId, &Substs)> {
match self { match self {
Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_def), parameters }) => { Ty::Adt(adt_def, parameters) => Some((*adt_def, parameters)),
Some((*adt_def, parameters))
}
_ => None, _ => None,
} }
} }
pub fn as_tuple(&self) -> Option<&Substs> { pub fn as_tuple(&self) -> Option<&Substs> {
match self { match self {
Ty::Apply(ApplicationTy { ctor: TypeCtor::Tuple { .. }, parameters }) => { Ty::Tuple { substs: parameters, .. } => Some(parameters),
Some(parameters) _ => None,
} }
}
pub fn as_generic_def(&self) -> Option<GenericDefId> {
match *self {
Ty::Adt(adt, ..) => Some(adt.into()),
Ty::FnDef(callable, ..) => Some(callable.into()),
Ty::AssociatedType(type_alias, ..) => Some(type_alias.into()),
Ty::ForeignType(type_alias, ..) => Some(type_alias.into()),
_ => None, _ => None,
} }
} }
pub fn is_never(&self) -> bool { pub fn is_never(&self) -> bool {
matches!(self, Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. })) matches!(self, Ty::Never)
} }
pub fn is_unknown(&self) -> bool { pub fn is_unknown(&self) -> bool {
matches!(self, Ty::Unknown) matches!(self, Ty::Unknown)
} }
pub fn equals_ctor(&self, other: &Ty) -> bool {
match (self, other) {
(Ty::Adt(adt, ..), Ty::Adt(adt2, ..)) => adt == adt2,
(Ty::Slice(_), Ty::Slice(_)) | (Ty::Array(_), Ty::Array(_)) => true,
(Ty::FnDef(def_id, ..), Ty::FnDef(def_id2, ..)) => def_id == def_id2,
(Ty::OpaqueType(ty_id, ..), Ty::OpaqueType(ty_id2, ..)) => ty_id == ty_id2,
(Ty::AssociatedType(ty_id, ..), Ty::AssociatedType(ty_id2, ..))
| (Ty::ForeignType(ty_id, ..), Ty::ForeignType(ty_id2, ..)) => ty_id == ty_id2,
(Ty::Closure { def, expr, .. }, Ty::Closure { def: def2, expr: expr2, .. }) => {
expr == expr2 && def == def2
}
(Ty::Ref(mutability, ..), Ty::Ref(mutability2, ..))
| (Ty::RawPtr(mutability, ..), Ty::RawPtr(mutability2, ..)) => {
mutability == mutability2
}
(
Ty::FnPtr { num_args, is_varargs, .. },
Ty::FnPtr { num_args: num_args2, is_varargs: is_varargs2, .. },
) => num_args == num_args2 && is_varargs == is_varargs2,
(Ty::Tuple { cardinality, .. }, Ty::Tuple { cardinality: cardinality2, .. }) => {
cardinality == cardinality2
}
(Ty::Str, Ty::Str) | (Ty::Never, Ty::Never) => true,
(Ty::Scalar(scalar), Ty::Scalar(scalar2)) => scalar == scalar2,
_ => false,
}
}
/// If this is a `dyn Trait` type, this returns the `Trait` part. /// If this is a `dyn Trait` type, this returns the `Trait` part.
pub fn dyn_trait_ref(&self) -> Option<&TraitRef> { pub fn dyn_trait_ref(&self) -> Option<&TraitRef> {
match self { match self {
@ -809,41 +711,32 @@ pub fn dyn_trait(&self) -> Option<TraitId> {
fn builtin_deref(&self) -> Option<Ty> { fn builtin_deref(&self) -> Option<Ty> {
match self { match self {
Ty::Apply(a_ty) => match a_ty.ctor { Ty::Ref(.., parameters) => Some(Ty::clone(parameters.as_single())),
TypeCtor::Ref(..) => Some(Ty::clone(a_ty.parameters.as_single())), Ty::RawPtr(.., parameters) => Some(Ty::clone(parameters.as_single())),
TypeCtor::RawPtr(..) => Some(Ty::clone(a_ty.parameters.as_single())),
_ => None,
},
_ => None, _ => None,
} }
} }
pub fn as_fn_def(&self) -> Option<FunctionId> { pub fn as_fn_def(&self) -> Option<FunctionId> {
match self { match self {
&Ty::Apply(ApplicationTy { &Ty::FnDef(CallableDefId::FunctionId(func), ..) => Some(func),
ctor: TypeCtor::FnDef(CallableDefId::FunctionId(func)),
..
}) => Some(func),
_ => None, _ => None,
} }
} }
pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<FnSig> { pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<FnSig> {
match self { match self {
Ty::Apply(a_ty) => match a_ty.ctor { Ty::FnPtr { is_varargs, substs: parameters, .. } => {
TypeCtor::FnPtr { is_varargs, .. } => { Some(FnSig::from_fn_ptr_substs(&parameters, *is_varargs))
Some(FnSig::from_fn_ptr_substs(&a_ty.parameters, is_varargs)) }
} Ty::FnDef(def, parameters) => {
TypeCtor::FnDef(def) => { let sig = db.callable_item_signature(*def);
let sig = db.callable_item_signature(def); Some(sig.subst(&parameters))
Some(sig.subst(&a_ty.parameters)) }
} Ty::Closure { substs: parameters, .. } => {
TypeCtor::Closure { .. } => { let sig_param = &parameters[0];
let sig_param = &a_ty.parameters[0]; sig_param.callable_sig(db)
sig_param.callable_sig(db) }
}
_ => None,
},
_ => None, _ => None,
} }
} }
@ -852,28 +745,69 @@ pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<FnSig> {
/// the `Substs` for these type parameters with the given ones. (So e.g. if /// the `Substs` for these type parameters with the given ones. (So e.g. if
/// `self` is `Option<_>` and the substs contain `u32`, we'll have /// `self` is `Option<_>` and the substs contain `u32`, we'll have
/// `Option<u32>` afterwards.) /// `Option<u32>` afterwards.)
pub fn apply_substs(self, substs: Substs) -> Ty { pub fn apply_substs(mut self, new_substs: Substs) -> Ty {
match self { match &mut self {
Ty::Apply(ApplicationTy { ctor, parameters: previous_substs }) => { Ty::Adt(_, substs)
assert_eq!(previous_substs.len(), substs.len()); | Ty::Slice(substs)
Ty::Apply(ApplicationTy { ctor, parameters: substs }) | Ty::Array(substs)
| Ty::RawPtr(_, substs)
| Ty::Ref(_, substs)
| Ty::FnDef(_, substs)
| Ty::FnPtr { substs, .. }
| Ty::Tuple { substs, .. }
| Ty::OpaqueType(_, substs)
| Ty::AssociatedType(_, substs)
| Ty::ForeignType(_, substs)
| Ty::Closure { substs, .. } => {
assert_eq!(substs.len(), new_substs.len());
*substs = new_substs;
} }
_ => self, _ => (),
} }
self
} }
/// Returns the type parameters of this type if it has some (i.e. is an ADT /// Returns the type parameters of this type if it has some (i.e. is an ADT
/// or function); so if `self` is `Option<u32>`, this returns the `u32`. /// or function); so if `self` is `Option<u32>`, this returns the `u32`.
pub fn substs(&self) -> Option<Substs> { pub fn substs(&self) -> Option<&Substs> {
match self { match self {
Ty::Apply(ApplicationTy { parameters, .. }) => Some(parameters.clone()), Ty::Adt(_, substs)
| Ty::Slice(substs)
| Ty::Array(substs)
| Ty::RawPtr(_, substs)
| Ty::Ref(_, substs)
| Ty::FnDef(_, substs)
| Ty::FnPtr { substs, .. }
| Ty::Tuple { substs, .. }
| Ty::OpaqueType(_, substs)
| Ty::AssociatedType(_, substs)
| Ty::ForeignType(_, substs)
| Ty::Closure { substs, .. } => Some(substs),
_ => None,
}
}
pub fn substs_mut(&mut self) -> Option<&mut Substs> {
match self {
Ty::Adt(_, substs)
| Ty::Slice(substs)
| Ty::Array(substs)
| Ty::RawPtr(_, substs)
| Ty::Ref(_, substs)
| Ty::FnDef(_, substs)
| Ty::FnPtr { substs, .. }
| Ty::Tuple { substs, .. }
| Ty::OpaqueType(_, substs)
| Ty::AssociatedType(_, substs)
| Ty::ForeignType(_, substs)
| Ty::Closure { substs, .. } => Some(substs),
_ => None, _ => None,
} }
} }
pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<GenericPredicate>> { pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<GenericPredicate>> {
match self { match self {
Ty::Apply(ApplicationTy { ctor: TypeCtor::OpaqueType(opaque_ty_id), .. }) => { Ty::OpaqueType(opaque_ty_id, ..) => {
match opaque_ty_id { match opaque_ty_id {
OpaqueTyId::AsyncBlockTypeImplTrait(def, _expr) => { OpaqueTyId::AsyncBlockTypeImplTrait(def, _expr) => {
let krate = def.module(db.upcast()).krate(); let krate = def.module(db.upcast()).krate();
@ -934,7 +868,7 @@ pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<GenericPredi
pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> { pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> {
match self { match self {
Ty::Apply(ApplicationTy { ctor: TypeCtor::AssociatedType(type_alias_id), .. }) => { Ty::AssociatedType(type_alias_id, ..) => {
match type_alias_id.lookup(db.upcast()).container { match type_alias_id.lookup(db.upcast()).container {
AssocContainerId::TraitId(trait_id) => Some(trait_id), AssocContainerId::TraitId(trait_id) => Some(trait_id),
_ => None, _ => None,
@ -1049,11 +983,6 @@ fn shift_bound_vars(self, n: DebruijnIndex) -> Self
impl TypeWalk for Ty { impl TypeWalk for Ty {
fn walk(&self, f: &mut impl FnMut(&Ty)) { fn walk(&self, f: &mut impl FnMut(&Ty)) {
match self { match self {
Ty::Apply(a_ty) => {
for t in a_ty.parameters.iter() {
t.walk(f);
}
}
Ty::Projection(p_ty) => { Ty::Projection(p_ty) => {
for t in p_ty.parameters.iter() { for t in p_ty.parameters.iter() {
t.walk(f); t.walk(f);
@ -1069,7 +998,13 @@ fn walk(&self, f: &mut impl FnMut(&Ty)) {
t.walk(f); t.walk(f);
} }
} }
Ty::Placeholder { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {} _ => {
if let Some(substs) = self.substs() {
for t in substs.iter() {
t.walk(f);
}
}
}
} }
f(self); f(self);
} }
@ -1080,9 +1015,6 @@ fn walk_mut_binders(
binders: DebruijnIndex, binders: DebruijnIndex,
) { ) {
match self { match self {
Ty::Apply(a_ty) => {
a_ty.parameters.walk_mut_binders(f, binders);
}
Ty::Projection(p_ty) => { Ty::Projection(p_ty) => {
p_ty.parameters.walk_mut_binders(f, binders); p_ty.parameters.walk_mut_binders(f, binders);
} }
@ -1094,7 +1026,11 @@ fn walk_mut_binders(
Ty::Opaque(o_ty) => { Ty::Opaque(o_ty) => {
o_ty.parameters.walk_mut_binders(f, binders); o_ty.parameters.walk_mut_binders(f, binders);
} }
Ty::Placeholder { .. } | Ty::Bound(_) | Ty::Infer(_) | Ty::Unknown => {} _ => {
if let Some(substs) = self.substs_mut() {
substs.walk_mut_binders(f, binders);
}
}
} }
f(self, binders); f(self, binders);
} }

View File

@ -33,7 +33,7 @@
}, },
Binders, BoundVar, DebruijnIndex, FnSig, GenericPredicate, OpaqueTy, OpaqueTyId, PolyFnSig, Binders, BoundVar, DebruijnIndex, FnSig, GenericPredicate, OpaqueTy, OpaqueTyId, PolyFnSig,
ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, ReturnTypeImplTraits, Substs, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, ReturnTypeImplTraits, Substs,
TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, TraitEnvironment, TraitRef, Ty, TypeWalk,
}; };
#[derive(Debug)] #[derive(Debug)]
@ -145,13 +145,10 @@ pub fn from_hir(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> Self {
pub fn from_hir_ext(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> (Self, Option<TypeNs>) { pub fn from_hir_ext(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> (Self, Option<TypeNs>) {
let mut res = None; let mut res = None;
let ty = match type_ref { let ty = match type_ref {
TypeRef::Never => Ty::simple(TypeCtor::Never), TypeRef::Never => Ty::Never,
TypeRef::Tuple(inner) => { TypeRef::Tuple(inner) => {
let inner_tys: Arc<[Ty]> = inner.iter().map(|tr| Ty::from_hir(ctx, tr)).collect(); let inner_tys: Arc<[Ty]> = inner.iter().map(|tr| Ty::from_hir(ctx, tr)).collect();
Ty::apply( Ty::Tuple { cardinality: inner_tys.len() as u16, substs: Substs(inner_tys) }
TypeCtor::Tuple { cardinality: inner_tys.len() as u16 },
Substs(inner_tys),
)
} }
TypeRef::Path(path) => { TypeRef::Path(path) => {
let (ty, res_) = Ty::from_hir_path(ctx, path); let (ty, res_) = Ty::from_hir_path(ctx, path);
@ -160,27 +157,24 @@ pub fn from_hir_ext(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> (Self, O
} }
TypeRef::RawPtr(inner, mutability) => { TypeRef::RawPtr(inner, mutability) => {
let inner_ty = Ty::from_hir(ctx, inner); let inner_ty = Ty::from_hir(ctx, inner);
Ty::apply_one(TypeCtor::RawPtr(*mutability), inner_ty) Ty::RawPtr(*mutability, Substs::single(inner_ty))
} }
TypeRef::Array(inner) => { TypeRef::Array(inner) => {
let inner_ty = Ty::from_hir(ctx, inner); let inner_ty = Ty::from_hir(ctx, inner);
Ty::apply_one(TypeCtor::Array, inner_ty) Ty::Array(Substs::single(inner_ty))
} }
TypeRef::Slice(inner) => { TypeRef::Slice(inner) => {
let inner_ty = Ty::from_hir(ctx, inner); let inner_ty = Ty::from_hir(ctx, inner);
Ty::apply_one(TypeCtor::Slice, inner_ty) Ty::Slice(Substs::single(inner_ty))
} }
TypeRef::Reference(inner, _, mutability) => { TypeRef::Reference(inner, _, mutability) => {
let inner_ty = Ty::from_hir(ctx, inner); let inner_ty = Ty::from_hir(ctx, inner);
Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty) Ty::Ref(*mutability, Substs::single(inner_ty))
} }
TypeRef::Placeholder => Ty::Unknown, TypeRef::Placeholder => Ty::Unknown,
TypeRef::Fn(params, is_varargs) => { TypeRef::Fn(params, is_varargs) => {
let sig = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect()); let sig = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect());
Ty::apply( Ty::FnPtr { num_args: sig.len() as u16 - 1, is_varargs: *is_varargs, substs: sig }
TypeCtor::FnPtr { num_args: sig.len() as u16 - 1, is_varargs: *is_varargs },
sig,
)
} }
TypeRef::DynTrait(bounds) => { TypeRef::DynTrait(bounds) => {
let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0));
@ -414,7 +408,6 @@ pub(crate) fn from_partly_resolved_hir_path(
// FIXME: report error // FIXME: report error
TypeNs::EnumVariantId(_) => return (Ty::Unknown, None), TypeNs::EnumVariantId(_) => return (Ty::Unknown, None),
}; };
Ty::from_type_relative_path(ctx, ty, Some(resolution), remaining_segments) Ty::from_type_relative_path(ctx, ty, Some(resolution), remaining_segments)
} }
@ -1025,7 +1018,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
let generics = generics(db.upcast(), def.into()); let generics = generics(db.upcast(), def.into());
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) Binders::new(substs.len(), Ty::FnDef(def.into(), substs))
} }
/// Build the declared type of a const. /// Build the declared type of a const.
@ -1068,7 +1061,7 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T
} }
let generics = generics(db.upcast(), def.into()); let generics = generics(db.upcast(), def.into());
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) Binders::new(substs.len(), Ty::FnDef(def.into(), substs))
} }
fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig { fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig {
@ -1093,13 +1086,13 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
} }
let generics = generics(db.upcast(), def.parent.into()); let generics = generics(db.upcast(), def.parent.into());
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) Binders::new(substs.len(), Ty::FnDef(def.into(), substs))
} }
fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
let generics = generics(db.upcast(), adt.into()); let generics = generics(db.upcast(), adt.into());
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
Binders::new(substs.len(), Ty::apply(TypeCtor::Adt(adt), substs)) Binders::new(substs.len(), Ty::Adt(adt, substs))
} }
fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
@ -1109,7 +1102,7 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
if db.type_alias_data(t).is_extern { if db.type_alias_data(t).is_extern {
Binders::new(substs.len(), Ty::apply(TypeCtor::ForeignType(t), substs)) Binders::new(substs.len(), Ty::ForeignType(t, substs))
} else { } else {
let type_ref = &db.type_alias_data(t).type_ref; let type_ref = &db.type_alias_data(t).type_ref;
let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error)); let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error));

View File

@ -7,8 +7,8 @@
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use base_db::CrateId; use base_db::CrateId;
use hir_def::{ use hir_def::{
lang_item::LangItemTarget, type_ref::Mutability, AssocContainerId, AssocItemId, FunctionId, lang_item::LangItemTarget, type_ref::Mutability, AdtId, AssocContainerId, AssocItemId,
GenericDefId, HasModule, ImplId, Lookup, ModuleId, TraitId, FunctionId, GenericDefId, HasModule, ImplId, Lookup, ModuleId, TraitId, TypeAliasId,
}; };
use hir_expand::name::Name; use hir_expand::name::Name;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
@ -18,15 +18,24 @@
db::HirDatabase, db::HirDatabase,
primitive::{self, FloatTy, IntTy, UintTy}, primitive::{self, FloatTy, IntTy, UintTy},
utils::all_super_traits, utils::all_super_traits,
ApplicationTy, Canonical, DebruijnIndex, InEnvironment, Scalar, Substs, TraitEnvironment, Canonical, DebruijnIndex, InEnvironment, Scalar, Substs, TraitEnvironment, TraitRef, Ty,
TraitRef, Ty, TyKind, TypeCtor, TypeWalk, TyKind, TypeWalk,
}; };
/// This is used as a key for indexing impls. /// This is used as a key for indexing impls.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum TyFingerprint { pub enum TyFingerprint {
Apply(TypeCtor), Str,
Slice,
Array,
Never,
RawPtr(Mutability),
Scalar(Scalar),
Adt(AdtId),
Dyn(TraitId), Dyn(TraitId),
Tuple { cardinality: u16 },
ForeignType(TypeAliasId),
FnPtr { num_args: u16, is_varargs: bool },
} }
impl TyFingerprint { impl TyFingerprint {
@ -34,32 +43,44 @@ impl TyFingerprint {
/// have impls: if we have some `struct S`, we can have an `impl S`, but not /// have impls: if we have some `struct S`, we can have an `impl S`, but not
/// `impl &S`. Hence, this will return `None` for reference types and such. /// `impl &S`. Hence, this will return `None` for reference types and such.
pub(crate) fn for_impl(ty: &Ty) -> Option<TyFingerprint> { pub(crate) fn for_impl(ty: &Ty) -> Option<TyFingerprint> {
match ty { let fp = match ty {
Ty::Apply(a_ty) => Some(TyFingerprint::Apply(a_ty.ctor)), &Ty::Str => TyFingerprint::Str,
Ty::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_)), &Ty::Never => TyFingerprint::Never,
_ => None, &Ty::Slice(..) => TyFingerprint::Slice,
} &Ty::Array(..) => TyFingerprint::Array,
&Ty::Scalar(scalar) => TyFingerprint::Scalar(scalar),
&Ty::Adt(adt, _) => TyFingerprint::Adt(adt),
&Ty::Tuple { cardinality: u16, .. } => TyFingerprint::Tuple { cardinality: u16 },
&Ty::RawPtr(mutability, ..) => TyFingerprint::RawPtr(mutability),
&Ty::ForeignType(alias_id, ..) => TyFingerprint::ForeignType(alias_id),
&Ty::FnPtr { num_args, is_varargs, .. } => {
TyFingerprint::FnPtr { num_args, is_varargs }
}
Ty::Dyn(_) => ty.dyn_trait().map(|trait_| TyFingerprint::Dyn(trait_))?,
_ => return None,
};
Some(fp)
} }
} }
pub(crate) const ALL_INT_FPS: [TyFingerprint; 12] = [ pub(crate) const ALL_INT_FPS: [TyFingerprint; 12] = [
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::I8))), TyFingerprint::Scalar(Scalar::Int(IntTy::I8)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::I16))), TyFingerprint::Scalar(Scalar::Int(IntTy::I16)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::I32))), TyFingerprint::Scalar(Scalar::Int(IntTy::I32)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::I64))), TyFingerprint::Scalar(Scalar::Int(IntTy::I64)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::I128))), TyFingerprint::Scalar(Scalar::Int(IntTy::I128)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::Isize))), TyFingerprint::Scalar(Scalar::Int(IntTy::Isize)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::U8))), TyFingerprint::Scalar(Scalar::Uint(UintTy::U8)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::U16))), TyFingerprint::Scalar(Scalar::Uint(UintTy::U16)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::U32))), TyFingerprint::Scalar(Scalar::Uint(UintTy::U32)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::U64))), TyFingerprint::Scalar(Scalar::Uint(UintTy::U64)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::U128))), TyFingerprint::Scalar(Scalar::Uint(UintTy::U128)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::Usize))), TyFingerprint::Scalar(Scalar::Uint(UintTy::Usize)),
]; ];
pub(crate) const ALL_FLOAT_FPS: [TyFingerprint; 2] = [ pub(crate) const ALL_FLOAT_FPS: [TyFingerprint; 2] = [
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Float(FloatTy::F32))), TyFingerprint::Scalar(Scalar::Float(FloatTy::F32)),
TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Float(FloatTy::F64))), TyFingerprint::Scalar(Scalar::Float(FloatTy::F64)),
]; ];
/// Trait impls defined or available in some crate. /// Trait impls defined or available in some crate.
@ -211,32 +232,29 @@ macro_rules! lang_item_crate {
let mod_to_crate_ids = |module: ModuleId| Some(std::iter::once(module.krate()).collect()); let mod_to_crate_ids = |module: ModuleId| Some(std::iter::once(module.krate()).collect());
let lang_item_targets = match self { let lang_item_targets = match self {
Ty::Apply(a_ty) => match a_ty.ctor { Ty::Adt(def_id, _) => {
TypeCtor::Adt(def_id) => { return mod_to_crate_ids(def_id.module(db.upcast()));
return mod_to_crate_ids(def_id.module(db.upcast())); }
} Ty::ForeignType(type_alias_id, _) => {
TypeCtor::ForeignType(type_alias_id) => { return mod_to_crate_ids(type_alias_id.lookup(db.upcast()).module(db.upcast()));
return mod_to_crate_ids(type_alias_id.lookup(db.upcast()).module(db.upcast())); }
} Ty::Scalar(Scalar::Bool) => lang_item_crate!("bool"),
TypeCtor::Scalar(Scalar::Bool) => lang_item_crate!("bool"), Ty::Scalar(Scalar::Char) => lang_item_crate!("char"),
TypeCtor::Scalar(Scalar::Char) => lang_item_crate!("char"), Ty::Scalar(Scalar::Float(f)) => match f {
TypeCtor::Scalar(Scalar::Float(f)) => match f { // There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
// There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime) FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"),
FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"), FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"),
FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"),
},
TypeCtor::Scalar(Scalar::Int(t)) => {
lang_item_crate!(primitive::int_ty_to_string(t))
}
TypeCtor::Scalar(Scalar::Uint(t)) => {
lang_item_crate!(primitive::uint_ty_to_string(t))
}
TypeCtor::Str => lang_item_crate!("str_alloc", "str"),
TypeCtor::Slice => lang_item_crate!("slice_alloc", "slice"),
TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!("const_ptr"),
TypeCtor::RawPtr(Mutability::Mut) => lang_item_crate!("mut_ptr"),
_ => return None,
}, },
&Ty::Scalar(Scalar::Int(t)) => {
lang_item_crate!(primitive::int_ty_to_string(t))
}
&Ty::Scalar(Scalar::Uint(t)) => {
lang_item_crate!(primitive::uint_ty_to_string(t))
}
Ty::Str => lang_item_crate!("str_alloc", "str"),
Ty::Slice(_) => lang_item_crate!("slice_alloc", "slice"),
Ty::RawPtr(Mutability::Shared, _) => lang_item_crate!("const_ptr"),
Ty::RawPtr(Mutability::Mut, _) => lang_item_crate!("mut_ptr"),
Ty::Dyn(_) => { Ty::Dyn(_) => {
return self.dyn_trait().and_then(|trait_| { return self.dyn_trait().and_then(|trait_| {
mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast())) mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast()))
@ -413,7 +431,7 @@ fn iterate_method_candidates_with_autoref(
} }
let refed = Canonical { let refed = Canonical {
kinds: deref_chain[0].kinds.clone(), kinds: deref_chain[0].kinds.clone(),
value: Ty::apply_one(TypeCtor::Ref(Mutability::Shared), deref_chain[0].value.clone()), value: Ty::Ref(Mutability::Shared, Substs::single(deref_chain[0].value.clone())),
}; };
if iterate_method_candidates_by_receiver( if iterate_method_candidates_by_receiver(
&refed, &refed,
@ -429,7 +447,7 @@ fn iterate_method_candidates_with_autoref(
} }
let ref_muted = Canonical { let ref_muted = Canonical {
kinds: deref_chain[0].kinds.clone(), kinds: deref_chain[0].kinds.clone(),
value: Ty::apply_one(TypeCtor::Ref(Mutability::Mut), deref_chain[0].value.clone()), value: Ty::Ref(Mutability::Mut, Substs::single(deref_chain[0].value.clone())),
}; };
if iterate_method_candidates_by_receiver( if iterate_method_candidates_by_receiver(
&ref_muted, &ref_muted,
@ -756,11 +774,9 @@ fn autoderef_method_receiver(
) -> Vec<Canonical<Ty>> { ) -> Vec<Canonical<Ty>> {
let mut deref_chain: Vec<_> = autoderef::autoderef(db, Some(krate), ty).collect(); let mut deref_chain: Vec<_> = autoderef::autoderef(db, Some(krate), ty).collect();
// As a last step, we can do array unsizing (that's the only unsizing that rustc does for method receivers!) // As a last step, we can do array unsizing (that's the only unsizing that rustc does for method receivers!)
if let Some(Ty::Apply(ApplicationTy { ctor: TypeCtor::Array, parameters })) = if let Some(Ty::Array(parameters)) = deref_chain.last().map(|ty| &ty.value) {
deref_chain.last().map(|ty| &ty.value)
{
let kinds = deref_chain.last().unwrap().kinds.clone(); let kinds = deref_chain.last().unwrap().kinds.clone();
let unsized_ty = Ty::apply(TypeCtor::Slice, parameters.clone()); let unsized_ty = Ty::Slice(parameters.clone());
deref_chain.push(Canonical { value: unsized_ty, kinds }) deref_chain.push(Canonical { value: unsized_ty, kinds })
} }
deref_chain deref_chain

View File

@ -1,30 +1,23 @@
//! Helper functions for binary operator type inference. //! Helper functions for binary operator type inference.
use hir_def::expr::{ArithOp, BinaryOp, CmpOp}; use hir_def::expr::{ArithOp, BinaryOp, CmpOp};
use super::{InferTy, Ty, TypeCtor}; use crate::{InferTy, Scalar, Ty};
use crate::{ApplicationTy, Scalar};
pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
match op { match op {
BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Scalar(Scalar::Bool)), BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::Scalar(Scalar::Bool),
BinaryOp::Assignment { .. } => Ty::unit(), BinaryOp::Assignment { .. } => Ty::unit(),
BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => match lhs_ty { BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => match lhs_ty {
Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { Ty::Scalar(Scalar::Int(_))
TypeCtor::Scalar(Scalar::Int(_)) | Ty::Scalar(Scalar::Uint(_))
| TypeCtor::Scalar(Scalar::Uint(_)) | Ty::Scalar(Scalar::Float(_)) => lhs_ty,
| TypeCtor::Scalar(Scalar::Float(_)) => lhs_ty,
_ => Ty::Unknown,
},
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
BinaryOp::ArithOp(_) => match rhs_ty { BinaryOp::ArithOp(_) => match rhs_ty {
Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { Ty::Scalar(Scalar::Int(_))
TypeCtor::Scalar(Scalar::Int(_)) | Ty::Scalar(Scalar::Uint(_))
| TypeCtor::Scalar(Scalar::Uint(_)) | Ty::Scalar(Scalar::Float(_)) => rhs_ty,
| TypeCtor::Scalar(Scalar::Float(_)) => rhs_ty,
_ => Ty::Unknown,
},
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
@ -33,13 +26,10 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
match op { match op {
BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Scalar(Scalar::Bool)), BinaryOp::LogicOp(..) => Ty::Scalar(Scalar::Bool),
BinaryOp::Assignment { op: None } => lhs_ty, BinaryOp::Assignment { op: None } => lhs_ty,
BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty { BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty {
Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { Ty::Scalar(_) | Ty::Str => lhs_ty,
TypeCtor::Scalar(_) | TypeCtor::Str => lhs_ty,
_ => Ty::Unknown,
},
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
@ -47,12 +37,9 @@ pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
BinaryOp::CmpOp(CmpOp::Ord { .. }) BinaryOp::CmpOp(CmpOp::Ord { .. })
| BinaryOp::Assignment { op: Some(_) } | BinaryOp::Assignment { op: Some(_) }
| BinaryOp::ArithOp(_) => match lhs_ty { | BinaryOp::ArithOp(_) => match lhs_ty {
Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { Ty::Scalar(Scalar::Int(_))
TypeCtor::Scalar(Scalar::Int(_)) | Ty::Scalar(Scalar::Uint(_))
| TypeCtor::Scalar(Scalar::Uint(_)) | Ty::Scalar(Scalar::Float(_)) => lhs_ty,
| TypeCtor::Scalar(Scalar::Float(_)) => lhs_ty,
_ => Ty::Unknown,
},
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },

View File

@ -20,7 +20,7 @@
method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS},
utils::generics, utils::generics,
BoundVar, CallableDefId, DebruijnIndex, FnSig, GenericPredicate, ProjectionPredicate, BoundVar, CallableDefId, DebruijnIndex, FnSig, GenericPredicate, ProjectionPredicate,
ProjectionTy, Substs, TraitRef, Ty, TypeCtor, ProjectionTy, Substs, TraitRef, Ty,
}; };
use mapping::{ use mapping::{
convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType, convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType,
@ -489,10 +489,11 @@ pub(crate) fn struct_datum_query(
struct_id: AdtId, struct_id: AdtId,
) -> Arc<StructDatum> { ) -> Arc<StructDatum> {
debug!("struct_datum {:?}", struct_id); debug!("struct_datum {:?}", struct_id);
let type_ctor = TypeCtor::Adt(from_chalk(db, struct_id)); let adt_id = from_chalk(db, struct_id);
let type_ctor = Ty::Adt(adt_id, Substs::empty());
debug!("struct {:?} = {:?}", struct_id, type_ctor); debug!("struct {:?} = {:?}", struct_id, type_ctor);
let num_params = type_ctor.num_ty_params(db); let num_params = generics(db.upcast(), adt_id.into()).len();
let upstream = type_ctor.krate(db) != Some(krate); let upstream = adt_id.module(db.upcast()).krate() != krate;
let where_clauses = type_ctor let where_clauses = type_ctor
.as_generic_def() .as_generic_def()
.map(|generic_def| { .map(|generic_def| {

View File

@ -16,9 +16,8 @@
db::HirDatabase, db::HirDatabase,
primitive::UintTy, primitive::UintTy,
traits::{Canonical, Obligation}, traits::{Canonical, Obligation},
ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, ProjectionPredicate,
ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind,
TypeCtor,
}; };
use super::interner::*; use super::interner::*;
@ -28,75 +27,71 @@ impl ToChalk for Ty {
type Chalk = chalk_ir::Ty<Interner>; type Chalk = chalk_ir::Ty<Interner>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> {
match self { match self {
Ty::Apply(apply_ty) => match apply_ty.ctor { Ty::Ref(m, parameters) => ref_to_chalk(db, m, parameters),
TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), Ty::Array(parameters) => array_to_chalk(db, parameters),
TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), Ty::FnPtr { num_args: _, is_varargs, substs } => {
TypeCtor::FnPtr { num_args: _, is_varargs } => { let substitution = chalk_ir::FnSubst(substs.to_chalk(db).shifted_in(&Interner));
let substitution = chalk_ir::TyKind::Function(chalk_ir::FnPointer {
chalk_ir::FnSubst(apply_ty.parameters.to_chalk(db).shifted_in(&Interner)); num_binders: 0,
chalk_ir::TyKind::Function(chalk_ir::FnPointer { sig: chalk_ir::FnSig {
num_binders: 0, abi: (),
sig: chalk_ir::FnSig { safety: chalk_ir::Safety::Safe,
abi: (), variadic: is_varargs,
safety: chalk_ir::Safety::Safe, },
variadic: is_varargs, substitution,
}, })
substitution, .intern(&Interner)
}) }
.intern(&Interner) Ty::AssociatedType(type_alias, substs) => {
} let assoc_type = TypeAliasAsAssocType(type_alias);
TypeCtor::AssociatedType(type_alias) => { let assoc_type_id = assoc_type.to_chalk(db);
let assoc_type = TypeAliasAsAssocType(type_alias); let substitution = substs.to_chalk(db);
let assoc_type_id = assoc_type.to_chalk(db); chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
let substitution = apply_ty.parameters.to_chalk(db); }
chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
}
TypeCtor::OpaqueType(impl_trait_id) => { Ty::OpaqueType(impl_trait_id, substs) => {
let id = impl_trait_id.to_chalk(db); let id = impl_trait_id.to_chalk(db);
let substitution = apply_ty.parameters.to_chalk(db); let substitution = substs.to_chalk(db);
chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner)
} }
TypeCtor::ForeignType(type_alias) => { Ty::ForeignType(type_alias, _) => {
let foreign_type = TypeAliasAsForeignType(type_alias); let foreign_type = TypeAliasAsForeignType(type_alias);
let foreign_type_id = foreign_type.to_chalk(db); let foreign_type_id = foreign_type.to_chalk(db);
chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner)
} }
TypeCtor::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), Ty::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner),
TypeCtor::Tuple { cardinality } => { Ty::Tuple { cardinality, substs } => {
let substitution = apply_ty.parameters.to_chalk(db); let substitution = substs.to_chalk(db);
chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner)
} }
TypeCtor::RawPtr(mutability) => { Ty::RawPtr(mutability, substs) => {
let ty = apply_ty.parameters[0].clone().to_chalk(db); let ty = substs[0].clone().to_chalk(db);
chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner) chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner)
} }
TypeCtor::Slice => { Ty::Slice(substs) => {
chalk_ir::TyKind::Slice(apply_ty.parameters[0].clone().to_chalk(db)) chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner)
.intern(&Interner) }
} Ty::Str => chalk_ir::TyKind::Str.intern(&Interner),
TypeCtor::Str => chalk_ir::TyKind::Str.intern(&Interner), Ty::FnDef(callable_def, substs) => {
TypeCtor::FnDef(callable_def) => { let id = callable_def.to_chalk(db);
let id = callable_def.to_chalk(db); let substitution = substs.to_chalk(db);
let substitution = apply_ty.parameters.to_chalk(db); chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner)
chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) }
} Ty::Never => chalk_ir::TyKind::Never.intern(&Interner),
TypeCtor::Never => chalk_ir::TyKind::Never.intern(&Interner),
TypeCtor::Closure { def, expr } => { Ty::Closure { def, expr, substs } => {
let closure_id = db.intern_closure((def, expr)); let closure_id = db.intern_closure((def, expr));
let substitution = apply_ty.parameters.to_chalk(db); let substitution = substs.to_chalk(db);
chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner) chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner)
} }
TypeCtor::Adt(adt_id) => { Ty::Adt(adt_id, substs) => {
let substitution = apply_ty.parameters.to_chalk(db); let substitution = substs.to_chalk(db);
chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner) chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner)
} }
},
Ty::Projection(proj_ty) => { Ty::Projection(proj_ty) => {
let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db);
let substitution = proj_ty.parameters.to_chalk(db); let substitution = proj_ty.parameters.to_chalk(db);
@ -143,9 +138,7 @@ fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> {
fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
match chalk.data(&Interner).kind.clone() { match chalk.data(&Interner).kind.clone() {
chalk_ir::TyKind::Error => Ty::Unknown, chalk_ir::TyKind::Error => Ty::Unknown,
chalk_ir::TyKind::Array(ty, _size) => { chalk_ir::TyKind::Array(ty, _size) => Ty::Array(Substs::single(from_chalk(db, ty))),
Ty::apply(TypeCtor::Array, Substs::single(from_chalk(db, ty)))
}
chalk_ir::TyKind::Placeholder(idx) => { chalk_ir::TyKind::Placeholder(idx) => {
assert_eq!(idx.ui, UniverseIndex::ROOT); assert_eq!(idx.ui, UniverseIndex::ROOT);
let interned_id = crate::db::GlobalTypeParamId::from_intern_id( let interned_id = crate::db::GlobalTypeParamId::from_intern_id(
@ -175,13 +168,11 @@ fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
db, db,
substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"),
); );
Ty::Apply(ApplicationTy { Ty::FnPtr {
ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16,
num_args: (parameters.len() - 1) as u16, is_varargs: variadic,
is_varargs: variadic, substs: parameters,
}, }
parameters,
})
} }
chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx), chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx),
chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown, chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown,
@ -196,60 +187,50 @@ fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
Ty::Dyn(predicates) Ty::Dyn(predicates)
} }
chalk_ir::TyKind::Adt(struct_id, subst) => { chalk_ir::TyKind::Adt(struct_id, subst) => Ty::Adt(struct_id.0, from_chalk(db, subst)),
apply_ty_from_chalk(db, TypeCtor::Adt(struct_id.0), subst) chalk_ir::TyKind::AssociatedType(type_id, subst) => Ty::AssociatedType(
} from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0,
chalk_ir::TyKind::AssociatedType(type_id, subst) => apply_ty_from_chalk( from_chalk(db, subst),
db,
TypeCtor::AssociatedType(from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0),
subst,
), ),
chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => {
apply_ty_from_chalk(db, TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)), subst) Ty::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst))
} }
chalk_ir::TyKind::Scalar(scalar) => Ty::simple(TypeCtor::Scalar(scalar)), chalk_ir::TyKind::Scalar(scalar) => Ty::Scalar(scalar),
chalk_ir::TyKind::Tuple(cardinality, subst) => { chalk_ir::TyKind::Tuple(cardinality, subst) => {
apply_ty_from_chalk(db, TypeCtor::Tuple { cardinality: cardinality as u16 }, subst) Ty::Tuple { cardinality: cardinality as u16, substs: from_chalk(db, subst) }
} }
chalk_ir::TyKind::Raw(mutability, ty) => { chalk_ir::TyKind::Raw(mutability, ty) => {
Ty::apply_one(TypeCtor::RawPtr(from_chalk(db, mutability)), from_chalk(db, ty)) Ty::RawPtr(from_chalk(db, mutability), Substs::single(from_chalk(db, ty)))
} }
chalk_ir::TyKind::Slice(ty) => Ty::apply_one(TypeCtor::Slice, from_chalk(db, ty)), chalk_ir::TyKind::Slice(ty) => Ty::Slice(Substs::single(from_chalk(db, ty))),
chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => {
Ty::apply_one(TypeCtor::Ref(from_chalk(db, mutability)), from_chalk(db, ty)) Ty::Ref(from_chalk(db, mutability), Substs::single(from_chalk(db, ty)))
} }
chalk_ir::TyKind::Str => Ty::simple(TypeCtor::Str), chalk_ir::TyKind::Str => Ty::Str,
chalk_ir::TyKind::Never => Ty::simple(TypeCtor::Never), chalk_ir::TyKind::Never => Ty::Never,
chalk_ir::TyKind::FnDef(fn_def_id, subst) => { chalk_ir::TyKind::FnDef(fn_def_id, subst) => {
let callable_def = from_chalk(db, fn_def_id); Ty::FnDef(from_chalk(db, fn_def_id), from_chalk(db, subst))
apply_ty_from_chalk(db, TypeCtor::FnDef(callable_def), subst)
} }
chalk_ir::TyKind::Closure(id, subst) => { chalk_ir::TyKind::Closure(id, subst) => {
let id: crate::db::ClosureId = id.into(); let id: crate::db::ClosureId = id.into();
let (def, expr) = db.lookup_intern_closure(id); let (def, expr) = db.lookup_intern_closure(id);
apply_ty_from_chalk(db, TypeCtor::Closure { def, expr }, subst) Ty::Closure { def, expr, substs: from_chalk(db, subst) }
} }
chalk_ir::TyKind::Foreign(foreign_def_id) => Ty::simple(TypeCtor::ForeignType( chalk_ir::TyKind::Foreign(foreign_def_id) => Ty::ForeignType(
from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0, from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0,
)), Substs::empty(),
),
chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME
chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME
} }
} }
} }
fn apply_ty_from_chalk(
db: &dyn HirDatabase,
ctor: TypeCtor,
subst: chalk_ir::Substitution<Interner>,
) -> Ty {
Ty::Apply(ApplicationTy { ctor, parameters: from_chalk(db, subst) })
}
/// We currently don't model lifetimes, but Chalk does. So, we have to insert a /// We currently don't model lifetimes, but Chalk does. So, we have to insert a
/// fake lifetime here, because Chalks built-in logic may expect it to be there. /// fake lifetime here, because Chalks built-in logic may expect it to be there.
fn ref_to_chalk( fn ref_to_chalk(