summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/rust-analyzer/crates/hir-ty/src/infer.rs')
-rw-r--r--src/tools/rust-analyzer/crates/hir-ty/src/infer.rs116
1 files changed, 75 insertions, 41 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs b/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs
index 0b3c23f57..6b59f1c20 100644
--- a/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs
+++ b/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs
@@ -19,10 +19,11 @@ use std::sync::Arc;
use chalk_ir::{cast::Cast, ConstValue, DebruijnIndex, Mutability, Safety, Scalar, TypeFlags};
use hir_def::{
body::Body,
- builtin_type::BuiltinType,
+ builtin_type::{BuiltinInt, BuiltinType, BuiltinUint},
data::{ConstData, StaticData},
- expr::{BindingAnnotation, ExprId, PatId},
+ expr::{BindingAnnotation, ExprId, ExprOrPatId, PatId},
lang_item::LangItemTarget,
+ layout::Integer,
path::{path, Path},
resolver::{HasResolver, ResolveValueResult, Resolver, TypeNs, ValueNs},
type_ref::TypeRef,
@@ -33,7 +34,7 @@ use hir_expand::name::{name, Name};
use itertools::Either;
use la_arena::ArenaMap;
use rustc_hash::FxHashMap;
-use stdx::{always, impl_from};
+use stdx::always;
use crate::{
db::HirDatabase, fold_tys, fold_tys_and_consts, infer::coerce::CoerceMany,
@@ -70,8 +71,26 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
DefWithBodyId::StaticId(s) => ctx.collect_static(&db.static_data(s)),
DefWithBodyId::VariantId(v) => {
ctx.return_ty = TyBuilder::builtin(match db.enum_data(v.parent).variant_body_type() {
- Either::Left(builtin) => BuiltinType::Int(builtin),
- Either::Right(builtin) => BuiltinType::Uint(builtin),
+ hir_def::layout::IntegerType::Pointer(signed) => match signed {
+ true => BuiltinType::Int(BuiltinInt::Isize),
+ false => BuiltinType::Uint(BuiltinUint::Usize),
+ },
+ hir_def::layout::IntegerType::Fixed(size, signed) => match signed {
+ true => BuiltinType::Int(match size {
+ Integer::I8 => BuiltinInt::I8,
+ Integer::I16 => BuiltinInt::I16,
+ Integer::I32 => BuiltinInt::I32,
+ Integer::I64 => BuiltinInt::I64,
+ Integer::I128 => BuiltinInt::I128,
+ }),
+ false => BuiltinType::Uint(match size {
+ Integer::I8 => BuiltinUint::U8,
+ Integer::I16 => BuiltinUint::U16,
+ Integer::I32 => BuiltinUint::U32,
+ Integer::I64 => BuiltinUint::U64,
+ Integer::I128 => BuiltinUint::U128,
+ }),
+ },
});
}
}
@@ -101,13 +120,6 @@ pub(crate) fn normalize(db: &dyn HirDatabase, owner: DefWithBodyId, ty: Ty) -> T
table.resolve_completely(ty_with_vars)
}
-#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
-enum ExprOrPatId {
- ExprId(ExprId),
- PatId(PatId),
-}
-impl_from!(ExprId, PatId for ExprOrPatId);
-
/// Binding modes inferred for patterns.
/// <https://doc.rust-lang.org/reference/patterns.html#binding-modes>
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
@@ -189,6 +201,8 @@ pub(crate) type InferResult<T> = Result<InferOk<T>, TypeError>;
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum InferenceDiagnostic {
NoSuchField { expr: ExprId },
+ PrivateField { expr: ExprId, field: FieldId },
+ PrivateAssocItem { id: ExprOrPatId, item: AssocItemId },
BreakOutsideOfLoop { expr: ExprId, is_break: bool },
MismatchedArgCount { call_expr: ExprId, expected: usize, found: usize },
}
@@ -330,7 +344,7 @@ pub struct InferenceResult {
/// For each struct literal or pattern, records the variant it resolves to.
variant_resolutions: FxHashMap<ExprOrPatId, VariantId>,
/// For each associated item record what it resolves to
- assoc_resolutions: FxHashMap<ExprOrPatId, AssocItemId>,
+ assoc_resolutions: FxHashMap<ExprOrPatId, (AssocItemId, Substitution)>,
pub diagnostics: Vec<InferenceDiagnostic>,
pub type_of_expr: ArenaMap<ExprId, Ty>,
/// For each pattern record the type it resolves to.
@@ -360,11 +374,11 @@ impl InferenceResult {
pub fn variant_resolution_for_pat(&self, id: PatId) -> Option<VariantId> {
self.variant_resolutions.get(&id.into()).copied()
}
- pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<AssocItemId> {
- self.assoc_resolutions.get(&id.into()).copied()
+ pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<(AssocItemId, Substitution)> {
+ self.assoc_resolutions.get(&id.into()).cloned()
}
- pub fn assoc_resolutions_for_pat(&self, id: PatId) -> Option<AssocItemId> {
- self.assoc_resolutions.get(&id.into()).copied()
+ pub fn assoc_resolutions_for_pat(&self, id: PatId) -> Option<(AssocItemId, Substitution)> {
+ self.assoc_resolutions.get(&id.into()).cloned()
}
pub fn type_mismatch_for_expr(&self, expr: ExprId) -> Option<&TypeMismatch> {
self.type_mismatches.get(&expr.into())
@@ -484,7 +498,7 @@ impl<'a> InferenceContext<'a> {
result: InferenceResult::default(),
table: unify::InferenceTable::new(db, trait_env.clone()),
trait_env,
- return_ty: TyKind::Error.intern(Interner), // set in collect_fn_signature
+ return_ty: TyKind::Error.intern(Interner), // set in collect_* calls
resume_yield_tys: None,
db,
owner,
@@ -498,6 +512,8 @@ impl<'a> InferenceContext<'a> {
fn resolve_all(self) -> InferenceResult {
let InferenceContext { mut table, mut result, .. } = self;
+ table.fallback_if_possible();
+
// FIXME resolve obligations as well (use Guidance if necessary)
table.resolve_obligations_as_possible();
@@ -516,6 +532,9 @@ impl<'a> InferenceContext<'a> {
for (_, subst) in result.method_resolutions.values_mut() {
*subst = table.resolve_completely(subst.clone());
}
+ for (_, subst) in result.assoc_resolutions.values_mut() {
+ *subst = table.resolve_completely(subst.clone());
+ }
for adjustment in result.expr_adjustments.values_mut().flatten() {
adjustment.target = table.resolve_completely(adjustment.target.clone());
}
@@ -537,8 +556,20 @@ impl<'a> InferenceContext<'a> {
let data = self.db.function_data(func);
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)
.with_impl_trait_mode(ImplTraitLoweringMode::Param);
- let param_tys =
+ let mut param_tys =
data.params.iter().map(|(_, type_ref)| ctx.lower_ty(type_ref)).collect::<Vec<_>>();
+ // Check if function contains a va_list, if it does then we append it to the parameter types
+ // that are collected from the function data
+ if data.is_varargs() {
+ let va_list_ty = match self.resolve_va_list() {
+ Some(va_list) => TyBuilder::adt(self.db, va_list)
+ .fill_with_defaults(self.db, || self.table.new_type_var())
+ .build(),
+ None => self.err_ty(),
+ };
+
+ param_tys.push(va_list_ty)
+ }
for (ty, pat) in param_tys.into_iter().zip(self.body.params.iter()) {
let ty = self.insert_type_vars(ty);
let ty = self.normalize_associated_types_in(ty);
@@ -551,14 +582,17 @@ impl<'a> InferenceContext<'a> {
} else {
&*data.ret_type
};
- let return_ty = self.make_ty_with_mode(return_ty, ImplTraitLoweringMode::Opaque);
- self.return_ty = return_ty;
- if let Some(rpits) = self.db.return_type_impl_traits(func) {
+ let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)
+ .with_impl_trait_mode(ImplTraitLoweringMode::Opaque);
+ let return_ty = ctx.lower_ty(return_ty);
+ let return_ty = self.insert_type_vars(return_ty);
+
+ let return_ty = if let Some(rpits) = self.db.return_type_impl_traits(func) {
// RPIT opaque types use substitution of their parent function.
let fn_placeholders = TyBuilder::placeholder_subst(self.db, func);
- self.return_ty = fold_tys(
- self.return_ty.clone(),
+ fold_tys(
+ return_ty,
|ty, _| {
let opaque_ty_id = match ty.kind(Interner) {
TyKind::OpaqueType(opaque_ty_id, _) => *opaque_ty_id,
@@ -579,14 +613,18 @@ impl<'a> InferenceContext<'a> {
let (var_predicate, binders) = predicate
.substitute(Interner, &var_subst)
.into_value_and_skipped_binders();
- always!(binders.len(Interner) == 0); // quantified where clauses not yet handled
+ always!(binders.is_empty(Interner)); // quantified where clauses not yet handled
self.push_obligation(var_predicate.cast(Interner));
}
var
},
DebruijnIndex::INNERMOST,
- );
- }
+ )
+ } else {
+ return_ty
+ };
+
+ self.return_ty = self.normalize_associated_types_in(return_ty);
}
fn infer_body(&mut self) {
@@ -609,8 +647,8 @@ impl<'a> InferenceContext<'a> {
self.result.variant_resolutions.insert(id, variant);
}
- fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItemId) {
- self.result.assoc_resolutions.insert(id, item);
+ fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItemId, subs: Substitution) {
+ self.result.assoc_resolutions.insert(id, (item, subs));
}
fn write_pat_ty(&mut self, pat: PatId, ty: Ty) {
@@ -621,23 +659,14 @@ impl<'a> InferenceContext<'a> {
self.result.diagnostics.push(diagnostic);
}
- fn make_ty_with_mode(
- &mut self,
- type_ref: &TypeRef,
- impl_trait_mode: ImplTraitLoweringMode,
- ) -> Ty {
+ fn make_ty(&mut self, type_ref: &TypeRef) -> Ty {
// FIXME use right resolver for block
- let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)
- .with_impl_trait_mode(impl_trait_mode);
+ let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
let ty = ctx.lower_ty(type_ref);
let ty = self.insert_type_vars(ty);
self.normalize_associated_types_in(ty)
}
- fn make_ty(&mut self, type_ref: &TypeRef) -> Ty {
- self.make_ty_with_mode(type_ref, ImplTraitLoweringMode::Disallowed)
- }
-
fn err_ty(&self) -> Ty {
self.result.standard_types.unknown.clone()
}
@@ -656,7 +685,7 @@ impl<'a> InferenceContext<'a> {
}
}
- /// Replaces Ty::Unknown by a new type var, so we can maybe still infer it.
+ /// Replaces `Ty::Error` by a new type var, so we can maybe still infer it.
fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty {
match ty.kind(Interner) {
TyKind::Error => self.table.new_type_var(),
@@ -983,6 +1012,11 @@ impl<'a> InferenceContext<'a> {
let trait_ = self.resolve_ops_index()?;
self.db.trait_data(trait_).associated_type_by_name(&name![Output])
}
+
+ fn resolve_va_list(&self) -> Option<AdtId> {
+ let struct_ = self.resolve_lang_item(name![va_list])?.as_struct()?;
+ Some(struct_.into())
+ }
}
/// When inferring an expression, we propagate downward whatever type hint we