Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir/src/source_analyzer.rs')
| -rw-r--r-- | crates/hir/src/source_analyzer.rs | 368 |
1 files changed, 198 insertions, 170 deletions
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index 539b25387a..8144b2f737 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -7,18 +7,10 @@ //! purely for "IDE needs". use std::iter::{self, once}; -use crate::{ - Adt, AssocItem, BindingMode, BuiltinAttr, BuiltinType, Callable, Const, DeriveHelper, Field, - Function, GenericSubstitution, Local, Macro, ModuleDef, Static, Struct, ToolModule, Trait, - TupleField, Type, TypeAlias, Variant, - db::HirDatabase, - semantics::{PathResolution, PathResolutionPerNs}, -}; -use base_db::salsa; use either::Either; use hir_def::{ AdtId, AssocItemId, CallableDefId, ConstId, DefWithBodyId, FieldId, FunctionId, GenericDefId, - ItemContainerId, LocalFieldId, Lookup, ModuleDefId, StructId, TraitId, VariantId, + LocalFieldId, ModuleDefId, StructId, TraitId, VariantId, expr_store::{ Body, BodySourceMap, ExpressionStore, ExpressionStoreSourceMap, HygieneId, lower::ExprCollector, @@ -26,10 +18,10 @@ use hir_def::{ scope::{ExprScopes, ScopeId}, }, hir::{BindingId, Expr, ExprId, ExprOrPatId, Pat}, - lang_item::LangItem, + lang_item::LangItems, nameres::MacroSubNs, resolver::{HasResolver, Resolver, TypeNs, ValueNs, resolver_for_scope}, - type_ref::{Mutability, TypeRefId}, + type_ref::{Mutability, TypeRef, TypeRefId}, }; use hir_expand::{ HirFileId, InFile, @@ -37,18 +29,24 @@ use hir_expand::{ name::{AsName, Name}, }; use hir_ty::{ - Adjustment, AliasTy, InferenceResult, Interner, LifetimeElisionKind, ProjectionTy, - Substitution, ToChalk, TraitEnvironment, Ty, TyExt, TyKind, TyLoweringContext, + Adjustment, InferenceResult, LifetimeElisionKind, TraitEnvironment, TyLoweringContext, diagnostics::{ InsideUnsafeBlock, record_literal_missing_fields, record_pattern_missing_fields, unsafe_operations, }, - from_assoc_type_id, lang_items::lang_items_for_bin_op, - method_resolution, + method_resolution::{self, CandidateId}, + next_solver::{ + DbInterner, ErrorGuaranteed, GenericArgs, Ty, TyKind, TypingMode, infer::DbInternerInferExt, + }, + traits::structurally_normalize_ty, }; use intern::sym; use itertools::Itertools; +use rustc_type_ir::{ + AliasTyKind, + inherent::{AdtDef, IntoKind, Ty as _}, +}; use smallvec::SmallVec; use stdx::never; use syntax::{ @@ -57,22 +55,30 @@ use syntax::{ }; use triomphe::Arc; +use crate::{ + Adt, AssocItem, BindingMode, BuiltinAttr, BuiltinType, Callable, Const, DeriveHelper, Field, + Function, GenericSubstitution, Local, Macro, ModuleDef, Static, Struct, ToolModule, Trait, + TupleField, Type, TypeAlias, Variant, + db::HirDatabase, + semantics::{PathResolution, PathResolutionPerNs}, +}; + /// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of /// original source files. It should not be used inside the HIR itself. #[derive(Debug)] pub(crate) struct SourceAnalyzer<'db> { pub(crate) file_id: HirFileId, pub(crate) resolver: Resolver<'db>, - pub(crate) body_or_sig: Option<BodyOrSig>, + pub(crate) body_or_sig: Option<BodyOrSig<'db>>, } #[derive(Debug)] -pub(crate) enum BodyOrSig { +pub(crate) enum BodyOrSig<'db> { Body { def: DefWithBodyId, body: Arc<Body>, source_map: Arc<BodySourceMap>, - infer: Option<Arc<InferenceResult>>, + infer: Option<&'db InferenceResult<'db>>, }, // To be folded into body once it is considered one VariantFields { @@ -95,7 +101,7 @@ impl<'db> SourceAnalyzer<'db> { node: InFile<&SyntaxNode>, offset: Option<TextSize>, ) -> SourceAnalyzer<'db> { - Self::new_for_body_(db, def, node, offset, Some(db.infer(def))) + Self::new_for_body_(db, def, node, offset, Some(InferenceResult::for_body(db, def))) } pub(crate) fn new_for_body_no_infer( @@ -112,7 +118,7 @@ impl<'db> SourceAnalyzer<'db> { def: DefWithBodyId, node @ InFile { file_id, .. }: InFile<&SyntaxNode>, offset: Option<TextSize>, - infer: Option<Arc<InferenceResult>>, + infer: Option<&'db InferenceResult<'db>>, ) -> SourceAnalyzer<'db> { let (body, source_map) = db.body_with_source_map(def); let scopes = db.expr_scopes(def); @@ -178,7 +184,9 @@ impl<'db> SourceAnalyzer<'db> { } // FIXME: Remove this - fn body_(&self) -> Option<(DefWithBodyId, &Body, &BodySourceMap, Option<&InferenceResult>)> { + fn body_( + &self, + ) -> Option<(DefWithBodyId, &Body, &BodySourceMap, Option<&InferenceResult<'db>>)> { self.body_or_sig.as_ref().and_then(|it| match it { BodyOrSig::Body { def, body, source_map, infer } => { Some((*def, &**body, &**source_map, infer.as_deref())) @@ -187,7 +195,7 @@ impl<'db> SourceAnalyzer<'db> { }) } - fn infer(&self) -> Option<&InferenceResult> { + fn infer(&self) -> Option<&InferenceResult<'db>> { self.body_or_sig.as_ref().and_then(|it| match it { BodyOrSig::Sig { .. } => None, BodyOrSig::VariantFields { .. } => None, @@ -219,7 +227,7 @@ impl<'db> SourceAnalyzer<'db> { }) } - fn trait_environment(&self, db: &'db dyn HirDatabase) -> Arc<TraitEnvironment> { + fn trait_environment(&self, db: &'db dyn HirDatabase) -> Arc<TraitEnvironment<'db>> { self.body_().map(|(def, ..)| def).map_or_else( || TraitEnvironment::empty(self.resolver.krate()), |def| db.trait_environment_for_body(def), @@ -246,7 +254,7 @@ impl<'db> SourceAnalyzer<'db> { if let Pat::Bind { id, .. } = self.store()?[pat_id.as_pat()?] { Some(id) } else { None } } - pub(crate) fn expr_adjustments(&self, expr: &ast::Expr) -> Option<&[Adjustment]> { + pub(crate) fn expr_adjustments(&self, expr: &ast::Expr) -> Option<&[Adjustment<'db>]> { // It is safe to omit destructuring assignments here because they have no adjustments (neither // expressions nor patterns). let expr_id = self.expr_id(expr.clone())?.as_expr()?; @@ -259,8 +267,11 @@ impl<'db> SourceAnalyzer<'db> { db: &'db dyn HirDatabase, ty: &ast::Type, ) -> Option<Type<'db>> { + let interner = DbInterner::new_no_crate(db); + let type_ref = self.type_id(ty)?; - let ty = TyLoweringContext::new( + + let mut ty = TyLoweringContext::new( db, &self.resolver, self.store()?, @@ -271,6 +282,31 @@ impl<'db> SourceAnalyzer<'db> { LifetimeElisionKind::Infer, ) .lower_ty(type_ref); + + // Try and substitute unknown types using InferenceResult + if let Some(infer) = self.infer() + && let Some(store) = self.store() + { + let mut inferred_types = vec![]; + TypeRef::walk(type_ref, store, &mut |type_ref_id, type_ref| { + if matches!(type_ref, TypeRef::Placeholder) { + inferred_types.push(infer.type_of_type_placeholder(type_ref_id)); + } + }); + let mut inferred_types = inferred_types.into_iter(); + + let substituted_ty = hir_ty::next_solver::fold::fold_tys(interner, ty, |ty| { + if ty.is_ty_error() { inferred_types.next().flatten().unwrap_or(ty) } else { ty } + }); + + // Only used the result if the placeholder and unknown type counts matched + let success = + inferred_types.next().is_none() && !substituted_ty.references_non_lt_error(); + if success { + ty = substituted_ty; + } + } + Some(Type::new_with_resolver(db, &self.resolver, ty)) } @@ -284,9 +320,9 @@ impl<'db> SourceAnalyzer<'db> { let coerced = expr_id .as_expr() .and_then(|expr_id| infer.expr_adjustment(expr_id)) - .and_then(|adjusts| adjusts.last().map(|adjust| adjust.target.clone())); - let ty = infer[expr_id].clone(); - let mk_ty = |ty| Type::new_with_resolver(db, &self.resolver, ty); + .and_then(|adjusts| adjusts.last().map(|adjust| adjust.target)); + let ty = infer[expr_id]; + let mk_ty = |ty: Ty<'db>| Type::new_with_resolver(db, &self.resolver, ty); Some((mk_ty(ty), coerced.map(mk_ty))) } @@ -307,8 +343,8 @@ impl<'db> SourceAnalyzer<'db> { } }; - let ty = infer[expr_or_pat_id].clone(); - let mk_ty = |ty| Type::new_with_resolver(db, &self.resolver, ty); + let ty = infer[expr_or_pat_id]; + let mk_ty = |ty: Ty<'db>| Type::new_with_resolver(db, &self.resolver, ty); Some((mk_ty(ty), coerced.map(mk_ty))) } @@ -319,8 +355,8 @@ impl<'db> SourceAnalyzer<'db> { ) -> Option<Type<'db>> { let binding_id = self.binding_id_of_pat(pat)?; let infer = self.infer()?; - let ty = infer[binding_id].clone(); - let mk_ty = |ty| Type::new_with_resolver(db, &self.resolver, ty); + let ty = infer[binding_id]; + let mk_ty = |ty: Ty<'db>| Type::new_with_resolver(db, &self.resolver, ty); Some(mk_ty(ty)) } @@ -330,7 +366,7 @@ impl<'db> SourceAnalyzer<'db> { _param: &ast::SelfParam, ) -> Option<Type<'db>> { let binding = self.body()?.self_param?; - let ty = self.infer()?[binding].clone(); + let ty = self.infer()?[binding]; Some(Type::new_with_resolver(db, &self.resolver, ty)) } @@ -343,8 +379,10 @@ impl<'db> SourceAnalyzer<'db> { let infer = self.infer()?; infer.binding_mode(id.as_pat()?).map(|bm| match bm { hir_ty::BindingMode::Move => BindingMode::Move, - hir_ty::BindingMode::Ref(hir_ty::Mutability::Mut) => BindingMode::Ref(Mutability::Mut), - hir_ty::BindingMode::Ref(hir_ty::Mutability::Not) => { + hir_ty::BindingMode::Ref(hir_ty::next_solver::Mutability::Mut) => { + BindingMode::Ref(Mutability::Mut) + } + hir_ty::BindingMode::Ref(hir_ty::next_solver::Mutability::Not) => { BindingMode::Ref(Mutability::Shared) } }) @@ -360,7 +398,7 @@ impl<'db> SourceAnalyzer<'db> { infer .pat_adjustment(pat_id.as_pat()?)? .iter() - .map(|ty| Type::new_with_resolver(db, &self.resolver, ty.clone())) + .map(|ty| Type::new_with_resolver(db, &self.resolver, *ty)) .collect(), ) } @@ -371,8 +409,9 @@ impl<'db> SourceAnalyzer<'db> { call: &ast::MethodCallExpr, ) -> Option<Callable<'db>> { let expr_id = self.expr_id(call.clone().into())?.as_expr()?; - let (func, substs) = self.infer()?.method_resolution(expr_id)?; - let ty = db.value_ty(func.into())?.substitute(Interner, &substs); + let (func, args) = self.infer()?.method_resolution(expr_id)?; + let interner = DbInterner::new_no_crate(db); + let ty = db.value_ty(func.into())?.instantiate(interner, args); let ty = Type::new_with_resolver(db, &self.resolver, ty); let mut res = ty.as_callable(db)?; res.is_bound_method = true; @@ -437,17 +476,13 @@ impl<'db> SourceAnalyzer<'db> { fn field_subst( &self, field_expr: ExprId, - infer: &InferenceResult, + infer: &InferenceResult<'db>, db: &'db dyn HirDatabase, ) -> Option<GenericSubstitution<'db>> { let body = self.store()?; if let Expr::Field { expr: object_expr, name: _ } = body[field_expr] { let (adt, subst) = infer.type_of_expr_with_adjust(object_expr)?.as_adt()?; - return Some(GenericSubstitution::new( - adt.into(), - subst.clone(), - self.trait_environment(db), - )); + return Some(GenericSubstitution::new(adt.into(), subst, self.trait_environment(db))); } None } @@ -530,7 +565,7 @@ impl<'db> SourceAnalyzer<'db> { db: &'db dyn HirDatabase, await_expr: &ast::AwaitExpr, ) -> Option<FunctionId> { - let mut ty = self.ty_of_expr(await_expr.expr()?)?.clone(); + let mut ty = self.ty_of_expr(await_expr.expr()?)?; let into_future_trait = self .resolver @@ -538,7 +573,7 @@ impl<'db> SourceAnalyzer<'db> { .map(Trait::from); if let Some(into_future_trait) = into_future_trait { - let type_ = Type::new_with_resolver(db, &self.resolver, ty.clone()); + let type_ = Type::new_with_resolver(db, &self.resolver, ty); if type_.impls_trait(db, into_future_trait, &[]) { let items = into_future_trait.items(db); let into_future_type = items.into_iter().find_map(|item| match item { @@ -554,11 +589,11 @@ impl<'db> SourceAnalyzer<'db> { } } - let future_trait = LangItem::Future.resolve_trait(db, self.resolver.krate())?; - let poll_fn = LangItem::FuturePoll.resolve_function(db, self.resolver.krate())?; + let poll_fn = self.lang_items(db).FuturePoll?; // HACK: subst for `poll()` coincides with that for `Future` because `poll()` itself // doesn't have any generic parameters, so we skip building another subst for `poll()`. - let substs = hir_ty::TyBuilder::subst_for_def(db, future_trait, None).push(ty).build(); + let interner = DbInterner::new_no_crate(db); + let substs = GenericArgs::new_from_iter(interner, [ty.into()]); Some(self.resolve_impl_method_or_trait_def(db, poll_fn, substs)) } @@ -567,20 +602,23 @@ impl<'db> SourceAnalyzer<'db> { db: &'db dyn HirDatabase, prefix_expr: &ast::PrefixExpr, ) -> Option<FunctionId> { - let (op_trait, op_fn) = match prefix_expr.op_kind()? { + let (_op_trait, op_fn) = match prefix_expr.op_kind()? { ast::UnaryOp::Deref => { // This can be either `Deref::deref` or `DerefMut::deref_mut`. // Since deref kind is inferenced and stored in `InferenceResult.method_resolution`, // use that result to find out which one it is. - let (deref_trait, deref) = - self.lang_trait_fn(db, LangItem::Deref, &Name::new_symbol_root(sym::deref))?; + let (deref_trait, deref) = self.lang_trait_fn( + db, + self.lang_items(db).Deref, + &Name::new_symbol_root(sym::deref), + )?; self.infer() .and_then(|infer| { let expr = self.expr_id(prefix_expr.clone().into())?.as_expr()?; let (func, _) = infer.method_resolution(expr)?; let (deref_mut_trait, deref_mut) = self.lang_trait_fn( db, - LangItem::DerefMut, + self.lang_items(db).DerefMut, &Name::new_symbol_root(sym::deref_mut), )?; if func == deref_mut { Some((deref_mut_trait, deref_mut)) } else { None } @@ -588,18 +626,19 @@ impl<'db> SourceAnalyzer<'db> { .unwrap_or((deref_trait, deref)) } ast::UnaryOp::Not => { - self.lang_trait_fn(db, LangItem::Not, &Name::new_symbol_root(sym::not))? + self.lang_trait_fn(db, self.lang_items(db).Not, &Name::new_symbol_root(sym::not))? } ast::UnaryOp::Neg => { - self.lang_trait_fn(db, LangItem::Neg, &Name::new_symbol_root(sym::neg))? + self.lang_trait_fn(db, self.lang_items(db).Neg, &Name::new_symbol_root(sym::neg))? } }; let ty = self.ty_of_expr(prefix_expr.expr()?)?; + let interner = DbInterner::new_no_crate(db); // HACK: subst for all methods coincides with that for their trait because the methods // don't have any generic parameters, so we skip building another subst for the methods. - let substs = hir_ty::TyBuilder::subst_for_def(db, op_trait, None).push(ty.clone()).build(); + let substs = GenericArgs::new_from_iter(interner, [ty.into()]); Some(self.resolve_impl_method_or_trait_def(db, op_fn, substs)) } @@ -612,27 +651,25 @@ impl<'db> SourceAnalyzer<'db> { let base_ty = self.ty_of_expr(index_expr.base()?)?; let index_ty = self.ty_of_expr(index_expr.index()?)?; - let (index_trait, index_fn) = - self.lang_trait_fn(db, LangItem::Index, &Name::new_symbol_root(sym::index))?; - let (op_trait, op_fn) = self + let (_index_trait, index_fn) = + self.lang_trait_fn(db, self.lang_items(db).Index, &Name::new_symbol_root(sym::index))?; + let op_fn = self .infer() .and_then(|infer| { let expr = self.expr_id(index_expr.clone().into())?.as_expr()?; let (func, _) = infer.method_resolution(expr)?; - let (index_mut_trait, index_mut_fn) = self.lang_trait_fn( + let (_index_mut_trait, index_mut_fn) = self.lang_trait_fn( db, - LangItem::IndexMut, + self.lang_items(db).IndexMut, &Name::new_symbol_root(sym::index_mut), )?; - if func == index_mut_fn { Some((index_mut_trait, index_mut_fn)) } else { None } + if func == index_mut_fn { Some(index_mut_fn) } else { None } }) - .unwrap_or((index_trait, index_fn)); + .unwrap_or(index_fn); // HACK: subst for all methods coincides with that for their trait because the methods // don't have any generic parameters, so we skip building another subst for the methods. - let substs = hir_ty::TyBuilder::subst_for_def(db, op_trait, None) - .push(base_ty.clone()) - .push(index_ty.clone()) - .build(); + let interner = DbInterner::new_no_crate(db); + let substs = GenericArgs::new_from_iter(interner, [base_ty.into(), index_ty.into()]); Some(self.resolve_impl_method_or_trait_def(db, op_fn, substs)) } @@ -645,14 +682,14 @@ impl<'db> SourceAnalyzer<'db> { let lhs = self.ty_of_expr(binop_expr.lhs()?)?; let rhs = self.ty_of_expr(binop_expr.rhs()?)?; - let (op_trait, op_fn) = lang_items_for_bin_op(op) - .and_then(|(name, lang_item)| self.lang_trait_fn(db, lang_item, &name))?; + let (_op_trait, op_fn) = + lang_items_for_bin_op(self.lang_items(db), op).and_then(|(name, lang_item)| { + self.lang_trait_fn(db, lang_item, &Name::new_symbol_root(name)) + })?; // HACK: subst for `index()` coincides with that for `Index` because `index()` itself // doesn't have any generic parameters, so we skip building another subst for `index()`. - let substs = hir_ty::TyBuilder::subst_for_def(db, op_trait, None) - .push(lhs.clone()) - .push(rhs.clone()) - .build(); + let interner = DbInterner::new_no_crate(db); + let substs = GenericArgs::new_from_iter(interner, [lhs.into(), rhs.into()]); Some(self.resolve_impl_method_or_trait_def(db, op_fn, substs)) } @@ -664,14 +701,11 @@ impl<'db> SourceAnalyzer<'db> { ) -> Option<FunctionId> { let ty = self.ty_of_expr(try_expr.expr()?)?; - let op_fn = LangItem::TryTraitBranch.resolve_function(db, self.resolver.krate())?; - let op_trait = match op_fn.lookup(db).container { - ItemContainerId::TraitId(id) => id, - _ => return None, - }; + let op_fn = self.lang_items(db).TryTraitBranch?; // HACK: subst for `branch()` coincides with that for `Try` because `branch()` itself // doesn't have any generic parameters, so we skip building another subst for `branch()`. - let substs = hir_ty::TyBuilder::subst_for_def(db, op_trait, None).push(ty.clone()).build(); + let interner = DbInterner::new_no_crate(db); + let substs = GenericArgs::new_from_iter(interner, [ty.into()]); Some(self.resolve_impl_method_or_trait_def(db, op_fn, substs)) } @@ -684,6 +718,7 @@ impl<'db> SourceAnalyzer<'db> { let record_expr = ast::RecordExpr::cast(field.syntax().parent().and_then(|p| p.parent())?)?; let expr = ast::Expr::from(record_expr); let expr_id = self.store_sm()?.node_expr(InFile::new(self.file_id, &expr))?; + let interner = DbInterner::new_no_crate(db); let ast_name = field.field_name()?; let local_name = ast_name.as_name(); @@ -710,13 +745,12 @@ impl<'db> SourceAnalyzer<'db> { let variant = self.infer()?.variant_resolution_for_expr_or_pat(expr_id)?; let variant_data = variant.fields(db); let field = FieldId { parent: variant, local_id: variant_data.field(&local_name)? }; - let field_ty = - db.field_types(variant).get(field.local_id)?.clone().substitute(Interner, subst); + let field_ty = (*db.field_types(variant).get(field.local_id)?).instantiate(interner, subst); Some(( field.into(), local, Type::new_with_resolver(db, &self.resolver, field_ty), - GenericSubstitution::new(adt.into(), subst.clone(), self.trait_environment(db)), + GenericSubstitution::new(adt.into(), subst, self.trait_environment(db)), )) } @@ -725,6 +759,7 @@ impl<'db> SourceAnalyzer<'db> { db: &'db dyn HirDatabase, field: &ast::RecordPatField, ) -> Option<(Field, Type<'db>, GenericSubstitution<'db>)> { + let interner = DbInterner::new_no_crate(db); let field_name = field.field_name()?.as_name(); let record_pat = ast::RecordPat::cast(field.syntax().parent().and_then(|p| p.parent())?)?; let pat_id = self.pat_id(&record_pat.into())?; @@ -732,12 +767,11 @@ impl<'db> SourceAnalyzer<'db> { let variant_data = variant.fields(db); let field = FieldId { parent: variant, local_id: variant_data.field(&field_name)? }; let (adt, subst) = self.infer()?[pat_id.as_pat()?].as_adt()?; - let field_ty = - db.field_types(variant).get(field.local_id)?.clone().substitute(Interner, subst); + let field_ty = (*db.field_types(variant).get(field.local_id)?).instantiate(interner, subst); Some(( field.into(), Type::new_with_resolver(db, &self.resolver, field_ty), - GenericSubstitution::new(adt.into(), subst.clone(), self.trait_environment(db)), + GenericSubstitution::new(adt.into(), subst, self.trait_environment(db)), )) } @@ -786,43 +820,40 @@ impl<'db> SourceAnalyzer<'db> { let container = self.type_of_type(db, &container)?; let trait_env = container.env; + + let interner = DbInterner::new_with(db, trait_env.krate); + let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis); + let mut container = Either::Right(container.ty); for field_name in offset_of_expr.fields() { - if let Some( - TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty_id, substitution })) - | TyKind::AssociatedType(associated_ty_id, substitution), - ) = container.as_ref().right().map(|it| it.kind(Interner)) - { - let projection = ProjectionTy { - associated_ty_id: *associated_ty_id, - substitution: substitution.clone(), - }; - container = Either::Right(db.normalize_projection(projection, trait_env.clone())); + if let Either::Right(container) = &mut container { + *container = structurally_normalize_ty(&infcx, *container, trait_env.clone()); } - let handle_variants = |variant: VariantId, subst: &Substitution, container: &mut _| { - let fields = variant.fields(db); - let field = fields.field(&field_name.as_name())?; - let field_types = db.field_types(variant); - *container = Either::Right(field_types[field].clone().substitute(Interner, subst)); - let generic_def = match variant { - VariantId::EnumVariantId(it) => it.loc(db).parent.into(), - VariantId::StructId(it) => it.into(), - VariantId::UnionId(it) => it.into(), + let handle_variants = + |variant: VariantId, subst: GenericArgs<'db>, container: &mut _| { + let fields = variant.fields(db); + let field = fields.field(&field_name.as_name())?; + let field_types = db.field_types(variant); + *container = Either::Right(field_types[field].instantiate(interner, subst)); + let generic_def = match variant { + VariantId::EnumVariantId(it) => it.loc(db).parent.into(), + VariantId::StructId(it) => it.into(), + VariantId::UnionId(it) => it.into(), + }; + Some(( + Either::Right(Field { parent: variant.into(), id: field }), + generic_def, + subst, + )) }; - Some(( - Either::Right(Field { parent: variant.into(), id: field }), - generic_def, - subst.clone(), - )) - }; - let temp_ty = TyKind::Error.intern(Interner); + let temp_ty = Ty::new_error(interner, ErrorGuaranteed); let (field_def, generic_def, subst) = - match std::mem::replace(&mut container, Either::Right(temp_ty.clone())) { + match std::mem::replace(&mut container, Either::Right(temp_ty)) { Either::Left((variant_id, subst)) => { - handle_variants(VariantId::from(variant_id), &subst, &mut container)? + handle_variants(VariantId::from(variant_id), subst, &mut container)? } - Either::Right(container_ty) => match container_ty.kind(Interner) { - TyKind::Adt(adt_id, subst) => match adt_id.0 { + Either::Right(container_ty) => match container_ty.kind() { + TyKind::Adt(adt_def, subst) => match adt_def.def_id().0 { AdtId::StructId(id) => { handle_variants(id.into(), subst, &mut container)? } @@ -832,8 +863,8 @@ impl<'db> SourceAnalyzer<'db> { AdtId::EnumId(id) => { let variants = id.enum_variants(db); let variant = variants.variant(&field_name.as_name())?; - container = Either::Left((variant, subst.clone())); - (Either::Left(Variant { id: variant }), id.into(), subst.clone()) + container = Either::Left((variant, subst)); + (Either::Left(Variant { id: variant }), id.into(), subst) } }, _ => return None, @@ -863,7 +894,7 @@ impl<'db> SourceAnalyzer<'db> { let expr_id = self.expr_id(path_expr.into())?; if let Some((assoc, subs)) = infer.assoc_resolutions_for_expr_or_pat(expr_id) { let (assoc, subst) = match assoc { - AssocItemId::FunctionId(f_in_trait) => { + CandidateId::FunctionId(f_in_trait) => { match infer.type_of_expr_or_pat(expr_id) { None => { let subst = GenericSubstitution::new( @@ -871,15 +902,13 @@ impl<'db> SourceAnalyzer<'db> { subs, self.trait_environment(db), ); - (assoc, subst) + (AssocItemId::from(f_in_trait), subst) } Some(func_ty) => { - if let TyKind::FnDef(_fn_def, subs) = func_ty.kind(Interner) { + if let TyKind::FnDef(_fn_def, subs) = func_ty.kind() { let (fn_, subst) = self .resolve_impl_method_or_trait_def_with_subst( - db, - f_in_trait, - subs.clone(), + db, f_in_trait, subs, ); let subst = GenericSubstitution::new( fn_.into(), @@ -893,12 +922,12 @@ impl<'db> SourceAnalyzer<'db> { subs, self.trait_environment(db), ); - (assoc, subst) + (f_in_trait.into(), subst) } } } } - AssocItemId::ConstId(const_id) => { + CandidateId::ConstId(const_id) => { let (konst, subst) = self.resolve_impl_const_or_trait_def_with_subst(db, const_id, subs); let subst = GenericSubstitution::new( @@ -908,14 +937,6 @@ impl<'db> SourceAnalyzer<'db> { ); (konst.into(), subst) } - AssocItemId::TypeAliasId(type_alias) => ( - assoc, - GenericSubstitution::new( - type_alias.into(), - subs, - self.trait_environment(db), - ), - ), }; return Some((PathResolution::Def(AssocItem::from(assoc).into()), Some(subst))); @@ -931,7 +952,7 @@ impl<'db> SourceAnalyzer<'db> { if let Some((assoc, subs)) = infer.assoc_resolutions_for_expr_or_pat(expr_or_pat_id) { let (assoc, subst) = match assoc { - AssocItemId::ConstId(const_id) => { + CandidateId::ConstId(const_id) => { let (konst, subst) = self.resolve_impl_const_or_trait_def_with_subst(db, const_id, subs); let subst = GenericSubstitution::new( @@ -939,12 +960,12 @@ impl<'db> SourceAnalyzer<'db> { subst, self.trait_environment(db), ); - (konst.into(), subst) + (AssocItemId::from(konst), subst) } - assoc => ( - assoc, + CandidateId::FunctionId(function_id) => ( + function_id.into(), GenericSubstitution::new( - assoc.into(), + function_id.into(), subs, self.trait_environment(db), ), @@ -1146,33 +1167,35 @@ impl<'db> SourceAnalyzer<'db> { self.infer()?.type_of_expr_or_pat(expr_id)? } else if let Some(pat) = ast::Pat::cast(parent) { let pat_id = self.pat_id(&pat)?; - &self.infer()?[pat_id] + self.infer()?[pat_id] } else { return None; }; let env = self.trait_environment(db); - let (subst, expected_resolution) = match ty.kind(Interner) { - TyKind::Adt(adt_id, subst) => ( - GenericSubstitution::new(adt_id.0.into(), subst.clone(), env), - PathResolution::Def(ModuleDef::Adt(adt_id.0.into())), - ), - TyKind::AssociatedType(assoc_id, subst) => { - let assoc_id = from_assoc_type_id(*assoc_id); + let (subst, expected_resolution) = match ty.kind() { + TyKind::Adt(adt_def, subst) => { + let adt_id = adt_def.def_id().0; ( - GenericSubstitution::new(assoc_id.into(), subst.clone(), env), + GenericSubstitution::new(adt_id.into(), subst, env), + PathResolution::Def(ModuleDef::Adt(adt_id.into())), + ) + } + TyKind::Alias(AliasTyKind::Projection, alias) => { + let assoc_id = alias.def_id.expect_type_alias(); + ( + GenericSubstitution::new(assoc_id.into(), alias.args, env), PathResolution::Def(ModuleDef::TypeAlias(assoc_id.into())), ) } TyKind::FnDef(fn_id, subst) => { - let fn_id = ToChalk::from_chalk(db, *fn_id); - let generic_def_id = match fn_id { + let generic_def_id = match fn_id.0 { CallableDefId::StructId(id) => id.into(), CallableDefId::FunctionId(id) => id.into(), CallableDefId::EnumVariantId(_) => return None, }; ( - GenericSubstitution::new(generic_def_id, subst.clone(), env), - PathResolution::Def(ModuleDefId::from(fn_id).into()), + GenericSubstitution::new(generic_def_id, subst, env), + PathResolution::Def(ModuleDefId::from(fn_id.0).into()), ) } _ => return None, @@ -1250,17 +1273,18 @@ impl<'db> SourceAnalyzer<'db> { fn missing_fields( &self, db: &'db dyn HirDatabase, - substs: &Substitution, + substs: GenericArgs<'db>, variant: VariantId, missing_fields: Vec<LocalFieldId>, ) -> Vec<(Field, Type<'db>)> { + let interner = DbInterner::new_no_crate(db); let field_types = db.field_types(variant); missing_fields .into_iter() .map(|local_id| { let field = FieldId { parent: variant, local_id }; - let ty = field_types[local_id].clone().substitute(Interner, substs); + let ty = field_types[local_id].instantiate(interner, substs); (field.into(), Type::new_with_resolver_inner(db, &self.resolver, ty)) }) .collect() @@ -1373,7 +1397,7 @@ impl<'db> SourceAnalyzer<'db> { &self, db: &'db dyn HirDatabase, func: FunctionId, - substs: Substitution, + substs: GenericArgs<'db>, ) -> FunctionId { self.resolve_impl_method_or_trait_def_with_subst(db, func, substs).0 } @@ -1382,8 +1406,8 @@ impl<'db> SourceAnalyzer<'db> { &self, db: &'db dyn HirDatabase, func: FunctionId, - substs: Substitution, - ) -> (FunctionId, Substitution) { + substs: GenericArgs<'db>, + ) -> (FunctionId, GenericArgs<'db>) { let owner = match self.resolver.body_owner() { Some(it) => it, None => return (func, substs), @@ -1396,28 +1420,34 @@ impl<'db> SourceAnalyzer<'db> { &self, db: &'db dyn HirDatabase, const_id: ConstId, - subs: Substitution, - ) -> (ConstId, Substitution) { + subs: GenericArgs<'db>, + ) -> (ConstId, GenericArgs<'db>) { let owner = match self.resolver.body_owner() { Some(it) => it, None => return (const_id, subs), }; let env = db.trait_environment_for_body(owner); - method_resolution::lookup_impl_const(db, env, const_id, subs) + let interner = DbInterner::new_with(db, env.krate); + let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis); + method_resolution::lookup_impl_const(&infcx, env, const_id, subs) + } + + fn lang_items<'a>(&self, db: &'a dyn HirDatabase) -> &'a LangItems { + hir_def::lang_item::lang_items(db, self.resolver.krate()) } fn lang_trait_fn( &self, db: &'db dyn HirDatabase, - lang_trait: LangItem, + lang_trait: Option<TraitId>, method_name: &Name, ) -> Option<(TraitId, FunctionId)> { - let trait_id = lang_trait.resolve_trait(db, self.resolver.krate())?; + let trait_id = lang_trait?; let fn_id = trait_id.trait_items(db).method_by_name(method_name)?; Some((trait_id, fn_id)) } - fn ty_of_expr(&self, expr: ast::Expr) -> Option<&Ty> { + fn ty_of_expr(&self, expr: ast::Expr) -> Option<Ty<'db>> { self.infer()?.type_of_expr_or_pat(self.expr_id(expr)?) } } @@ -1593,14 +1623,12 @@ fn resolve_hir_path_( Some(unresolved) => resolver .generic_def() .and_then(|def| { - salsa::attach(db, || { - hir_ty::associated_type_shorthand_candidates( - db, - def, - res.in_type_ns()?, - |name, _| name == unresolved.name, - ) - }) + hir_ty::associated_type_shorthand_candidates( + db, + def, + res.in_type_ns()?, + |name, _| name == unresolved.name, + ) }) .map(TypeAlias::from) .map(Into::into) |