use std::iter::{repeat, repeat_with};
use std::{mem, sync::Arc};
-use chalk_ir::{cast::Cast, Mutability, TyVariableKind};
+use chalk_ir::{cast::Cast, fold::Shift, ConstData, Mutability, TyVariableKind};
use hir_def::{
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
path::{GenericArg, GenericArgs},
resolver::resolver_for_expr,
+ type_ref::ConstScalar,
AssocContainerId, FieldId, Lookup,
};
use hir_expand::name::{name, Name};
use crate::{
autoderef,
lower::lower_to_chalk_mutability,
+ mapping::from_chalk,
method_resolution, op,
primitive::{self, UintTy},
- to_chalk_trait_id,
- traits::{chalk::from_chalk, FnTrait},
- utils::{generics, variant_data, Generics},
- AdtId, Binders, CallableDefId, FnPointer, FnSig, InEnvironment, Interner, Rawness, Scalar,
- Substitution, TraitRef, Ty, TyBuilder, TyKind,
+ static_lifetime, to_chalk_trait_id,
+ traits::FnTrait,
+ utils::{generics, Generics},
+ AdtId, Binders, CallableDefId, ConcreteConst, ConstValue, FnPointer, FnSig, FnSubst,
+ InEnvironment, Interner, ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty,
+ TyBuilder, TyExt, TyKind,
};
use super::{
let inner_ty = self.infer_expr(*body, &Expectation::none());
let impl_trait_id = crate::ImplTraitId::AsyncBlockTypeImplTrait(self.owner, *body);
let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into();
- TyKind::OpaqueType(opaque_ty_id, Substitution::single(inner_ty)).intern(&Interner)
+ TyKind::OpaqueType(opaque_ty_id, Substitution::from1(&Interner, inner_ty))
+ .intern(&Interner)
}
Expr::Loop { body, label } => {
self.breakables.push(BreakableContext {
};
sig_tys.push(ret_ty.clone());
let sig_ty = TyKind::Function(FnPointer {
- num_args: sig_tys.len() - 1,
+ num_binders: 0,
sig: FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic: false },
- substs: Substitution::from_iter(&Interner, sig_tys.clone()),
+ substitution: FnSubst(
+ Substitution::from_iter(&Interner, sig_tys.clone()).shifted_in(&Interner),
+ ),
})
.intern(&Interner);
let closure_id = self.db.intern_closure((self.owner, tgt_expr)).into();
let closure_ty =
- TyKind::Closure(closure_id, Substitution::single(sig_ty)).intern(&Interner);
+ TyKind::Closure(closure_id, Substitution::from1(&Interner, sig_ty))
+ .intern(&Interner);
// Eagerly try to relate the closure type with the expected
// type, otherwise we often won't have enough information to
self.normalize_associated_types_in(ret_ty)
}
Expr::MethodCall { receiver, args, method_name, generic_args } => self
- .infer_method_call(tgt_expr, *receiver, &args, &method_name, generic_args.as_ref()),
+ .infer_method_call(
+ tgt_expr,
+ *receiver,
+ &args,
+ &method_name,
+ generic_args.as_deref(),
+ ),
Expr::Match { expr, arms } => {
let input_ty = self.infer_expr(*expr, &Expectation::none());
TyKind::Never.intern(&Interner)
}
Expr::RecordLit { path, fields, spread } => {
- let (ty, def_id) = self.resolve_variant(path.as_ref());
+ let (ty, def_id) = self.resolve_variant(path.as_deref());
if let Some(variant) = def_id {
self.write_variant_resolution(tgt_expr.into(), variant);
}
self.unify(&ty, &expected.ty);
- let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner));
+ let substs = ty
+ .as_adt()
+ .map(|(_, s)| s.clone())
+ .unwrap_or_else(|| Substitution::empty(&Interner));
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| it.variant_data(self.db.upcast()));
for field in fields.iter() {
let field_def =
variant_data.as_ref().and_then(|it| match it.field(&field.name) {
None
}
});
- if let Some(field_def) = field_def {
- self.result.record_field_resolutions.insert(field.expr, field_def);
- }
let field_ty = field_def.map_or(self.err_ty(), |it| {
- field_types[it.local_id].clone().substitute(&substs)
+ field_types[it.local_id].clone().substitute(&Interner, &substs)
});
self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty));
}
};
match canonicalized.decanonicalize_ty(derefed_ty.value).kind(&Interner) {
TyKind::Tuple(_, substs) => name.as_tuple_index().and_then(|idx| {
- substs.interned().get(idx).map(|a| a.assert_ty_ref(&Interner)).cloned()
+ substs
+ .as_slice(&Interner)
+ .get(idx)
+ .map(|a| a.assert_ty_ref(&Interner))
+ .cloned()
}),
TyKind::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => {
let local_id = self.db.struct_data(*s).variant_data.field(name)?;
Some(
self.db.field_types((*s).into())[field.local_id]
.clone()
- .substitute(¶meters),
+ .substitute(&Interner, ¶meters),
)
} else {
None
Some(
self.db.field_types((*u).into())[field.local_id]
.clone()
- .substitute(¶meters),
+ .substitute(&Interner, ¶meters),
)
} else {
None
let inner_ty = self.infer_expr_inner(*expr, &expectation);
match rawness {
Rawness::RawPtr => TyKind::Raw(mutability, inner_ty),
- Rawness::Ref => TyKind::Ref(mutability, inner_ty),
+ Rawness::Ref => TyKind::Ref(mutability, static_lifetime(), inner_ty),
}
.intern(&Interner)
}
}
Expr::Array(array) => {
let elem_ty = match expected.ty.kind(&Interner) {
- TyKind::Array(st) | TyKind::Slice(st) => st.clone(),
+ TyKind::Array(st, _) | TyKind::Slice(st) => st.clone(),
_ => self.table.new_type_var(),
};
- match array {
+ let len = match array {
Array::ElementList(items) => {
for expr in items.iter() {
self.infer_expr_coerce(*expr, &Expectation::has_type(elem_ty.clone()));
}
+ Some(items.len())
}
Array::Repeat { initializer, repeat } => {
self.infer_expr_coerce(
TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner),
),
);
+ // FIXME: we don't know the length here because hir Exprs don't actually
+ // get the value out of the AST, even though it is there.
+ None
}
- }
+ };
- TyKind::Array(elem_ty).intern(&Interner)
+ let cd = ConstData {
+ ty: TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner),
+ value: ConstValue::Concrete(chalk_ir::ConcreteConst {
+ interned: len
+ .map(|len| ConstScalar::Usize(len as u64))
+ .unwrap_or(ConstScalar::Unknown),
+ }),
+ };
+ TyKind::Array(elem_ty, cd.intern(&Interner)).intern(&Interner)
}
Expr::Literal(lit) => match lit {
Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
Literal::String(..) => {
- TyKind::Ref(Mutability::Not, TyKind::Str.intern(&Interner)).intern(&Interner)
+ TyKind::Ref(Mutability::Not, static_lifetime(), TyKind::Str.intern(&Interner))
+ .intern(&Interner)
}
- Literal::ByteString(..) => {
+ Literal::ByteString(bs) => {
let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner);
- let array_type = TyKind::Array(byte_type).intern(&Interner);
- TyKind::Ref(Mutability::Not, array_type).intern(&Interner)
+
+ let len = ConstData {
+ ty: TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner),
+ value: ConstValue::Concrete(ConcreteConst {
+ interned: ConstScalar::Usize(bs.len() as u64),
+ }),
+ }
+ .intern(&Interner);
+
+ let array_type = TyKind::Array(byte_type, len).intern(&Interner);
+ TyKind::Ref(Mutability::Not, static_lifetime(), array_type).intern(&Interner)
}
Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner),
Literal::Int(_v, ty) => match ty {
let ty = self.resolve_ty_as_possible(ty);
self.infer_pat(*pat, &ty, BindingMode::default());
}
- Statement::Expr(expr) => {
+ Statement::Expr { expr, .. } => {
self.infer_expr(*expr, &Expectation::none());
}
}
None => (receiver_ty, Binders::empty(&Interner, self.err_ty()), None),
};
let substs = self.substs_for_method_call(def_generics, generic_args, &derefed_receiver_ty);
- let method_ty = method_ty.substitute(&substs);
+ let method_ty = method_ty.substitute(&Interner, &substs);
let method_ty = self.insert_type_vars(method_ty);
self.register_obligations_for_call(&method_ty);
let (expected_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) {
// Apply autoref so the below unification works correctly
// FIXME: return correct autorefs from lookup_method
let actual_receiver_ty = match expected_receiver_ty.as_reference() {
- Some((_, mutability)) => TyKind::Ref(mutability, derefed_receiver_ty).intern(&Interner),
+ Some((_, lifetime, mutability)) => {
+ TyKind::Ref(mutability, lifetime, derefed_receiver_ty).intern(&Interner)
+ }
_ => derefed_receiver_ty,
};
self.unify(&expected_receiver_ty, &actual_receiver_ty);
let def: CallableDefId = from_chalk(self.db, *fn_def);
let generic_predicates = self.db.generic_predicates(def.into());
for predicate in generic_predicates.iter() {
- let (predicate, binders) =
- predicate.clone().substitute(parameters).into_value_and_skipped_binders();
+ let (predicate, binders) = predicate
+ .clone()
+ .substitute(&Interner, parameters)
+ .into_value_and_skipped_binders();
always!(binders.len(&Interner) == 0); // quantified where clauses not yet handled
self.push_obligation(predicate.cast(&Interner));
}
if let AssocContainerId::TraitId(trait_) = f.lookup(self.db.upcast()).container
{
// construct a TraitRef
- let substs =
- parameters.prefix(generics(self.db.upcast(), trait_.into()).len());
+ let substs = crate::subst_prefix(
+ &*parameters,
+ generics(self.db.upcast(), trait_.into()).len(),
+ );
self.push_obligation(
TraitRef { trait_id: to_chalk_trait_id(trait_), substitution: substs }
.cast(&Interner),