Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir/src/lib.rs')
| -rw-r--r-- | crates/hir/src/lib.rs | 1804 |
1 files changed, 908 insertions, 896 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 4ddb04b24f..2bb2f80ecc 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -20,6 +20,8 @@ #![cfg_attr(feature = "in-rust-tree", feature(rustc_private))] #![recursion_limit = "512"] +extern crate ra_ap_rustc_type_ir as rustc_type_ir; + mod attrs; mod from_id; mod has_source; @@ -34,6 +36,7 @@ pub mod term_search; mod display; use std::{ + fmt, mem::discriminant, ops::{ControlFlow, Not}, }; @@ -46,7 +49,7 @@ use hir_def::{ CrateRootModuleId, DefWithBodyId, EnumId, EnumVariantId, ExternBlockId, ExternCrateId, FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, ItemContainerId, LifetimeParamId, LocalFieldId, Lookup, MacroExpander, MacroId, ModuleId, StaticId, StructId, SyntheticSyntax, - TraitAliasId, TupleId, TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId, + TupleId, TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId, expr_store::{ExpressionStoreDiagnostics, ExpressionStoreSourceMap}, hir::{ BindingAnnotation, BindingId, Expr, ExprId, ExprOrPatId, LabelId, Pat, @@ -54,10 +57,13 @@ use hir_def::{ }, item_tree::ImportAlias, layout::{self, ReprOptions, TargetDataLayout}, - nameres::{self, assoc::TraitItems, diagnostics::DefDiagnostic}, + nameres::{ + assoc::TraitItems, + diagnostics::{DefDiagnostic, DefDiagnosticKind}, + }, per_ns::PerNs, resolver::{HasResolver, Resolver}, - signatures::{ImplFlags, StaticFlags, TraitFlags, VariantFields}, + signatures::{ImplFlags, StaticFlags, StructFlags, TraitFlags, VariantFields}, src::HasSource as _, visibility::visibility_from_ast, }; @@ -66,25 +72,31 @@ use hir_expand::{ proc_macro::ProcMacroKind, }; use hir_ty::{ - AliasTy, CallableSig, Canonical, CanonicalVarKinds, Cast, ClosureId, GenericArg, - GenericArgData, Interner, ParamKind, QuantifiedWhereClause, Scalar, Substitution, - TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, TyLoweringDiagnostic, - ValueTyDefId, WhereClause, all_super_traits, autoderef, check_orphan_rules, - consteval::{ConstExt, try_const_usize, unknown_const_as_generic}, + TraitEnvironment, TyDefId, TyLoweringDiagnostic, ValueTyDefId, all_super_traits, autoderef, + check_orphan_rules, + consteval::try_const_usize, + db::{InternedClosureId, InternedCoroutineId}, diagnostics::BodyValidationDiagnostic, - direct_super_traits, error_lifetime, known_const_to_ast, + direct_super_traits, known_const_to_ast, layout::{Layout as TyLayout, RustcEnumVariantIdx, RustcFieldIdx, TagEncoding}, method_resolution, mir::{MutBorrowKind, interpret_mir}, - primitive::UintTy, - traits::FnTrait, + next_solver::{ + AliasTy, Canonical, ClauseKind, ConstKind, DbInterner, ErrorGuaranteed, GenericArg, + GenericArgs, PolyFnSig, Region, SolverDefId, Ty, TyKind, TypingMode, + infer::{DbInternerInferExt, InferCtxt}, + }, + traits::{self, FnTrait, structurally_normalize_ty}, }; use itertools::Itertools; -use nameres::diagnostics::DefDiagnosticKind; use rustc_hash::FxHashSet; +use rustc_type_ir::{ + AliasTyKind, TypeSuperVisitable, TypeVisitable, TypeVisitor, + inherent::{AdtDef, GenericArgs as _, IntoKind, SliceLike, Term as _, Ty as _}, +}; use smallvec::SmallVec; use span::{AstIdNode, Edition, FileId}; -use stdx::{format_to, impl_from, never, variance::PhantomCovariantLifetime}; +use stdx::{format_to, impl_from, never}; use syntax::{ AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, T, TextRange, ToSmolStr, ast::{self, HasAttrs as _, HasName, HasVisibility as _}, @@ -117,7 +129,7 @@ pub use { cfg::{CfgAtom, CfgExpr, CfgOptions}, hir_def::{ Complete, - ImportPathConfig, + FindPathConfig, attr::{AttrSourceMap, Attrs, AttrsWithOwner}, find_path::PrefixKind, import_map, @@ -146,18 +158,21 @@ pub use { proc_macro::{ProcMacros, ProcMacrosBuilder}, tt, }, + // FIXME: Properly encapsulate mir + hir_ty::mir, hir_ty::{ - CastError, DropGlue, FnAbi, PointerCast, Safety, Variance, + CastError, FnAbi, PointerCast, attach_db, attach_db_allow_change, consteval::ConstEvalError, diagnostics::UnsafetyReason, display::{ClosureStyle, DisplayTarget, HirDisplay, HirDisplayError, HirWrite}, + drop::DropGlue, dyn_compatibility::{DynCompatibilityViolation, MethodViolationCode}, layout::LayoutError, method_resolution::TyFingerprint, mir::{MirEvalError, MirLowerError}, + next_solver::abi::Safety, + next_solver::clear_tls_solver_cache, }, - // FIXME: Properly encapsulate mir - hir_ty::{Interner as ChalkTyInterner, mir}, intern::{Symbol, sym}, }; @@ -187,6 +202,10 @@ pub struct CrateDependency { } impl Crate { + pub fn base(self) -> base_db::Crate { + self.id + } + pub fn origin(self, db: &dyn HirDatabase) -> CrateOrigin { self.id.data(db).origin.clone() } @@ -320,7 +339,6 @@ pub enum ModuleDef { Const(Const), Static(Static), Trait(Trait), - TraitAlias(TraitAlias), TypeAlias(TypeAlias), BuiltinType(BuiltinType), Macro(Macro), @@ -333,7 +351,6 @@ impl_from!( Const, Static, Trait, - TraitAlias, TypeAlias, BuiltinType, Macro @@ -360,7 +377,6 @@ impl ModuleDef { ModuleDef::Const(it) => Some(it.module(db)), ModuleDef::Static(it) => Some(it.module(db)), ModuleDef::Trait(it) => Some(it.module(db)), - ModuleDef::TraitAlias(it) => Some(it.module(db)), ModuleDef::TypeAlias(it) => Some(it.module(db)), ModuleDef::Macro(it) => Some(it.module(db)), ModuleDef::BuiltinType(_) => None, @@ -389,7 +405,6 @@ impl ModuleDef { ModuleDef::Const(it) => it.name(db)?, ModuleDef::Adt(it) => it.name(db), ModuleDef::Trait(it) => it.name(db), - ModuleDef::TraitAlias(it) => it.name(db), ModuleDef::Function(it) => it.name(db), ModuleDef::Variant(it) => it.name(db), ModuleDef::TypeAlias(it) => it.name(db), @@ -412,7 +427,6 @@ impl ModuleDef { Adt::Union(it) => it.id.into(), }, ModuleDef::Trait(it) => it.id.into(), - ModuleDef::TraitAlias(it) => it.id.into(), ModuleDef::Function(it) => it.id.into(), ModuleDef::TypeAlias(it) => it.id.into(), ModuleDef::Module(it) => it.id.into(), @@ -452,7 +466,6 @@ impl ModuleDef { ModuleDef::Module(_) | ModuleDef::Adt(_) | ModuleDef::Trait(_) - | ModuleDef::TraitAlias(_) | ModuleDef::TypeAlias(_) | ModuleDef::Macro(_) | ModuleDef::BuiltinType(_) => None, @@ -465,7 +478,6 @@ impl ModuleDef { ModuleDef::Function(it) => Some(it.into()), ModuleDef::Adt(it) => Some(it.into()), ModuleDef::Trait(it) => Some(it.into()), - ModuleDef::TraitAlias(it) => Some(it.into()), ModuleDef::TypeAlias(it) => Some(it.into()), ModuleDef::Module(_) | ModuleDef::Variant(_) @@ -485,7 +497,6 @@ impl ModuleDef { ModuleDef::Const(it) => it.attrs(db), ModuleDef::Static(it) => it.attrs(db), ModuleDef::Trait(it) => it.attrs(db), - ModuleDef::TraitAlias(it) => it.attrs(db), ModuleDef::TypeAlias(it) => it.attrs(db), ModuleDef::Macro(it) => it.attrs(db), ModuleDef::BuiltinType(_) => return None, @@ -511,7 +522,6 @@ impl HasVisibility for ModuleDef { ModuleDef::Const(it) => it.visibility(db), ModuleDef::Static(it) => it.visibility(db), ModuleDef::Trait(it) => it.visibility(db), - ModuleDef::TraitAlias(it) => it.visibility(db), ModuleDef::TypeAlias(it) => it.visibility(db), ModuleDef::Variant(it) => it.visibility(db), ModuleDef::Macro(it) => it.visibility(db), @@ -743,12 +753,15 @@ impl Module { let inherent_impls = db.inherent_impls_in_crate(self.id.krate()); + let interner = DbInterner::new_with(db, Some(self.id.krate()), self.id.containing_block()); + let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis()); + let mut impl_assoc_items_scratch = vec![]; for impl_def in self.impl_defs(db) { GenericDef::Impl(impl_def).diagnostics(db, acc); let loc = impl_def.id.lookup(db); - let source_map = db.impl_signature_with_source_map(impl_def.id).1; + let (impl_signature, source_map) = db.impl_signature_with_source_map(impl_def.id); expr_store_diagnostics(db, acc, &source_map); let file_id = loc.id.file_id; @@ -777,10 +790,17 @@ impl Module { } let trait_ = impl_def.trait_(db); - let trait_is_unsafe = trait_.is_some_and(|t| t.is_unsafe(db)); + let mut trait_is_unsafe = trait_.is_some_and(|t| t.is_unsafe(db)); let impl_is_negative = impl_def.is_negative(db); let impl_is_unsafe = impl_def.is_unsafe(db); + let trait_is_unresolved = trait_.is_none() && impl_signature.target_trait.is_some(); + if trait_is_unresolved { + // Ignore trait safety errors when the trait is unresolved, as otherwise we'll treat it as safe, + // which may not be correct. + trait_is_unsafe = impl_is_unsafe; + } + let drop_maybe_dangle = (|| { // FIXME: This can be simplified a lot by exposing hir-ty's utils.rs::Generics helper let trait_ = trait_?; @@ -860,23 +880,15 @@ impl Module { .collect(); if !missing.is_empty() { - let self_ty = db.impl_self_ty(impl_def.id).substitute( - Interner, - &hir_ty::generics::generics(db, impl_def.id.into()).placeholder_subst(db), + let self_ty = db.impl_self_ty(impl_def.id).instantiate_identity(); + let self_ty = structurally_normalize_ty( + &infcx, + self_ty, + db.trait_environment(impl_def.id.into()), ); - let self_ty = if let TyKind::Alias(AliasTy::Projection(projection)) = - self_ty.kind(Interner) - { - db.normalize_projection( - projection.clone(), - db.trait_environment(impl_def.id.into()), - ) - } else { - self_ty - }; let self_ty_is_guaranteed_unsized = matches!( - self_ty.kind(Interner), - TyKind::Dyn(..) | TyKind::Slice(..) | TyKind::Str + self_ty.kind(), + TyKind::Dynamic(..) | TyKind::Slice(..) | TyKind::Str ); if self_ty_is_guaranteed_unsized { missing.retain(|(_, assoc_item)| { @@ -949,7 +961,7 @@ impl Module { self, db: &dyn DefDatabase, item: impl Into<ItemInNs>, - cfg: ImportPathConfig, + cfg: FindPathConfig, ) -> Option<ModPath> { hir_def::find_path::find_path( db, @@ -968,7 +980,7 @@ impl Module { db: &dyn DefDatabase, item: impl Into<ItemInNs>, prefix_kind: PrefixKind, - cfg: ImportPathConfig, + cfg: FindPathConfig, ) -> Option<ModPath> { hir_def::find_path::find_path(db, item.into().into(), self.into(), prefix_kind, true, cfg) } @@ -1020,21 +1032,21 @@ fn emit_macro_def_diagnostics<'db>( m: Macro, ) { let id = db.macro_def(m.id); - if let hir_expand::db::TokenExpander::DeclarativeMacro(expander) = db.macro_expander(id) { - if let Some(e) = expander.mac.err() { - let Some(ast) = id.ast_id().left() else { - never!("declarative expander for non decl-macro: {:?}", e); - return; - }; - let krate = HasModule::krate(&m.id, db); - let edition = krate.data(db).edition; - emit_def_diagnostic_( - db, - acc, - &DefDiagnosticKind::MacroDefError { ast, message: e.to_string() }, - edition, - ); - } + if let hir_expand::db::TokenExpander::DeclarativeMacro(expander) = db.macro_expander(id) + && let Some(e) = expander.mac.err() + { + let Some(ast) = id.ast_id().left() else { + never!("declarative expander for non decl-macro: {:?}", e); + return; + }; + let krate = HasModule::krate(&m.id, db); + let edition = krate.data(db).edition; + emit_def_diagnostic_( + db, + acc, + &DefDiagnosticKind::MacroDefError { ast, message: e.to_string() }, + edition, + ); } } @@ -1247,6 +1259,25 @@ pub struct Field { pub(crate) id: LocalFieldId, } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct InstantiatedField<'db> { + pub(crate) inner: Field, + pub(crate) args: GenericArgs<'db>, +} + +impl<'db> InstantiatedField<'db> { + /// Returns the type as in the signature of the struct. + pub fn ty(&self, db: &'db dyn HirDatabase) -> TypeNs<'db> { + let krate = self.inner.krate(db); + let interner = DbInterner::new_with(db, Some(krate.base()), None); + + let var_id = self.inner.parent.into(); + let field = db.field_types(var_id)[self.inner.id]; + let ty = field.instantiate(interner, self.args); + TypeNs::new(db, var_id, ty) + } +} + #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)] pub struct TupleField { pub owner: DefWithBodyId, @@ -1260,19 +1291,15 @@ impl TupleField { } pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> Type<'db> { + let interner = DbInterner::new_with(db, None, None); let ty = db .infer(self.owner) .tuple_field_access_type(self.tuple) - .as_slice(Interner) + .as_slice() .get(self.index as usize) - .and_then(|arg| arg.ty(Interner)) - .cloned() - .unwrap_or_else(|| TyKind::Error.intern(Interner)); - Type { - env: db.trait_environment_for_body(self.owner), - ty, - _pd: PhantomCovariantLifetime::new(), - } + .copied() + .unwrap_or_else(|| Ty::new_error(interner, ErrorGuaranteed)); + Type { env: db.trait_environment_for_body(self.owner), ty } } } @@ -1320,19 +1347,12 @@ impl Field { u32::from(self.id.into_raw()) as usize } - /// Returns the type as in the signature of the struct (i.e., with - /// placeholder types for type parameters). Only use this in the context of - /// the field definition. - pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> Type<'db> { + /// Returns the type as in the signature of the struct. Only use this in the + /// context of the field definition. + pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> TypeNs<'db> { let var_id = self.parent.into(); - let generic_def_id: GenericDefId = match self.parent { - VariantDef::Struct(it) => it.id.into(), - VariantDef::Union(it) => it.id.into(), - VariantDef::Variant(it) => it.id.lookup(db).parent.into(), - }; - let substs = TyBuilder::placeholder_subst(db, generic_def_id); - let ty = db.field_types(var_id)[self.id].clone().substitute(Interner, &substs); - Type::new(db, var_id, ty) + let ty = db.field_types(var_id)[self.id].skip_binder(); + TypeNs::new(db, var_id, ty) } // FIXME: Find better API to also handle const generics @@ -1347,17 +1367,9 @@ impl Field { VariantDef::Union(it) => it.id.into(), VariantDef::Variant(it) => it.parent_enum(db).id.into(), }; - let mut generics = generics.map(|it| it.ty); - let substs = TyBuilder::subst_for_def(db, def_id, None) - .fill(|x| match x { - ParamKind::Type => { - generics.next().unwrap_or_else(|| TyKind::Error.intern(Interner)).cast(Interner) - } - ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()), - ParamKind::Lifetime => error_lifetime().cast(Interner), - }) - .build(); - let ty = db.field_types(var_id)[self.id].clone().substitute(Interner, &substs); + let interner = DbInterner::new_with(db, None, None); + let args = generic_args_from_tys(interner, def_id.into(), generics.map(|ty| ty.ty)); + let ty = db.field_types(var_id)[self.id].instantiate(interner, args); Type::new(db, var_id, ty) } @@ -1417,8 +1429,8 @@ impl Struct { Type::from_def(db, self.id) } - pub fn ty_placeholders(self, db: &dyn HirDatabase) -> Type<'_> { - Type::from_def_placeholders(db, self.id) + pub fn ty_params(self, db: &dyn HirDatabase) -> Type<'_> { + Type::from_def_params(db, self.id) } pub fn constructor_ty(self, db: &dyn HirDatabase) -> Type<'_> { @@ -1444,6 +1456,11 @@ impl Struct { pub fn is_unstable(self, db: &dyn HirDatabase) -> bool { db.attrs(self.id.into()).is_unstable() } + + pub fn instantiate_infer<'db>(self, infer_ctxt: &InferCtxt<'db>) -> InstantiatedStruct<'db> { + let args = infer_ctxt.fresh_args_for_item(self.id.into()); + InstantiatedStruct { inner: self, args } + } } impl HasVisibility for Struct { @@ -1455,6 +1472,35 @@ impl HasVisibility for Struct { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct InstantiatedStruct<'db> { + pub(crate) inner: Struct, + pub(crate) args: GenericArgs<'db>, +} + +impl<'db> InstantiatedStruct<'db> { + pub fn fields(self, db: &dyn HirDatabase) -> Vec<InstantiatedField<'db>> { + self.inner + .id + .fields(db) + .fields() + .iter() + .map(|(id, _)| InstantiatedField { + inner: Field { parent: self.inner.into(), id }, + args: self.args, + }) + .collect() + } + + pub fn ty(self, db: &'db dyn HirDatabase) -> TypeNs<'db> { + let krate = self.inner.krate(db); + let interner = DbInterner::new_with(db, Some(krate.base()), None); + + let ty = db.ty(self.inner.id.into()); + TypeNs::new(db, self.inner.id, ty.instantiate(interner, self.args)) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Union { pub(crate) id: UnionId, } @@ -1472,8 +1518,8 @@ impl Union { Type::from_def(db, self.id) } - pub fn ty_placeholders(self, db: &dyn HirDatabase) -> Type<'_> { - Type::from_def_placeholders(db, self.id) + pub fn ty_params(self, db: &dyn HirDatabase) -> Type<'_> { + Type::from_def_params(db, self.id) } pub fn constructor_ty(self, db: &dyn HirDatabase) -> Type<'_> { @@ -1539,40 +1585,43 @@ impl Enum { Type::from_def(db, self.id) } - pub fn ty_placeholders<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> { - Type::from_def_placeholders(db, self.id) + pub fn ty_params<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> { + Type::from_def_params(db, self.id) } /// The type of the enum variant bodies. pub fn variant_body_ty<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> { + let interner = DbInterner::new_with(db, None, None); Type::new_for_crate( self.id.lookup(db).container.krate(), - TyBuilder::builtin(match db.enum_signature(self.id).variant_body_type() { + match db.enum_signature(self.id).variant_body_type() { layout::IntegerType::Pointer(sign) => match sign { - true => hir_def::builtin_type::BuiltinType::Int( - hir_def::builtin_type::BuiltinInt::Isize, - ), - false => hir_def::builtin_type::BuiltinType::Uint( - hir_def::builtin_type::BuiltinUint::Usize, - ), + true => Ty::new_int(interner, rustc_type_ir::IntTy::Isize), + false => Ty::new_uint(interner, rustc_type_ir::UintTy::Usize), }, layout::IntegerType::Fixed(i, sign) => match sign { - true => hir_def::builtin_type::BuiltinType::Int(match i { - layout::Integer::I8 => hir_def::builtin_type::BuiltinInt::I8, - layout::Integer::I16 => hir_def::builtin_type::BuiltinInt::I16, - layout::Integer::I32 => hir_def::builtin_type::BuiltinInt::I32, - layout::Integer::I64 => hir_def::builtin_type::BuiltinInt::I64, - layout::Integer::I128 => hir_def::builtin_type::BuiltinInt::I128, - }), - false => hir_def::builtin_type::BuiltinType::Uint(match i { - layout::Integer::I8 => hir_def::builtin_type::BuiltinUint::U8, - layout::Integer::I16 => hir_def::builtin_type::BuiltinUint::U16, - layout::Integer::I32 => hir_def::builtin_type::BuiltinUint::U32, - layout::Integer::I64 => hir_def::builtin_type::BuiltinUint::U64, - layout::Integer::I128 => hir_def::builtin_type::BuiltinUint::U128, - }), + true => Ty::new_int( + interner, + match i { + layout::Integer::I8 => rustc_type_ir::IntTy::I8, + layout::Integer::I16 => rustc_type_ir::IntTy::I16, + layout::Integer::I32 => rustc_type_ir::IntTy::I32, + layout::Integer::I64 => rustc_type_ir::IntTy::I64, + layout::Integer::I128 => rustc_type_ir::IntTy::I128, + }, + ), + false => Ty::new_uint( + interner, + match i { + layout::Integer::I8 => rustc_type_ir::UintTy::U8, + layout::Integer::I16 => rustc_type_ir::UintTy::U16, + layout::Integer::I32 => rustc_type_ir::UintTy::U32, + layout::Integer::I64 => rustc_type_ir::UintTy::U64, + layout::Integer::I128 => rustc_type_ir::UintTy::U128, + }, + ), }, - }), + }, ) } @@ -1598,6 +1647,22 @@ impl HasVisibility for Enum { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct InstantiatedEnum<'db> { + pub(crate) inner: Enum, + pub(crate) args: GenericArgs<'db>, +} + +impl<'db> InstantiatedEnum<'db> { + pub fn ty(self, db: &'db dyn HirDatabase) -> TypeNs<'db> { + let krate = self.inner.krate(db); + let interner = DbInterner::new_with(db, Some(krate.base()), None); + + let ty = db.ty(self.inner.id.into()); + TypeNs::new(db, self.inner.id, ty.instantiate(interner, self.args)) + } +} + impl From<&Variant> for DefWithBodyId { fn from(&v: &Variant) -> Self { DefWithBodyId::VariantId(v.into()) @@ -1650,7 +1715,7 @@ impl Variant { self.source(db)?.value.expr() } - pub fn eval(self, db: &dyn HirDatabase) -> Result<i128, ConstEvalError> { + pub fn eval(self, db: &dyn HirDatabase) -> Result<i128, ConstEvalError<'_>> { db.const_eval_discriminant(self.into()) } @@ -1673,6 +1738,38 @@ impl Variant { pub fn is_unstable(self, db: &dyn HirDatabase) -> bool { db.attrs(self.id.into()).is_unstable() } + + pub fn instantiate_infer<'db>(self, infer_ctxt: &InferCtxt<'db>) -> InstantiatedVariant<'db> { + let args = + infer_ctxt.fresh_args_for_item(self.parent_enum(infer_ctxt.interner.db()).id.into()); + InstantiatedVariant { inner: self, args } + } +} + +// FIXME: Rename to `EnumVariant` +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct InstantiatedVariant<'db> { + pub(crate) inner: Variant, + pub(crate) args: GenericArgs<'db>, +} + +impl<'db> InstantiatedVariant<'db> { + pub fn parent_enum(self, db: &dyn HirDatabase) -> InstantiatedEnum<'db> { + InstantiatedEnum { inner: self.inner.id.lookup(db).parent.into(), args: self.args } + } + + pub fn fields(self, db: &dyn HirDatabase) -> Vec<InstantiatedField<'db>> { + self.inner + .id + .fields(db) + .fields() + .iter() + .map(|(id, _)| InstantiatedField { + inner: Field { parent: self.inner.into(), id }, + args: self.args, + }) + .collect() + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -1700,23 +1797,18 @@ impl_from!(Struct, Union, Enum for Adt); impl Adt { pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { - let subst = db.generic_defaults(self.into()); - (subst.is_empty() && db.generic_params(self.into()).len_type_or_consts() != 0) - || subst.iter().any(|ty| match ty.skip_binders().data(Interner) { - GenericArgData::Ty(it) => it.is_unknown(), - _ => false, - }) + has_non_default_type_params(db, self.into()) } pub fn layout(self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> { - db.layout_of_adt( - self.into(), - TyBuilder::adt(db, self.into()) - .fill_with_defaults(db, || TyKind::Error.intern(Interner)) - .build_into_subst(), - db.trait_environment(self.into()), - ) - .map(|layout| Layout(layout, db.target_data_layout(self.krate(db).id).unwrap())) + let env = db.trait_environment(self.into()); + let interner = DbInterner::new_with(db, Some(env.krate), env.block); + let adt_id = AdtId::from(self); + let args = GenericArgs::for_item_with_defaults(interner, adt_id.into(), |_, id, _| { + GenericArg::error_from_id(interner, id) + }); + db.layout_of_adt(adt_id, args, env) + .map(|layout| Layout(layout, db.target_data_layout(self.krate(db).id).unwrap())) } /// Turns this ADT into a type. Any type parameters of the ADT will be @@ -1735,17 +1827,12 @@ impl Adt { args: impl IntoIterator<Item = Type<'db>>, ) -> Type<'db> { let id = AdtId::from(self); - let mut it = args.into_iter().map(|t| t.ty); - let ty = TyBuilder::def_ty(db, id.into(), None) - .fill(|x| { - let r = it.next().unwrap_or_else(|| TyKind::Error.intern(Interner)); - match x { - ParamKind::Type => r.cast(Interner), - ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()), - ParamKind::Lifetime => error_lifetime().cast(Interner), - } - }) - .build(); + let interner = DbInterner::new_with(db, None, None); + let ty = Ty::new_adt( + interner, + id, + generic_args_from_tys(interner, id.into(), args.into_iter().map(|ty| ty.ty)), + ); Type::new(db, id, ty) } @@ -1955,8 +2042,8 @@ impl DefWithBody { acc.push( TypeMismatch { expr_or_pat, - expected: Type::new(db, DefWithBodyId::from(self), mismatch.expected.clone()), - actual: Type::new(db, DefWithBodyId::from(self), mismatch.actual.clone()), + expected: Type::new(db, DefWithBodyId::from(self), mismatch.expected), + actual: Type::new(db, DefWithBodyId::from(self), mismatch.actual), } .into(), ); @@ -2026,10 +2113,7 @@ impl DefWithBody { } mir::MirSpan::Unknown => continue, }; - acc.push( - MovedOutOfRef { ty: Type::new_for_crate(krate, moof.ty.clone()), span } - .into(), - ) + acc.push(MovedOutOfRef { ty: Type::new_for_crate(krate, moof.ty), span }.into()) } let mol = &borrowck_result.mutability_of_locals; for (binding_id, binding_data) in body.bindings() { @@ -2179,18 +2263,24 @@ impl Function { pub fn fn_ptr_type(self, db: &dyn HirDatabase) -> Type<'_> { let resolver = self.id.resolver(db); - let substs = TyBuilder::placeholder_subst(db, self.id); - let callable_sig = db.callable_item_signature(self.id.into()).substitute(Interner, &substs); - let ty = TyKind::Function(callable_sig.to_fn_ptr()).intern(Interner); + let interner = DbInterner::new_with(db, None, None); + // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s. + let callable_sig = db.callable_item_signature(self.id.into()).instantiate_identity(); + let ty = Ty::new_fn_ptr(interner, callable_sig); Type::new_with_resolver_inner(db, &resolver, ty) } + // FIXME: Find a better API to express all combinations here, perhaps we should have `PreInstantiationType`? + /// Get this function's return type pub fn ret_type(self, db: &dyn HirDatabase) -> Type<'_> { let resolver = self.id.resolver(db); - let substs = TyBuilder::placeholder_subst(db, self.id); - let callable_sig = db.callable_item_signature(self.id.into()).substitute(Interner, &substs); - let ty = callable_sig.ret().clone(); + // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s. + let ty = db + .callable_item_signature(self.id.into()) + .instantiate_identity() + .skip_binder() + .output(); Type::new_with_resolver_inner(db, &resolver, ty) } @@ -2201,26 +2291,15 @@ impl Function { generics: impl Iterator<Item = Type<'db>>, ) -> Type<'db> { let resolver = self.id.resolver(db); - let parent_id: Option<GenericDefId> = match self.id.lookup(db).container { - ItemContainerId::ImplId(it) => Some(it.into()), - ItemContainerId::TraitId(it) => Some(it.into()), - ItemContainerId::ModuleId(_) | ItemContainerId::ExternBlockId(_) => None, - }; - let mut generics = generics.map(|it| it.ty); - let mut filler = |x: &_| match x { - ParamKind::Type => { - generics.next().unwrap_or_else(|| TyKind::Error.intern(Interner)).cast(Interner) - } - ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()), - ParamKind::Lifetime => error_lifetime().cast(Interner), - }; + let interner = DbInterner::new_with(db, None, None); + let args = generic_args_from_tys(interner, self.id.into(), generics.map(|ty| ty.ty)); - let parent_substs = - parent_id.map(|id| TyBuilder::subst_for_def(db, id, None).fill(&mut filler).build()); - let substs = TyBuilder::subst_for_def(db, self.id, parent_substs).fill(&mut filler).build(); - - let callable_sig = db.callable_item_signature(self.id.into()).substitute(Interner, &substs); - let ty = callable_sig.ret().clone(); + let interner = DbInterner::new_with(db, None, None); + let ty = db + .callable_item_signature(self.id.into()) + .instantiate(interner, args) + .skip_binder() + .output(); Type::new_with_resolver_inner(db, &resolver, ty) } @@ -2229,12 +2308,17 @@ impl Function { return None; } let resolver = self.id.resolver(db); - let substs = TyBuilder::placeholder_subst(db, self.id); - let callable_sig = db.callable_item_signature(self.id.into()).substitute(Interner, &substs); - let ret_ty = callable_sig.ret().clone(); + // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s. + let ret_ty = db + .callable_item_signature(self.id.into()) + .instantiate_identity() + .skip_binder() + .output(); for pred in ret_ty.impl_trait_bounds(db).into_iter().flatten() { - if let WhereClause::AliasEq(output_eq) = pred.into_value_and_skipped_binders().0 { - return Type::new_with_resolver_inner(db, &resolver, output_eq.ty).into(); + if let ClauseKind::Projection(projection) = pred.kind().skip_binder() + && let Some(output_ty) = projection.term.as_type() + { + return Type::new_with_resolver_inner(db, &resolver, output_ty).into(); } } None @@ -2250,18 +2334,15 @@ impl Function { pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param<'_>> { let environment = db.trait_environment(self.id.into()); - let substs = TyBuilder::placeholder_subst(db, self.id); - let callable_sig = db.callable_item_signature(self.id.into()).substitute(Interner, &substs); + // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s. + let callable_sig = + db.callable_item_signature(self.id.into()).instantiate_identity().skip_binder(); callable_sig - .params() + .inputs() .iter() .enumerate() .map(|(idx, ty)| { - let ty = Type { - env: environment.clone(), - ty: ty.clone(), - _pd: PhantomCovariantLifetime::new(), - }; + let ty = Type { env: environment.clone(), ty }; Param { func: Callee::Def(CallableDefId::FunctionId(self.id)), ty, idx } }) .collect() @@ -2278,20 +2359,17 @@ impl Function { pub fn params_without_self(self, db: &dyn HirDatabase) -> Vec<Param<'_>> { let environment = db.trait_environment(self.id.into()); - let substs = TyBuilder::placeholder_subst(db, self.id); - let callable_sig = db.callable_item_signature(self.id.into()).substitute(Interner, &substs); + // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s. + let callable_sig = + db.callable_item_signature(self.id.into()).instantiate_identity().skip_binder(); let skip = if db.function_signature(self.id).has_self_param() { 1 } else { 0 }; callable_sig - .params() + .inputs() .iter() .enumerate() .skip(skip) .map(|(idx, ty)| { - let ty = Type { - env: environment.clone(), - ty: ty.clone(), - _pd: PhantomCovariantLifetime::new(), - }; + let ty = Type { env: environment.clone(), ty }; Param { func: Callee::Def(CallableDefId::FunctionId(self.id)), ty, idx } }) .collect() @@ -2304,44 +2382,18 @@ impl Function { generics: impl Iterator<Item = Type<'db>>, ) -> Vec<Param<'db>> { let environment = db.trait_environment(self.id.into()); - let parent_id: Option<GenericDefId> = match self.id.lookup(db).container { - ItemContainerId::ImplId(it) => Some(it.into()), - ItemContainerId::TraitId(it) => Some(it.into()), - ItemContainerId::ModuleId(_) | ItemContainerId::ExternBlockId(_) => None, - }; - let mut generics = generics.map(|it| it.ty); - let parent_substs = parent_id.map(|id| { - TyBuilder::subst_for_def(db, id, None) - .fill(|x| match x { - ParamKind::Type => generics - .next() - .unwrap_or_else(|| TyKind::Error.intern(Interner)) - .cast(Interner), - ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()), - ParamKind::Lifetime => error_lifetime().cast(Interner), - }) - .build() - }); - - let substs = TyBuilder::subst_for_def(db, self.id, parent_substs) - .fill(|_| { - let ty = generics.next().unwrap_or_else(|| TyKind::Error.intern(Interner)); - GenericArg::new(Interner, GenericArgData::Ty(ty)) - }) - .build(); - let callable_sig = db.callable_item_signature(self.id.into()).substitute(Interner, &substs); + let interner = DbInterner::new_with(db, None, None); + let args = generic_args_from_tys(interner, self.id.into(), generics.map(|ty| ty.ty)); + let callable_sig = + db.callable_item_signature(self.id.into()).instantiate(interner, args).skip_binder(); let skip = if db.function_signature(self.id).has_self_param() { 1 } else { 0 }; callable_sig - .params() + .inputs() .iter() .enumerate() .skip(skip) .map(|(idx, ty)| { - let ty = Type { - env: environment.clone(), - ty: ty.clone(), - _pd: PhantomCovariantLifetime::new(), - }; + let ty = Type { env: environment.clone(), ty }; Param { func: Callee::Def(CallableDefId::FunctionId(self.id)), ty, idx } }) .collect() @@ -2430,11 +2482,28 @@ impl Function { caller: Option<Function>, call_edition: Edition, ) -> bool { - let target_features = caller - .map(|caller| hir_ty::TargetFeatures::from_attrs(&db.attrs(caller.id.into()))) - .unwrap_or_default(); + let (target_features, target_feature_is_safe_in_target) = caller + .map(|caller| { + let target_features = + hir_ty::TargetFeatures::from_attrs(&db.attrs(caller.id.into())); + let target_feature_is_safe_in_target = + match &caller.krate(db).id.workspace_data(db).target { + Ok(target) => hir_ty::target_feature_is_safe_in_target(target), + Err(_) => hir_ty::TargetFeatureIsSafeInTarget::No, + }; + (target_features, target_feature_is_safe_in_target) + }) + .unwrap_or_else(|| { + (hir_ty::TargetFeatures::default(), hir_ty::TargetFeatureIsSafeInTarget::No) + }); matches!( - hir_ty::is_fn_unsafe_to_call(db, self.id, &target_features, call_edition), + hir_ty::is_fn_unsafe_to_call( + db, + self.id, + &target_features, + call_edition, + target_feature_is_safe_in_target + ), hir_ty::Unsafety::Unsafe ) } @@ -2463,10 +2532,11 @@ impl Function { self, db: &dyn HirDatabase, span_formatter: impl Fn(FileId, TextRange) -> String, - ) -> Result<String, ConstEvalError> { + ) -> Result<String, ConstEvalError<'_>> { + let interner = DbInterner::new_with(db, None, None); let body = db.monomorphized_mir_body( self.id.into(), - Substitution::empty(Interner), + GenericArgs::new_from_iter(interner, []), db.trait_environment(self.id.into()), )?; let (result, output) = interpret_mir(db, body, false, None)?; @@ -2505,18 +2575,18 @@ pub enum Access { Owned, } -impl From<hir_ty::Mutability> for Access { - fn from(mutability: hir_ty::Mutability) -> Access { +impl From<hir_ty::next_solver::Mutability> for Access { + fn from(mutability: hir_ty::next_solver::Mutability) -> Access { match mutability { - hir_ty::Mutability::Not => Access::Shared, - hir_ty::Mutability::Mut => Access::Exclusive, + hir_ty::next_solver::Mutability::Not => Access::Shared, + hir_ty::next_solver::Mutability::Mut => Access::Exclusive, } } } #[derive(Clone, PartialEq, Eq, Hash, Debug)] pub struct Param<'db> { - func: Callee, + func: Callee<'db>, /// The index in parameter list, including self parameter. idx: usize, ty: Type<'db>, @@ -2562,12 +2632,12 @@ impl<'db> Param<'db> { } } Callee::Closure(closure, _) => { - let c = db.lookup_intern_closure(closure.into()); + let c = db.lookup_intern_closure(closure); let body = db.body(c.0); - if let Expr::Closure { args, .. } = &body[c.1] { - if let Pat::Bind { id, .. } = &body[args[self.idx]] { - return Some(Local { parent: c.0, binding_id: *id }); - } + if let Expr::Closure { args, .. } = &body[c.1] + && let Pat::Bind { id, .. } = &body[args[self.idx]] + { + return Some(Local { parent: c.0, binding_id: *id }); } None } @@ -2606,12 +2676,12 @@ impl SelfParam { } pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> Type<'db> { - let substs = TyBuilder::placeholder_subst(db, self.func); + // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s. let callable_sig = - db.callable_item_signature(self.func.into()).substitute(Interner, &substs); + db.callable_item_signature(self.func.into()).instantiate_identity().skip_binder(); let environment = db.trait_environment(self.func.into()); - let ty = callable_sig.params()[0].clone(); - Type { env: environment, ty, _pd: PhantomCovariantLifetime::new() } + let ty = callable_sig.inputs().as_slice()[0]; + Type { env: environment, ty } } // FIXME: Find better API to also handle const generics @@ -2620,31 +2690,13 @@ impl SelfParam { db: &'db dyn HirDatabase, generics: impl Iterator<Item = Type<'db>>, ) -> Type<'db> { - let parent_id: GenericDefId = match self.func.lookup(db).container { - ItemContainerId::ImplId(it) => it.into(), - ItemContainerId::TraitId(it) => it.into(), - ItemContainerId::ModuleId(_) | ItemContainerId::ExternBlockId(_) => { - panic!("Never get here") - } - }; - - let mut generics = generics.map(|it| it.ty); - let mut filler = |x: &_| match x { - ParamKind::Type => { - generics.next().unwrap_or_else(|| TyKind::Error.intern(Interner)).cast(Interner) - } - ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()), - ParamKind::Lifetime => error_lifetime().cast(Interner), - }; - - let parent_substs = TyBuilder::subst_for_def(db, parent_id, None).fill(&mut filler).build(); - let substs = - TyBuilder::subst_for_def(db, self.func, Some(parent_substs)).fill(&mut filler).build(); + let interner = DbInterner::new_with(db, None, None); + let args = generic_args_from_tys(interner, self.func.into(), generics.map(|ty| ty.ty)); let callable_sig = - db.callable_item_signature(self.func.into()).substitute(Interner, &substs); + db.callable_item_signature(self.func.into()).instantiate(interner, args).skip_binder(); let environment = db.trait_environment(self.func.into()); - let ty = callable_sig.params()[0].clone(); - Type { env: environment, ty, _pd: PhantomCovariantLifetime::new() } + let ty = callable_sig.inputs().as_slice()[0]; + Type { env: environment, ty } } } @@ -2737,9 +2789,11 @@ impl Const { } /// Evaluate the constant. - pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst, ConstEvalError> { - db.const_eval(self.id.into(), Substitution::empty(Interner), None) - .map(|it| EvaluatedConst { const_: it, def: self.id.into() }) + pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst<'_>, ConstEvalError<'_>> { + let interner = DbInterner::new_with(db, None, None); + let ty = db.value_ty(self.id.into()).unwrap().instantiate_identity(); + db.const_eval(self.id.into(), GenericArgs::new_from_iter(interner, []), None) + .map(|it| EvaluatedConst { const_: it, def: self.id.into(), ty }) } } @@ -2749,41 +2803,36 @@ impl HasVisibility for Const { } } -pub struct EvaluatedConst { +pub struct EvaluatedConst<'db> { def: DefWithBodyId, - const_: hir_ty::Const, + const_: hir_ty::next_solver::Const<'db>, + ty: Ty<'db>, } -impl EvaluatedConst { +impl<'db> EvaluatedConst<'db> { pub fn render(&self, db: &dyn HirDatabase, display_target: DisplayTarget) -> String { format!("{}", self.const_.display(db, display_target)) } - pub fn render_debug(&self, db: &dyn HirDatabase) -> Result<String, MirEvalError> { - let data = self.const_.data(Interner); - if let TyKind::Scalar(s) = data.ty.kind(Interner) { - if matches!(s, Scalar::Int(_) | Scalar::Uint(_)) { - if let hir_ty::ConstValue::Concrete(c) = &data.value { - if let hir_ty::ConstScalar::Bytes(b, _) = &c.interned { - let value = u128::from_le_bytes(mir::pad16(b, false)); - let value_signed = - i128::from_le_bytes(mir::pad16(b, matches!(s, Scalar::Int(_)))); - let mut result = if let Scalar::Int(_) = s { - value_signed.to_string() - } else { - value.to_string() - }; - if value >= 10 { - format_to!(result, " ({value:#X})"); - return Ok(result); - } else { - return Ok(result); - } - } - } + pub fn render_debug(&self, db: &'db dyn HirDatabase) -> Result<String, MirEvalError<'db>> { + let kind = self.const_.kind(); + if let ConstKind::Value(c) = kind + && let ty = c.ty.kind() + && let TyKind::Int(_) | TyKind::Uint(_) = ty + { + let b = &c.value.inner().memory; + let value = u128::from_le_bytes(mir::pad16(b, false)); + let value_signed = i128::from_le_bytes(mir::pad16(b, matches!(ty, TyKind::Int(_)))); + let mut result = + if let TyKind::Int(_) = ty { value_signed.to_string() } else { value.to_string() }; + if value >= 10 { + format_to!(result, " ({value:#X})"); + return Ok(result); + } else { + return Ok(result); } } - mir::render_const_using_debug_impl(db, self.def, &self.const_) + mir::render_const_using_debug_impl(db, self.def, self.const_, self.ty) } } @@ -2821,9 +2870,11 @@ impl Static { } /// Evaluate the static initializer. - pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst, ConstEvalError> { - db.const_eval(self.id.into(), Substitution::empty(Interner), None) - .map(|it| EvaluatedConst { const_: it, def: self.id.into() }) + pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst<'_>, ConstEvalError<'_>> { + let interner = DbInterner::new_with(db, None, None); + let ty = db.value_ty(self.id.into()).unwrap().instantiate_identity(); + db.const_eval(self.id.into(), GenericArgs::new_from_iter(interner, []), None) + .map(|it| EvaluatedConst { const_: it, def: self.id.into(), ty }) } } @@ -2938,41 +2989,13 @@ impl HasVisibility for Trait { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct TraitAlias { - pub(crate) id: TraitAliasId, -} - -impl TraitAlias { - pub fn module(self, db: &dyn HirDatabase) -> Module { - Module { id: self.id.lookup(db).container } - } - - pub fn name(self, db: &dyn HirDatabase) -> Name { - db.trait_alias_signature(self.id).name.clone() - } -} - -impl HasVisibility for TraitAlias { - fn visibility(&self, db: &dyn HirDatabase) -> Visibility { - let loc = self.id.lookup(db); - let source = loc.source(db); - visibility_from_ast(db, self.id, source.map(|src| src.visibility())) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct TypeAlias { pub(crate) id: TypeAliasId, } impl TypeAlias { pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { - let subst = db.generic_defaults(self.id.into()); - (subst.is_empty() && db.generic_params(self.id.into()).len_type_or_consts() != 0) - || subst.iter().any(|ty| match ty.skip_binders().data(Interner) { - GenericArgData::Ty(it) => it.is_unknown(), - _ => false, - }) + has_non_default_type_params(db, self.id.into()) } pub fn module(self, db: &dyn HirDatabase) -> Module { @@ -2983,8 +3006,8 @@ impl TypeAlias { Type::from_def(db, self.id) } - pub fn ty_placeholders(self, db: &dyn HirDatabase) -> Type<'_> { - Type::from_def_placeholders(db, self.id) + pub fn ty_params(self, db: &dyn HirDatabase) -> Type<'_> { + Type::from_def_params(db, self.id) } pub fn name(self, db: &dyn HirDatabase) -> Name { @@ -3037,7 +3060,8 @@ impl BuiltinType { pub fn ty<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> { let core = Crate::core(db).map(|core| core.id).unwrap_or_else(|| db.all_crates()[0]); - Type::new_for_crate(core, TyBuilder::builtin(self.inner)) + let interner = DbInterner::new_with(db, None, None); + Type::new_for_crate(core, Ty::from_builtin_type(interner, self.inner)) } pub fn name(self) -> Name { @@ -3582,7 +3606,6 @@ pub enum GenericDef { Function(Function), Adt(Adt), Trait(Trait), - TraitAlias(TraitAlias), TypeAlias(TypeAlias), Impl(Impl), // consts can have type parameters from their parents (i.e. associated consts of traits) @@ -3593,7 +3616,6 @@ impl_from!( Function, Adt(Struct, Enum, Union), Trait, - TraitAlias, TypeAlias, Impl, Const, @@ -3643,7 +3665,6 @@ impl GenericDef { GenericDef::Function(it) => it.id.into(), GenericDef::Adt(it) => it.into(), GenericDef::Trait(it) => it.id.into(), - GenericDef::TraitAlias(it) => it.id.into(), GenericDef::TypeAlias(it) => it.id.into(), GenericDef::Impl(it) => it.id.into(), GenericDef::Const(it) => it.id.into(), @@ -3668,7 +3689,6 @@ impl GenericDef { GenericDefId::FunctionId(it) => db.function_signature_with_source_map(it).1, GenericDefId::ImplId(it) => db.impl_signature_with_source_map(it).1, GenericDefId::StaticId(_) => return, - GenericDefId::TraitAliasId(it) => db.trait_alias_signature_with_source_map(it).1, GenericDefId::TraitId(it) => db.trait_signature_with_source_map(it).1, GenericDefId::TypeAliasId(it) => db.type_alias_signature_with_source_map(it).1, }; @@ -3705,7 +3725,6 @@ impl GenericDef { GenericDef::Adt(Adt::Enum(_)) => "enum", GenericDef::Adt(Adt::Union(_)) => "union", GenericDef::Trait(_) => "trait", - GenericDef::TraitAlias(_) => "trait alias", GenericDef::TypeAlias(_) => "type alias", GenericDef::Impl(_) => "impl", GenericDef::Const(_) => "constant", @@ -3718,14 +3737,13 @@ impl GenericDef { #[derive(Debug)] pub struct GenericSubstitution<'db> { def: GenericDefId, - subst: Substitution, - env: Arc<TraitEnvironment>, - _pd: PhantomCovariantLifetime<'db>, + subst: GenericArgs<'db>, + env: Arc<TraitEnvironment<'db>>, } impl<'db> GenericSubstitution<'db> { - fn new(def: GenericDefId, subst: Substitution, env: Arc<TraitEnvironment>) -> Self { - Self { def, subst, env, _pd: PhantomCovariantLifetime::new() } + fn new(def: GenericDefId, subst: GenericArgs<'db>, env: Arc<TraitEnvironment<'db>>) -> Self { + Self { def, subst, env } } pub fn types(&self, db: &'db dyn HirDatabase) -> Vec<(Symbol, Type<'db>)> { @@ -3755,26 +3773,23 @@ impl<'db> GenericSubstitution<'db> { TypeOrConstParamData::TypeParamData(param) => Some(param.name.clone()), TypeOrConstParamData::ConstParamData(_) => None, }); - let parent_len = self.subst.len(Interner) + let parent_len = self.subst.len() - generics .iter_type_or_consts() .filter(|g| matches!(g.1, TypeOrConstParamData::TypeParamData(..))) .count(); - let container_params = self.subst.as_slice(Interner)[..parent_len] + let container_params = self.subst.as_slice()[..parent_len] .iter() - .filter_map(|param| param.ty(Interner).cloned()) + .filter_map(|param| param.ty()) .zip(container_type_params.into_iter().flatten()); - let self_params = self.subst.as_slice(Interner)[parent_len..] + let self_params = self.subst.as_slice()[parent_len..] .iter() - .filter_map(|param| param.ty(Interner).cloned()) + .filter_map(|param| param.ty()) .zip(type_params); container_params .chain(self_params) .filter_map(|(ty, name)| { - Some(( - name?.symbol().clone(), - Type { ty, env: self.env.clone(), _pd: PhantomCovariantLifetime::new() }, - )) + Some((name?.symbol().clone(), Type { ty, env: self.env.clone() })) }) .collect() } @@ -3880,7 +3895,7 @@ impl Local { pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> { let def = self.parent; let infer = db.infer(def); - let ty = infer[self.binding_id].clone(); + let ty = infer[self.binding_id]; Type::new(db, def, ty) } @@ -3982,49 +3997,25 @@ impl DeriveHelper { } } -// FIXME: Wrong name? This is could also be a registered attribute #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct BuiltinAttr { - krate: Option<base_db::Crate>, idx: u32, } impl BuiltinAttr { - // FIXME: consider crates\hir_def\src\nameres\attr_resolution.rs? - pub(crate) fn by_name(db: &dyn HirDatabase, krate: Crate, name: &str) -> Option<Self> { - if let builtin @ Some(_) = Self::builtin(name) { - return builtin; - } - let idx = crate_def_map(db, krate.id) - .registered_attrs() - .iter() - .position(|it| it.as_str() == name)? as u32; - Some(BuiltinAttr { krate: Some(krate.id), idx }) - } - fn builtin(name: &str) -> Option<Self> { hir_expand::inert_attr_macro::find_builtin_attr_idx(&Symbol::intern(name)) - .map(|idx| BuiltinAttr { krate: None, idx: idx as u32 }) + .map(|idx| BuiltinAttr { idx: idx as u32 }) } - pub fn name(&self, db: &dyn HirDatabase) -> Name { - match self.krate { - Some(krate) => Name::new_symbol_root( - crate_def_map(db, krate).registered_attrs()[self.idx as usize].clone(), - ), - None => Name::new_symbol_root(Symbol::intern( - hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].name, - )), - } + pub fn name(&self) -> Name { + Name::new_symbol_root(Symbol::intern( + hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].name, + )) } - pub fn template(&self, _: &dyn HirDatabase) -> Option<AttributeTemplate> { - match self.krate { - Some(_) => None, - None => { - Some(hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].template) - } - } + pub fn template(&self) -> Option<AttributeTemplate> { + Some(hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].template) } } @@ -4121,7 +4112,39 @@ impl GenericParam { GenericParam::ConstParam(_) => return None, GenericParam::LifetimeParam(it) => generics.lifetime_idx(it.id)?, }; - db.variances_of(parent)?.get(index).copied() + db.variances_of(parent).get(index).map(Into::into) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Variance { + Bivariant, + Covariant, + Contravariant, + Invariant, +} + +impl From<rustc_type_ir::Variance> for Variance { + #[inline] + fn from(value: rustc_type_ir::Variance) -> Self { + match value { + rustc_type_ir::Variance::Covariant => Variance::Covariant, + rustc_type_ir::Variance::Invariant => Variance::Invariant, + rustc_type_ir::Variance::Contravariant => Variance::Contravariant, + rustc_type_ir::Variance::Bivariant => Variance::Bivariant, + } + } +} + +impl fmt::Display for Variance { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let description = match self { + Variance::Bivariant => "bivariant", + Variance::Covariant => "covariant", + Variance::Contravariant => "contravariant", + Variance::Invariant => "invariant", + }; + f.pad(description) } } @@ -4160,8 +4183,9 @@ impl TypeParam { pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> { let resolver = self.id.parent().resolver(db); - let ty = - TyKind::Placeholder(hir_ty::to_placeholder_idx(db, self.id.into())).intern(Interner); + let interner = DbInterner::new_with(db, None, None); + let index = hir_ty::param_idx(db, self.id.into()).unwrap(); + let ty = Ty::new_param(interner, self.id, index as u32); Type::new_with_resolver_inner(db, &resolver, ty) } @@ -4171,10 +4195,8 @@ impl TypeParam { pub fn trait_bounds(self, db: &dyn HirDatabase) -> Vec<Trait> { db.generic_predicates_for_param(self.id.parent(), self.id.into(), None) .iter() - .filter_map(|pred| match &pred.skip_binders().skip_binders() { - hir_ty::WhereClause::Implemented(trait_ref) => { - Some(Trait::from(trait_ref.hir_trait_id())) - } + .filter_map(|pred| match &pred.kind().skip_binder() { + ClauseKind::Trait(trait_ref) => Some(Trait::from(trait_ref.def_id().0)), _ => None, }) .collect() @@ -4183,9 +4205,9 @@ impl TypeParam { pub fn default(self, db: &dyn HirDatabase) -> Option<Type<'_>> { let ty = generic_arg_from_param(db, self.id.into())?; let resolver = self.id.parent().resolver(db); - match ty.data(Interner) { - GenericArgData::Ty(it) if *it.kind(Interner) != TyKind::Error => { - Some(Type::new_with_resolver_inner(db, &resolver, it.clone())) + match ty { + GenericArg::Ty(it) if !it.is_ty_error() => { + Some(Type::new_with_resolver_inner(db, &resolver, it)) } _ => None, } @@ -4246,7 +4268,7 @@ impl ConstParam { } pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> { - Type::new(db, self.id.parent(), db.const_param_ty(self.id)) + Type::new(db, self.id.parent(), db.const_param_ty_ns(self.id)) } pub fn default( @@ -4255,17 +4277,16 @@ impl ConstParam { display_target: DisplayTarget, ) -> Option<ast::ConstArg> { let arg = generic_arg_from_param(db, self.id.into())?; - known_const_to_ast(arg.constant(Interner)?, db, display_target) + known_const_to_ast(arg.konst()?, db, display_target) } } -fn generic_arg_from_param(db: &dyn HirDatabase, id: TypeOrConstParamId) -> Option<GenericArg> { +fn generic_arg_from_param(db: &dyn HirDatabase, id: TypeOrConstParamId) -> Option<GenericArg<'_>> { let local_idx = hir_ty::param_idx(db, id)?; let defaults = db.generic_defaults(id.parent); - let ty = defaults.get(local_idx)?.clone(); - let full_subst = TyBuilder::placeholder_subst(db, id.parent); - let subst = &full_subst.as_slice(Interner)[..local_idx]; - Some(ty.substitute(Interner, &subst)) + let ty = defaults.get(local_idx)?; + // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s. + Some(ty.instantiate_identity()) } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -4347,11 +4368,8 @@ impl Impl { module.id.def_map(db)[module.id.local_id].scope.impls().map(Into::into).collect() } - pub fn all_for_type<'db>( - db: &'db dyn HirDatabase, - Type { ty, env, _pd: _ }: Type<'db>, - ) -> Vec<Impl> { - let def_crates = match method_resolution::def_crates(db, &ty, env.krate) { + pub fn all_for_type<'db>(db: &'db dyn HirDatabase, Type { ty, env }: Type<'db>) -> Vec<Impl> { + let def_crates = match method_resolution::def_crates(db, ty, env.krate) { Some(def_crates) => def_crates, None => return Vec::new(), }; @@ -4359,10 +4377,10 @@ impl Impl { let filter = |impl_def: &Impl| { let self_ty = impl_def.self_ty(db); let rref = self_ty.remove_ref(); - ty.equals_ctor(rref.as_ref().map_or(&self_ty.ty, |it| &it.ty)) + ty.equals_ctor(rref.as_ref().map_or(self_ty.ty, |it| it.ty)) }; - let fp = TyFingerprint::for_inherent_impl(&ty); + let fp = TyFingerprint::for_inherent_impl(ty); let fp = match fp { Some(fp) => fp, None => return Vec::new(), @@ -4372,7 +4390,7 @@ impl Impl { def_crates.iter().for_each(|&id| { all.extend( db.inherent_impls_in_crate(id) - .for_self_ty(&ty) + .for_self_ty(ty) .iter() .cloned() .map(Self::from) @@ -4393,11 +4411,10 @@ impl Impl { ); } - if let Some(block) = ty.adt_id(Interner).and_then(|def| def.0.module(db).containing_block()) - { + if let Some(block) = ty.as_adt().and_then(|(def, _)| def.module(db).containing_block()) { if let Some(inherent_impls) = db.inherent_impls_in_block(block) { all.extend( - inherent_impls.for_self_ty(&ty).iter().cloned().map(Self::from).filter(filter), + inherent_impls.for_self_ty(ty).iter().cloned().map(Self::from).filter(filter), ); } if let Some(trait_impls) = db.trait_impls_in_block(block) { @@ -4421,31 +4438,30 @@ impl Impl { let impls = db.trait_impls_in_crate(id); all.extend(impls.for_trait(trait_.id).map(Self::from)) } - if let Some(block) = module.id.containing_block() { - if let Some(trait_impls) = db.trait_impls_in_block(block) { - all.extend(trait_impls.for_trait(trait_.id).map(Self::from)); - } + if let Some(block) = module.id.containing_block() + && let Some(trait_impls) = db.trait_impls_in_block(block) + { + all.extend(trait_impls.for_trait(trait_.id).map(Self::from)); } all } pub fn trait_(self, db: &dyn HirDatabase) -> Option<Trait> { let trait_ref = db.impl_trait(self.id)?; - let id = trait_ref.skip_binders().hir_trait_id(); - Some(Trait { id }) + let id = trait_ref.skip_binder().def_id; + Some(Trait { id: id.0 }) } pub fn trait_ref(self, db: &dyn HirDatabase) -> Option<TraitRef<'_>> { - let substs = TyBuilder::placeholder_subst(db, self.id); - let trait_ref = db.impl_trait(self.id)?.substitute(Interner, &substs); + let trait_ref = db.impl_trait(self.id)?.instantiate_identity(); let resolver = self.id.resolver(db); Some(TraitRef::new_with_resolver(db, &resolver, trait_ref)) } pub fn self_ty(self, db: &dyn HirDatabase) -> Type<'_> { let resolver = self.id.resolver(db); - let substs = TyBuilder::placeholder_subst(db, self.id); - let ty = db.impl_self_ty(self.id).substitute(Interner, &substs); + // FIXME: This shouldn't be `instantiate_identity()`, we shouldn't leak `TyKind::Param`s. + let ty = db.impl_self_ty(self.id).instantiate_identity(); Type::new_with_resolver_inner(db, &resolver, ty) } @@ -4505,120 +4521,138 @@ impl Impl { #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct TraitRef<'db> { - env: Arc<TraitEnvironment>, - trait_ref: hir_ty::TraitRef, - _pd: PhantomCovariantLifetime<'db>, + env: Arc<TraitEnvironment<'db>>, + trait_ref: hir_ty::next_solver::TraitRef<'db>, } impl<'db> TraitRef<'db> { pub(crate) fn new_with_resolver( db: &'db dyn HirDatabase, resolver: &Resolver<'_>, - trait_ref: hir_ty::TraitRef, + trait_ref: hir_ty::next_solver::TraitRef<'db>, ) -> Self { let env = resolver .generic_def() .map_or_else(|| TraitEnvironment::empty(resolver.krate()), |d| db.trait_environment(d)); - TraitRef { env, trait_ref, _pd: PhantomCovariantLifetime::new() } + TraitRef { env, trait_ref } } pub fn trait_(&self) -> Trait { - let id = self.trait_ref.hir_trait_id(); - Trait { id } + Trait { id: self.trait_ref.def_id.0 } } - pub fn self_ty(&self) -> Type<'_> { - let ty = self.trait_ref.self_type_parameter(Interner); - Type { env: self.env.clone(), ty, _pd: PhantomCovariantLifetime::new() } + pub fn self_ty(&self) -> TypeNs<'_> { + let ty = self.trait_ref.self_ty(); + TypeNs { env: self.env.clone(), ty } } /// Returns `idx`-th argument of this trait reference if it is a type argument. Note that the /// first argument is the `Self` type. - pub fn get_type_argument(&self, idx: usize) -> Option<Type<'db>> { + pub fn get_type_argument(&self, idx: usize) -> Option<TypeNs<'db>> { self.trait_ref - .substitution - .as_slice(Interner) + .args + .as_slice() .get(idx) - .and_then(|arg| arg.ty(Interner)) - .cloned() - .map(|ty| Type { env: self.env.clone(), ty, _pd: PhantomCovariantLifetime::new() }) + .and_then(|arg| arg.ty()) + .map(|ty| TypeNs { env: self.env.clone(), ty }) } } -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct Closure { - id: ClosureId, - subst: Substitution, +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +enum AnyClosureId { + ClosureId(InternedClosureId), + CoroutineClosureId(InternedCoroutineId), } -impl From<Closure> for ClosureId { - fn from(value: Closure) -> Self { - value.id - } +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct Closure<'db> { + id: AnyClosureId, + subst: GenericArgs<'db>, } -impl Closure { - fn as_ty(self) -> Ty { - TyKind::Closure(self.id, self.subst).intern(Interner) +impl<'db> Closure<'db> { + fn as_ty(&self, db: &'db dyn HirDatabase) -> Ty<'db> { + let interner = DbInterner::new_with(db, None, None); + match self.id { + AnyClosureId::ClosureId(id) => Ty::new_closure(interner, id.into(), self.subst), + AnyClosureId::CoroutineClosureId(id) => { + Ty::new_coroutine_closure(interner, id.into(), self.subst) + } + } } pub fn display_with_id(&self, db: &dyn HirDatabase, display_target: DisplayTarget) -> String { - self.clone() - .as_ty() + self.as_ty(db) .display(db, display_target) .with_closure_style(ClosureStyle::ClosureWithId) .to_string() } pub fn display_with_impl(&self, db: &dyn HirDatabase, display_target: DisplayTarget) -> String { - self.clone() - .as_ty() + self.as_ty(db) .display(db, display_target) .with_closure_style(ClosureStyle::ImplFn) .to_string() } - pub fn captured_items(&self, db: &dyn HirDatabase) -> Vec<ClosureCapture> { - let owner = db.lookup_intern_closure((self.id).into()).0; - let infer = &db.infer(owner); - let info = infer.closure_info(&self.id); + pub fn captured_items(&self, db: &'db dyn HirDatabase) -> Vec<ClosureCapture<'db>> { + let AnyClosureId::ClosureId(id) = self.id else { + // FIXME: Infer coroutine closures' captures. + return Vec::new(); + }; + let owner = db.lookup_intern_closure(id).0; + let infer = db.infer(owner); + let info = infer.closure_info(id); info.0 .iter() .cloned() - .map(|capture| ClosureCapture { owner, closure: self.id, capture }) + .map(|capture| ClosureCapture { owner, closure: id, capture }) .collect() } - pub fn capture_types<'db>(&self, db: &'db dyn HirDatabase) -> Vec<Type<'db>> { - let owner = db.lookup_intern_closure((self.id).into()).0; - let infer = &db.infer(owner); - let (captures, _) = infer.closure_info(&self.id); + pub fn capture_types(&self, db: &'db dyn HirDatabase) -> Vec<Type<'db>> { + let AnyClosureId::ClosureId(id) = self.id else { + // FIXME: Infer coroutine closures' captures. + return Vec::new(); + }; + let owner = db.lookup_intern_closure(id).0; + let infer = db.infer(owner); + let (captures, _) = infer.closure_info(id); + let env = db.trait_environment_for_body(owner); captures .iter() - .map(|capture| Type { - env: db.trait_environment_for_body(owner), - ty: capture.ty(&self.subst), - _pd: PhantomCovariantLifetime::new(), - }) + .map(|capture| Type { env: env.clone(), ty: capture.ty(db, self.subst) }) .collect() } pub fn fn_trait(&self, db: &dyn HirDatabase) -> FnTrait { - let owner = db.lookup_intern_closure((self.id).into()).0; - let infer = &db.infer(owner); - let info = infer.closure_info(&self.id); - info.1 + match self.id { + AnyClosureId::ClosureId(id) => { + let owner = db.lookup_intern_closure(id).0; + let infer = db.infer(owner); + let info = infer.closure_info(id); + info.1 + } + AnyClosureId::CoroutineClosureId(_id) => { + // FIXME: Infer kind for coroutine closures. + match self.subst.as_coroutine_closure().kind() { + rustc_type_ir::ClosureKind::Fn => FnTrait::AsyncFn, + rustc_type_ir::ClosureKind::FnMut => FnTrait::AsyncFnMut, + rustc_type_ir::ClosureKind::FnOnce => FnTrait::AsyncFnOnce, + } + } + } } } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct ClosureCapture { +pub struct ClosureCapture<'db> { owner: DefWithBodyId, - closure: ClosureId, - capture: hir_ty::CapturedItem, + closure: InternedClosureId, + capture: hir_ty::CapturedItem<'db>, } -impl ClosureCapture { +impl<'db> ClosureCapture<'db> { pub fn local(&self) -> Local { Local { parent: self.owner, binding_id: self.capture.local() } } @@ -4731,16 +4765,15 @@ impl CaptureUsageSource { #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct Type<'db> { - env: Arc<TraitEnvironment>, - ty: Ty, - _pd: PhantomCovariantLifetime<'db>, + env: Arc<TraitEnvironment<'db>>, + ty: Ty<'db>, } impl<'db> Type<'db> { pub(crate) fn new_with_resolver( db: &'db dyn HirDatabase, resolver: &Resolver<'_>, - ty: Ty, + ty: Ty<'db>, ) -> Self { Type::new_with_resolver_inner(db, resolver, ty) } @@ -4748,225 +4781,252 @@ impl<'db> Type<'db> { pub(crate) fn new_with_resolver_inner( db: &'db dyn HirDatabase, resolver: &Resolver<'_>, - ty: Ty, + ty: Ty<'db>, ) -> Self { let environment = resolver .generic_def() .map_or_else(|| TraitEnvironment::empty(resolver.krate()), |d| db.trait_environment(d)); - Type { env: environment, ty, _pd: PhantomCovariantLifetime::new() } + Type { env: environment, ty } } - pub(crate) fn new_for_crate(krate: base_db::Crate, ty: Ty) -> Self { - Type { env: TraitEnvironment::empty(krate), ty, _pd: PhantomCovariantLifetime::new() } + pub(crate) fn new_for_crate(krate: base_db::Crate, ty: Ty<'db>) -> Self { + Type { env: TraitEnvironment::empty(krate), ty } } - fn new(db: &'db dyn HirDatabase, lexical_env: impl HasResolver, ty: Ty) -> Self { + fn new(db: &'db dyn HirDatabase, lexical_env: impl HasResolver, ty: Ty<'db>) -> Self { let resolver = lexical_env.resolver(db); let environment = resolver .generic_def() .map_or_else(|| TraitEnvironment::empty(resolver.krate()), |d| db.trait_environment(d)); - Type { env: environment, ty, _pd: PhantomCovariantLifetime::new() } + Type { env: environment, ty } } fn from_def(db: &'db dyn HirDatabase, def: impl Into<TyDefId> + HasResolver) -> Self { + let interner = DbInterner::new_with(db, None, None); let ty = db.ty(def.into()); - let substs = TyBuilder::unknown_subst( - db, - match def.into() { - TyDefId::AdtId(it) => GenericDefId::AdtId(it), - TyDefId::TypeAliasId(it) => GenericDefId::TypeAliasId(it), - TyDefId::BuiltinType(_) => return Type::new(db, def, ty.skip_binders().clone()), - }, - ); - Type::new(db, def, ty.substitute(Interner, &substs)) + let def = match def.into() { + TyDefId::AdtId(it) => GenericDefId::AdtId(it), + TyDefId::TypeAliasId(it) => GenericDefId::TypeAliasId(it), + TyDefId::BuiltinType(_) => { + return Type::new(db, def, ty.skip_binder()); + } + }; + let args = GenericArgs::error_for_item(interner, def.into()); + Type::new(db, def, ty.instantiate(interner, args)) } - fn from_def_placeholders( - db: &'db dyn HirDatabase, - def: impl Into<TyDefId> + HasResolver, - ) -> Self { + // FIXME: We shouldn't leak `TyKind::Param`s. + fn from_def_params(db: &'db dyn HirDatabase, def: impl Into<TyDefId> + HasResolver) -> Self { let ty = db.ty(def.into()); - let substs = TyBuilder::placeholder_subst( - db, - match def.into() { - TyDefId::AdtId(it) => GenericDefId::AdtId(it), - TyDefId::TypeAliasId(it) => GenericDefId::TypeAliasId(it), - TyDefId::BuiltinType(_) => return Type::new(db, def, ty.skip_binders().clone()), - }, - ); - Type::new(db, def, ty.substitute(Interner, &substs)) + Type::new(db, def, ty.instantiate_identity()) } fn from_value_def( db: &'db dyn HirDatabase, def: impl Into<ValueTyDefId> + HasResolver, ) -> Self { + let interner = DbInterner::new_with(db, None, None); let Some(ty) = db.value_ty(def.into()) else { - return Type::new(db, def, TyKind::Error.intern(Interner)); + return Type::new(db, def, Ty::new_error(interner, ErrorGuaranteed)); }; - let substs = TyBuilder::unknown_subst( - db, - match def.into() { - ValueTyDefId::ConstId(it) => GenericDefId::ConstId(it), - ValueTyDefId::FunctionId(it) => GenericDefId::FunctionId(it), - ValueTyDefId::StructId(it) => GenericDefId::AdtId(AdtId::StructId(it)), - ValueTyDefId::UnionId(it) => GenericDefId::AdtId(AdtId::UnionId(it)), - ValueTyDefId::EnumVariantId(it) => { - GenericDefId::AdtId(AdtId::EnumId(it.lookup(db).parent)) - } - ValueTyDefId::StaticId(_) => return Type::new(db, def, ty.skip_binders().clone()), - }, - ); - Type::new(db, def, ty.substitute(Interner, &substs)) + let def = match def.into() { + ValueTyDefId::ConstId(it) => GenericDefId::ConstId(it), + ValueTyDefId::FunctionId(it) => GenericDefId::FunctionId(it), + ValueTyDefId::StructId(it) => GenericDefId::AdtId(AdtId::StructId(it)), + ValueTyDefId::UnionId(it) => GenericDefId::AdtId(AdtId::UnionId(it)), + ValueTyDefId::EnumVariantId(it) => { + GenericDefId::AdtId(AdtId::EnumId(it.lookup(db).parent)) + } + ValueTyDefId::StaticId(_) => { + return Type::new(db, def, ty.skip_binder()); + } + }; + let args = GenericArgs::error_for_item(interner, def.into()); + Type::new(db, def, ty.instantiate(interner, args)) } pub fn new_slice(ty: Self) -> Self { - Type { env: ty.env, ty: TyBuilder::slice(ty.ty), _pd: PhantomCovariantLifetime::new() } + let interner = DbInterner::conjure(); + Type { env: ty.env, ty: Ty::new_slice(interner, ty.ty) } } pub fn new_tuple(krate: base_db::Crate, tys: &[Self]) -> Self { - let tys = tys.iter().map(|it| it.ty.clone()); - Type { - env: TraitEnvironment::empty(krate), - ty: TyBuilder::tuple_with(tys), - _pd: PhantomCovariantLifetime::new(), - } + let tys = tys.iter().map(|it| it.ty); + let interner = DbInterner::conjure(); + Type { env: TraitEnvironment::empty(krate), ty: Ty::new_tup_from_iter(interner, tys) } } pub fn is_unit(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Tuple(0, ..)) + self.ty.is_unit() } pub fn is_bool(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Bool)) + matches!(self.ty.kind(), TyKind::Bool) } pub fn is_str(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Str) + matches!(self.ty.kind(), TyKind::Str) } pub fn is_never(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Never) + matches!(self.ty.kind(), TyKind::Never) } pub fn is_mutable_reference(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..)) + matches!(self.ty.kind(), TyKind::Ref(.., hir_ty::next_solver::Mutability::Mut)) } pub fn is_reference(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Ref(..)) + matches!(self.ty.kind(), TyKind::Ref(..)) } pub fn contains_reference(&self, db: &'db dyn HirDatabase) -> bool { - return go(db, self.env.krate, &self.ty); + let interner = DbInterner::new_with(db, None, None); + return self.ty.visit_with(&mut Visitor { interner }).is_break(); + + fn is_phantom_data(db: &dyn HirDatabase, adt_id: AdtId) -> bool { + match adt_id { + AdtId::StructId(s) => { + let flags = db.struct_signature(s).flags; + flags.contains(StructFlags::IS_PHANTOM_DATA) + } + AdtId::UnionId(_) | AdtId::EnumId(_) => false, + } + } - fn go(db: &dyn HirDatabase, krate: base_db::Crate, ty: &Ty) -> bool { - match ty.kind(Interner) { - // Reference itself - TyKind::Ref(_, _, _) => true, + struct Visitor<'db> { + interner: DbInterner<'db>, + } - // For non-phantom_data adts we check variants/fields as well as generic parameters - TyKind::Adt(adt_id, substitution) - if !db.adt_datum(krate, *adt_id).flags.phantom_data => - { - let adt_datum = &db.adt_datum(krate, *adt_id); - let adt_datum_bound = - adt_datum.binders.clone().substitute(Interner, substitution); - adt_datum_bound - .variants - .into_iter() - .flat_map(|variant| variant.fields.into_iter()) - .any(|ty| go(db, krate, &ty)) - || substitution - .iter(Interner) - .filter_map(|x| x.ty(Interner)) - .any(|ty| go(db, krate, ty)) + impl<'db> TypeVisitor<DbInterner<'db>> for Visitor<'db> { + type Result = ControlFlow<()>; + + fn visit_ty(&mut self, ty: Ty<'db>) -> Self::Result { + match ty.kind() { + // Reference itself + TyKind::Ref(..) => ControlFlow::Break(()), + + // For non-phantom_data adts we check variants/fields as well as generic parameters + TyKind::Adt(adt_def, args) + if !is_phantom_data(self.interner.db(), adt_def.def_id().0) => + { + let _variant_id_to_fields = |id: VariantId| { + let variant_data = &id.fields(self.interner.db()); + if variant_data.fields().is_empty() { + vec![] + } else { + let field_types = self.interner.db().field_types(id); + variant_data + .fields() + .iter() + .map(|(idx, _)| { + field_types[idx].instantiate(self.interner, args) + }) + .filter(|it| !it.references_non_lt_error()) + .collect() + } + }; + let variant_id_to_fields = |_: VariantId| vec![]; + + let variants: Vec<Vec<Ty<'db>>> = match adt_def.def_id().0 { + AdtId::StructId(id) => { + vec![variant_id_to_fields(id.into())] + } + AdtId::EnumId(id) => id + .enum_variants(self.interner.db()) + .variants + .iter() + .map(|&(variant_id, _, _)| variant_id_to_fields(variant_id.into())) + .collect(), + AdtId::UnionId(id) => { + vec![variant_id_to_fields(id.into())] + } + }; + + variants + .into_iter() + .flat_map(|variant| variant.into_iter()) + .try_for_each(|ty| ty.visit_with(self))?; + args.visit_with(self) + } + // And for `PhantomData<T>`, we check `T`. + _ => ty.super_visit_with(self), } - // And for `PhantomData<T>`, we check `T`. - TyKind::Adt(_, substitution) - | TyKind::Tuple(_, substitution) - | TyKind::OpaqueType(_, substitution) - | TyKind::AssociatedType(_, substitution) - | TyKind::FnDef(_, substitution) => substitution - .iter(Interner) - .filter_map(|x| x.ty(Interner)) - .any(|ty| go(db, krate, ty)), - - // For `[T]` or `*T` we check `T` - TyKind::Array(ty, _) | TyKind::Slice(ty) | TyKind::Raw(_, ty) => go(db, krate, ty), - - // Consider everything else as not reference - _ => false, } } } pub fn as_reference(&self) -> Option<(Type<'db>, Mutability)> { - let (ty, _lt, m) = self.ty.as_reference()?; - let m = Mutability::from_mutable(matches!(m, hir_ty::Mutability::Mut)); - Some((self.derived(ty.clone()), m)) + let TyKind::Ref(_lt, ty, m) = self.ty.kind() else { return None }; + let m = Mutability::from_mutable(matches!(m, hir_ty::next_solver::Mutability::Mut)); + Some((self.derived(ty), m)) } pub fn add_reference(&self, mutability: Mutability) -> Self { + let interner = DbInterner::conjure(); let ty_mutability = match mutability { - Mutability::Shared => hir_ty::Mutability::Not, - Mutability::Mut => hir_ty::Mutability::Mut, + Mutability::Shared => hir_ty::next_solver::Mutability::Not, + Mutability::Mut => hir_ty::next_solver::Mutability::Mut, }; - self.derived(TyKind::Ref(ty_mutability, error_lifetime(), self.ty.clone()).intern(Interner)) + self.derived(Ty::new_ref(interner, Region::error(interner), self.ty, ty_mutability)) } pub fn is_slice(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Slice(..)) + matches!(self.ty.kind(), TyKind::Slice(..)) } pub fn is_usize(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize))) + matches!(self.ty.kind(), TyKind::Uint(rustc_type_ir::UintTy::Usize)) } pub fn is_float(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Float(_))) + matches!(self.ty.kind(), TyKind::Float(_)) } pub fn is_char(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Char)) + matches!(self.ty.kind(), TyKind::Char) } pub fn is_int_or_uint(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_))) + matches!(self.ty.kind(), TyKind::Int(_) | TyKind::Uint(_)) } pub fn is_scalar(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Scalar(_)) + matches!( + self.ty.kind(), + TyKind::Bool | TyKind::Char | TyKind::Int(_) | TyKind::Uint(_) | TyKind::Float(_) + ) } pub fn is_tuple(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Tuple(..)) + matches!(self.ty.kind(), TyKind::Tuple(..)) } pub fn remove_ref(&self) -> Option<Type<'db>> { - match &self.ty.kind(Interner) { - TyKind::Ref(.., ty) => Some(self.derived(ty.clone())), + match self.ty.kind() { + TyKind::Ref(_, ty, _) => Some(self.derived(ty)), _ => None, } } pub fn as_slice(&self) -> Option<Type<'db>> { - match &self.ty.kind(Interner) { - TyKind::Slice(ty) => Some(self.derived(ty.clone())), + match self.ty.kind() { + TyKind::Slice(ty) => Some(self.derived(ty)), _ => None, } } pub fn strip_references(&self) -> Self { - self.derived(self.ty.strip_references().clone()) + self.derived(self.ty.strip_references()) } + // FIXME: This is the same as `remove_ref()`, remove one of these methods. pub fn strip_reference(&self) -> Self { - self.derived(self.ty.strip_reference().clone()) + self.derived(self.ty.strip_reference()) } pub fn is_unknown(&self) -> bool { - self.ty.is_unknown() + self.ty.is_ty_error() } /// Checks that particular type `ty` implements `std::future::IntoFuture` or @@ -4982,9 +5042,7 @@ impl<'db> Type<'db> { }) .or_else(|| LangItem::Future.resolve_trait(db, self.env.krate))?; - let canonical_ty = - Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(Interner) }; - if !method_resolution::implements_trait_unique(&canonical_ty, db, &self.env, trait_) { + if !traits::implements_trait_unique(self.ty, db, self.env.clone(), trait_) { return None; } @@ -5012,9 +5070,7 @@ impl<'db> Type<'db> { let Some(iterator_trait) = LangItem::Iterator.resolve_trait(db, self.env.krate) else { return false; }; - let canonical_ty = - Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(Interner) }; - method_resolution::implements_trait_unique(&canonical_ty, db, &self.env, iterator_trait) + traits::implements_trait_unique(self.ty, db, self.env.clone(), iterator_trait) } /// Resolves the projection `<Self as IntoIterator>::IntoIter` and returns the resulting type @@ -5027,9 +5083,7 @@ impl<'db> Type<'db> { }, )?; - let canonical_ty = - Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(Interner) }; - if !method_resolution::implements_trait_unique(&canonical_ty, db, &self.env, trait_) { + if !traits::implements_trait_unique(self.ty, db, self.env.clone(), trait_) { return None; } @@ -5049,36 +5103,18 @@ impl<'db> Type<'db> { None => return false, }; - let canonical_ty = - Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(Interner) }; - method_resolution::implements_trait_unique(&canonical_ty, db, &self.env, fnonce_trait) + traits::implements_trait_unique(self.ty, db, self.env.clone(), fnonce_trait) } // FIXME: Find better API that also handles const generics pub fn impls_trait(&self, db: &'db dyn HirDatabase, trait_: Trait, args: &[Type<'db>]) -> bool { - let mut it = args.iter().map(|t| t.ty.clone()); - let trait_ref = TyBuilder::trait_ref(db, trait_.id) - .push(self.ty.clone()) - .fill(|x| { - match x { - ParamKind::Type => { - it.next().unwrap_or_else(|| TyKind::Error.intern(Interner)).cast(Interner) - } - ParamKind::Const(ty) => { - // FIXME: this code is not covered in tests. - unknown_const_as_generic(ty.clone()) - } - ParamKind::Lifetime => error_lifetime().cast(Interner), - } - }) - .build(); - - let goal = Canonical { - value: hir_ty::InEnvironment::new(&self.env.env, trait_ref.cast(Interner)), - binders: CanonicalVarKinds::empty(Interner), - }; - - db.trait_solve(self.env.krate, self.env.block, goal).is_some() + let interner = DbInterner::new_with(db, None, None); + let args = generic_args_from_tys( + interner, + trait_.id.into(), + std::iter::once(self.ty).chain(args.iter().map(|ty| ty.ty)), + ); + traits::implements_trait_unique_with_args(db, self.env.clone(), trait_.id, args) } pub fn normalize_trait_assoc_type( @@ -5087,27 +5123,25 @@ impl<'db> Type<'db> { args: &[Type<'db>], alias: TypeAlias, ) -> Option<Type<'db>> { - let mut args = args.iter(); - let trait_id = match alias.id.lookup(db).container { - ItemContainerId::TraitId(id) => id, - _ => unreachable!("non assoc type alias reached in normalize_trait_assoc_type()"), - }; - let parent_subst = TyBuilder::subst_for_def(db, trait_id, None) - .push(self.ty.clone()) - .fill(|it| { - // FIXME: this code is not covered in tests. - match it { - ParamKind::Type => args.next().unwrap().ty.clone().cast(Interner), - ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()), - ParamKind::Lifetime => error_lifetime().cast(Interner), - } - }) - .build(); + let interner = DbInterner::new_with(db, Some(self.env.krate), self.env.block); + let args = generic_args_from_tys( + interner, + alias.id.into(), + std::iter::once(self.ty).chain(args.iter().map(|ty| ty.ty)), + ); // FIXME: We don't handle GATs yet. - let projection = TyBuilder::assoc_type_projection(db, alias.id, Some(parent_subst)).build(); + let projection = Ty::new_alias( + interner, + AliasTyKind::Projection, + AliasTy::new(interner, alias.id.into(), args), + ); - let ty = db.normalize_projection(projection, self.env.clone()); - if ty.is_unknown() { None } else { Some(self.derived(ty)) } + // FIXME(next-solver): This needs to be `PostAnalysis`, but this currently causes errors due to our incorrect + // handling of opaques. `non_body_analysis()` will also cause errors (from not revealing opaques inside their + // defining places), so we choose between two bad options. + let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis()); + let ty = structurally_normalize_ty(&infcx, projection, self.env.clone()); + if ty.is_ty_error() { None } else { Some(self.derived(ty)) } } pub fn is_copy(&self, db: &'db dyn HirDatabase) -> bool { @@ -5118,27 +5152,17 @@ impl<'db> Type<'db> { } pub fn as_callable(&self, db: &'db dyn HirDatabase) -> Option<Callable<'db>> { - let callee = match self.ty.kind(Interner) { - TyKind::Closure(id, subst) => Callee::Closure(*id, subst.clone()), - TyKind::Function(_) => Callee::FnPtr, - TyKind::FnDef(..) => Callee::Def(self.ty.callable_def(db)?), - kind => { - // This will happen when it implements fn or fn mut, since we add an autoborrow adjustment - let (ty, kind) = if let TyKind::Ref(_, _, ty) = kind { - (ty, ty.kind(Interner)) - } else { - (&self.ty, kind) - }; - if let TyKind::Closure(closure, subst) = kind { - let sig = ty.callable_sig(db)?; - return Some(Callable { - ty: self.clone(), - sig, - callee: Callee::Closure(*closure, subst.clone()), - is_bound_method: false, - }); - } - let (fn_trait, sig) = hir_ty::callable_sig_from_fn_trait(ty, self.env.clone(), db)?; + let interner = DbInterner::new_with(db, None, None); + let callee = match self.ty.kind() { + TyKind::Closure(id, subst) => Callee::Closure(id.0, subst), + TyKind::CoroutineClosure(id, subst) => Callee::CoroutineClosure(id.0, subst), + TyKind::FnPtr(..) => Callee::FnPtr, + TyKind::FnDef(id, _) => Callee::Def(id.0), + // This will happen when it implements fn or fn mut, since we add an autoborrow adjustment + TyKind::Ref(_, inner_ty, _) => return self.derived(inner_ty).as_callable(db), + _ => { + let (fn_trait, sig) = + hir_ty::callable_sig_from_fn_trait(self.ty, self.env.clone(), db)?; return Some(Callable { ty: self.clone(), sig, @@ -5148,32 +5172,37 @@ impl<'db> Type<'db> { } }; - let sig = self.ty.callable_sig(db)?; + let sig = self.ty.callable_sig(interner)?; Some(Callable { ty: self.clone(), sig, callee, is_bound_method: false }) } pub fn is_closure(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Closure { .. }) + matches!(self.ty.kind(), TyKind::Closure { .. }) } - pub fn as_closure(&self) -> Option<Closure> { - match self.ty.kind(Interner) { - TyKind::Closure(id, subst) => Some(Closure { id: *id, subst: subst.clone() }), + pub fn as_closure(&self) -> Option<Closure<'db>> { + match self.ty.kind() { + TyKind::Closure(id, subst) => { + Some(Closure { id: AnyClosureId::ClosureId(id.0), subst }) + } + TyKind::CoroutineClosure(id, subst) => { + Some(Closure { id: AnyClosureId::CoroutineClosureId(id.0), subst }) + } _ => None, } } pub fn is_fn(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::FnDef(..) | TyKind::Function { .. }) + matches!(self.ty.kind(), TyKind::FnDef(..) | TyKind::FnPtr { .. }) } pub fn is_array(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Array(..)) + matches!(self.ty.kind(), TyKind::Array(..)) } pub fn is_packed(&self, db: &'db dyn HirDatabase) -> bool { - let adt_id = match *self.ty.kind(Interner) { - TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, + let adt_id = match self.ty.kind() { + TyKind::Adt(adt_def, ..) => adt_def.def_id().0, _ => return false, }; @@ -5185,61 +5214,28 @@ impl<'db> Type<'db> { } pub fn is_raw_ptr(&self) -> bool { - matches!(self.ty.kind(Interner), TyKind::Raw(..)) + matches!(self.ty.kind(), TyKind::RawPtr(..)) } pub fn remove_raw_ptr(&self) -> Option<Type<'db>> { - if let TyKind::Raw(_, ty) = self.ty.kind(Interner) { - Some(self.derived(ty.clone())) - } else { - None - } + if let TyKind::RawPtr(ty, _) = self.ty.kind() { Some(self.derived(ty)) } else { None } } pub fn contains_unknown(&self) -> bool { - // FIXME: When we get rid of `ConstScalar::Unknown`, we can just look at precomputed - // `TypeFlags` in `TyData`. - return go(&self.ty); - - fn go(ty: &Ty) -> bool { - match ty.kind(Interner) { - TyKind::Error => true, - - TyKind::Adt(_, substs) - | TyKind::AssociatedType(_, substs) - | TyKind::Tuple(_, substs) - | TyKind::OpaqueType(_, substs) - | TyKind::FnDef(_, substs) - | TyKind::Closure(_, substs) => { - substs.iter(Interner).filter_map(|a| a.ty(Interner)).any(go) - } - - TyKind::Array(_ty, len) if len.is_unknown() => true, - TyKind::Array(ty, _) - | TyKind::Slice(ty) - | TyKind::Raw(_, ty) - | TyKind::Ref(_, _, ty) => go(ty), - - TyKind::Scalar(_) - | TyKind::Str - | TyKind::Never - | TyKind::Placeholder(_) - | TyKind::BoundVar(_) - | TyKind::InferenceVar(_, _) - | TyKind::Dyn(_) - | TyKind::Function(_) - | TyKind::Alias(_) - | TyKind::Foreign(_) - | TyKind::Coroutine(..) - | TyKind::CoroutineWitness(..) => false, - } - } + self.ty.references_non_lt_error() } pub fn fields(&self, db: &'db dyn HirDatabase) -> Vec<(Field, Self)> { - let (variant_id, substs) = match self.ty.kind(Interner) { - TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), substs) => ((*s).into(), substs), - TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), substs) => ((*u).into(), substs), + let interner = DbInterner::new_with(db, None, None); + let (variant_id, substs) = match self.ty.kind() { + TyKind::Adt(adt_def, substs) => { + let id = match adt_def.def_id().0 { + AdtId::StructId(id) => id.into(), + AdtId::UnionId(id) => id.into(), + AdtId::EnumId(_) => return Vec::new(), + }; + (id, substs) + } _ => return Vec::new(), }; @@ -5247,37 +5243,35 @@ impl<'db> Type<'db> { .iter() .map(|(local_id, ty)| { let def = Field { parent: variant_id.into(), id: local_id }; - let ty = ty.clone().substitute(Interner, substs); + let ty = ty.instantiate(interner, substs); (def, self.derived(ty)) }) .collect() } pub fn tuple_fields(&self, _db: &'db dyn HirDatabase) -> Vec<Self> { - if let TyKind::Tuple(_, substs) = &self.ty.kind(Interner) { - substs - .iter(Interner) - .map(|ty| self.derived(ty.assert_ty_ref(Interner).clone())) - .collect() + if let TyKind::Tuple(substs) = self.ty.kind() { + substs.iter().map(|ty| self.derived(ty)).collect() } else { Vec::new() } } pub fn as_array(&self, db: &'db dyn HirDatabase) -> Option<(Self, usize)> { - if let TyKind::Array(ty, len) = &self.ty.kind(Interner) { - try_const_usize(db, len).map(|it| (self.derived(ty.clone()), it as usize)) + if let TyKind::Array(ty, len) = self.ty.kind() { + try_const_usize(db, len).map(|it| (self.derived(ty), it as usize)) } else { None } } pub fn fingerprint_for_trait_impl(&self) -> Option<TyFingerprint> { - TyFingerprint::for_trait_impl(&self.ty) + TyFingerprint::for_trait_impl(self.ty) } - pub(crate) fn canonical(&self) -> Canonical<Ty> { - hir_ty::replace_errors_with_variables(&self.ty) + pub(crate) fn canonical(&self, db: &'db dyn HirDatabase) -> Canonical<'db, Ty<'db>> { + let interner = DbInterner::new_with(db, None, None); + hir_ty::replace_errors_with_variables(interner, &self.ty) } /// Returns types that this type dereferences to (including this type itself). The returned @@ -5289,9 +5283,10 @@ impl<'db> Type<'db> { self.autoderef_(db).map(move |ty| self.derived(ty)) } - fn autoderef_(&self, db: &'db dyn HirDatabase) -> impl Iterator<Item = Ty> { + fn autoderef_(&self, db: &'db dyn HirDatabase) -> impl Iterator<Item = Ty<'db>> { + let interner = DbInterner::new_with(db, None, None); // There should be no inference vars in types passed here - let canonical = hir_ty::replace_errors_with_variables(&self.ty); + let canonical = hir_ty::replace_errors_with_variables(interner, &self.ty); autoderef(db, self.env.clone(), canonical) } @@ -5317,14 +5312,15 @@ impl<'db> Type<'db> { krate: Crate, callback: &mut dyn FnMut(AssocItemId) -> bool, ) { - let def_crates = match method_resolution::def_crates(db, &self.ty, krate.id) { + let ty_ns = self.ty; + let def_crates = match method_resolution::def_crates(db, ty_ns, krate.id) { Some(it) => it, None => return, }; for krate in def_crates { let impls = db.inherent_impls_in_crate(krate); - for impl_def in impls.for_self_ty(&self.ty) { + for impl_def in impls.for_self_ty(ty_ns) { for &(_, item) in impl_def.impl_items(db).items.iter() { if callback(item) { return; @@ -5353,15 +5349,13 @@ impl<'db> Type<'db> { /// - "U" /// ``` pub fn type_arguments(&self) -> impl Iterator<Item = Type<'db>> + '_ { - self.ty - .strip_references() - .as_adt() - .map(|(_, substs)| substs) - .or_else(|| self.ty.strip_references().as_tuple()) - .into_iter() - .flat_map(|substs| substs.iter(Interner)) - .filter_map(|arg| arg.ty(Interner).cloned()) - .map(move |ty| self.derived(ty)) + match self.ty.strip_references().kind() { + TyKind::Adt(_, substs) => Either::Left(substs.types().map(move |ty| self.derived(ty))), + TyKind::Tuple(substs) => { + Either::Right(Either::Left(substs.iter().map(move |ty| self.derived(ty)))) + } + _ => Either::Right(Either::Right(std::iter::empty())), + } } /// Iterates its type and const arguments @@ -5391,15 +5385,13 @@ impl<'db> Type<'db> { .strip_references() .as_adt() .into_iter() - .flat_map(|(_, substs)| substs.iter(Interner)) - .filter_map(move |arg| { - // arg can be either a `Ty` or `constant` - if let Some(ty) = arg.ty(Interner) { - Some(format_smolstr!("{}", ty.display(db, display_target))) - } else { - arg.constant(Interner) - .map(|const_| format_smolstr!("{}", const_.display(db, display_target))) + .flat_map(|(_, substs)| substs.iter()) + .filter_map(move |arg| match arg { + GenericArg::Ty(ty) => Some(format_smolstr!("{}", ty.display(db, display_target))), + GenericArg::Const(const_) => { + Some(format_smolstr!("{}", const_.display(db, display_target))) } + GenericArg::Lifetime(_) => None, }) } @@ -5516,8 +5508,9 @@ impl<'db> Type<'db> { ?name, ) .entered(); + let interner = DbInterner::new_with(db, None, None); // There should be no inference vars in types passed here - let canonical = hir_ty::replace_errors_with_variables(&self.ty); + let canonical = hir_ty::replace_errors_with_variables(interner, &self.ty); let krate = scope.krate(); let environment = scope @@ -5604,7 +5597,8 @@ impl<'db> Type<'db> { } } - let canonical = hir_ty::replace_errors_with_variables(&self.ty); + let interner = DbInterner::new_with(db, None, None); + let canonical = hir_ty::replace_errors_with_variables(interner, &self.ty); let krate = scope.krate(); let environment = scope @@ -5652,7 +5646,7 @@ impl<'db> Type<'db> { pub fn env_traits(&self, db: &'db dyn HirDatabase) -> impl Iterator<Item = Trait> { let _p = tracing::info_span!("env_traits").entered(); self.autoderef_(db) - .filter(|ty| matches!(ty.kind(Interner), TyKind::Placeholder(_))) + .filter(|ty| matches!(ty.kind(), TyKind::Param(_))) .flat_map(|ty| { self.env .traits_in_scope_from_clauses(ty) @@ -5663,132 +5657,66 @@ impl<'db> Type<'db> { pub fn as_impl_traits(&self, db: &'db dyn HirDatabase) -> Option<impl Iterator<Item = Trait>> { self.ty.impl_trait_bounds(db).map(|it| { - it.into_iter().filter_map(|pred| match pred.skip_binders() { - hir_ty::WhereClause::Implemented(trait_ref) => { - Some(Trait::from(trait_ref.hir_trait_id())) - } + it.into_iter().filter_map(|pred| match pred.kind().skip_binder() { + ClauseKind::Trait(trait_ref) => Some(Trait::from(trait_ref.def_id().0)), _ => None, }) }) } pub fn as_associated_type_parent_trait(&self, db: &'db dyn HirDatabase) -> Option<Trait> { - self.ty.associated_type_parent_trait(db).map(Into::into) + let TyKind::Alias(AliasTyKind::Projection, alias) = self.ty.kind() else { return None }; + match alias.def_id.expect_type_alias().loc(db).container { + ItemContainerId::TraitId(id) => Some(Trait { id }), + _ => None, + } } - fn derived(&self, ty: Ty) -> Self { - Type { env: self.env.clone(), ty, _pd: PhantomCovariantLifetime::new() } + fn derived(&self, ty: Ty<'db>) -> Self { + Type { env: self.env.clone(), ty } } - /// Visits every type, including generic arguments, in this type. `cb` is called with type + /// Visits every type, including generic arguments, in this type. `callback` is called with type /// itself first, and then with its generic arguments. - pub fn walk(&self, db: &'db dyn HirDatabase, mut cb: impl FnMut(Type<'db>)) { - fn walk_substs<'db>( - db: &'db dyn HirDatabase, - type_: &Type<'db>, - substs: &Substitution, - cb: &mut impl FnMut(Type<'db>), - ) { - for ty in substs.iter(Interner).filter_map(|a| a.ty(Interner)) { - walk_type(db, &type_.derived(ty.clone()), cb); - } - } - - fn walk_bounds<'db>( + pub fn walk(&self, db: &'db dyn HirDatabase, callback: impl FnMut(Type<'db>)) { + struct Visitor<'db, F> { db: &'db dyn HirDatabase, - type_: &Type<'db>, - bounds: &[QuantifiedWhereClause], - cb: &mut impl FnMut(Type<'db>), - ) { - for pred in bounds { - if let WhereClause::Implemented(trait_ref) = pred.skip_binders() { - cb(type_.clone()); - // skip the self type. it's likely the type we just got the bounds from - if let [self_ty, params @ ..] = trait_ref.substitution.as_slice(Interner) { - for ty in - params.iter().filter(|&ty| ty != self_ty).filter_map(|a| a.ty(Interner)) - { - walk_type(db, &type_.derived(ty.clone()), cb); - } - } - } - } + env: Arc<TraitEnvironment<'db>>, + callback: F, + visited: FxHashSet<Ty<'db>>, } + impl<'db, F> TypeVisitor<DbInterner<'db>> for Visitor<'db, F> + where + F: FnMut(Type<'db>), + { + type Result = (); - fn walk_type<'db>( - db: &'db dyn HirDatabase, - type_: &Type<'db>, - cb: &mut impl FnMut(Type<'db>), - ) { - let ty = type_.ty.strip_references(); - match ty.kind(Interner) { - TyKind::Adt(_, substs) => { - cb(type_.derived(ty.clone())); - walk_substs(db, type_, substs, cb); - } - TyKind::AssociatedType(_, substs) => { - if ty.associated_type_parent_trait(db).is_some() { - cb(type_.derived(ty.clone())); - } - walk_substs(db, type_, substs, cb); - } - TyKind::OpaqueType(_, subst) => { - if let Some(bounds) = ty.impl_trait_bounds(db) { - walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); - } - - walk_substs(db, type_, subst, cb); - } - TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { - if let Some(bounds) = ty.impl_trait_bounds(db) { - walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); - } - - walk_substs(db, type_, &opaque_ty.substitution, cb); - } - TyKind::Placeholder(_) => { - cb(type_.derived(ty.clone())); - if let Some(bounds) = ty.impl_trait_bounds(db) { - walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); - } - } - TyKind::Dyn(bounds) => { - walk_bounds( - db, - &type_.derived(ty.clone()), - bounds.bounds.skip_binders().interned(), - cb, - ); + fn visit_ty(&mut self, ty: Ty<'db>) -> Self::Result { + if !self.visited.insert(ty) { + return; } - TyKind::Ref(_, _, ty) - | TyKind::Raw(_, ty) - | TyKind::Array(ty, _) - | TyKind::Slice(ty) => { - walk_type(db, &type_.derived(ty.clone()), cb); - } + (self.callback)(Type { env: self.env.clone(), ty }); - TyKind::FnDef(_, substs) - | TyKind::Tuple(_, substs) - | TyKind::Closure(.., substs) => { - walk_substs(db, type_, substs, cb); - } - TyKind::Function(hir_ty::FnPointer { substitution, .. }) => { - walk_substs(db, type_, &substitution.0, cb); + if let Some(bounds) = ty.impl_trait_bounds(self.db) { + bounds.visit_with(self); } - _ => {} + ty.super_visit_with(self); } } - walk_type(db, self, &mut cb); + let mut visitor = + Visitor { db, env: self.env.clone(), callback, visited: FxHashSet::default() }; + self.ty.visit_with(&mut visitor); } /// Check if type unifies with another type. /// /// Note that we consider placeholder types to unify with everything. /// For example `Option<T>` and `Option<U>` unify although there is unresolved goal `T = U`. pub fn could_unify_with(&self, db: &'db dyn HirDatabase, other: &Type<'db>) -> bool { - let tys = hir_ty::replace_errors_with_variables(&(self.ty.clone(), other.ty.clone())); + let interner = DbInterner::new_with(db, None, None); + let tys = hir_ty::replace_errors_with_variables(interner, &(self.ty, other.ty)); hir_ty::could_unify(db, self.env.clone(), &tys) } @@ -5797,39 +5725,90 @@ impl<'db> Type<'db> { /// This means that placeholder types are not considered to unify if there are any bounds set on /// them. For example `Option<T>` and `Option<U>` do not unify as we cannot show that `T = U` pub fn could_unify_with_deeply(&self, db: &'db dyn HirDatabase, other: &Type<'db>) -> bool { - let tys = hir_ty::replace_errors_with_variables(&(self.ty.clone(), other.ty.clone())); + let interner = DbInterner::new_with(db, None, None); + let tys = hir_ty::replace_errors_with_variables(interner, &(self.ty, other.ty)); hir_ty::could_unify_deeply(db, self.env.clone(), &tys) } pub fn could_coerce_to(&self, db: &'db dyn HirDatabase, to: &Type<'db>) -> bool { - let tys = hir_ty::replace_errors_with_variables(&(self.ty.clone(), to.ty.clone())); + let interner = DbInterner::new_with(db, None, None); + let tys = hir_ty::replace_errors_with_variables(interner, &(self.ty, to.ty)); hir_ty::could_coerce(db, self.env.clone(), &tys) } - pub fn as_type_param(&self, db: &'db dyn HirDatabase) -> Option<TypeParam> { - match self.ty.kind(Interner) { - TyKind::Placeholder(p) => Some(TypeParam { - id: TypeParamId::from_unchecked(hir_ty::from_placeholder_idx(db, *p)), - }), + pub fn as_type_param(&self, _db: &'db dyn HirDatabase) -> Option<TypeParam> { + match self.ty.kind() { + TyKind::Param(param) => Some(TypeParam { id: param.id }), _ => None, } } /// Returns unique `GenericParam`s contained in this type. pub fn generic_params(&self, db: &'db dyn HirDatabase) -> FxHashSet<GenericParam> { - hir_ty::collect_placeholders(&self.ty, db) + hir_ty::collect_params(&self.ty) .into_iter() .map(|id| TypeOrConstParam { id }.split(db).either_into()) .collect() } pub fn layout(&self, db: &'db dyn HirDatabase) -> Result<Layout, LayoutError> { - db.layout_of_ty(self.ty.clone(), self.env.clone()) + db.layout_of_ty(self.ty, self.env.clone()) .map(|layout| Layout(layout, db.target_data_layout(self.env.krate).unwrap())) } pub fn drop_glue(&self, db: &'db dyn HirDatabase) -> DropGlue { - db.has_drop_glue(self.ty.clone(), self.env.clone()) + let interner = DbInterner::new_with(db, Some(self.env.krate), self.env.block); + // FIXME: This should be `PostAnalysis` I believe. + let infcx = interner.infer_ctxt().build(TypingMode::non_body_analysis()); + hir_ty::drop::has_drop_glue(&infcx, self.ty, self.env.clone()) + } +} + +#[derive(Clone, PartialEq, Eq, Debug, Hash)] +pub struct TypeNs<'db> { + env: Arc<TraitEnvironment<'db>>, + ty: Ty<'db>, +} + +impl<'db> TypeNs<'db> { + fn new(db: &'db dyn HirDatabase, lexical_env: impl HasResolver, ty: Ty<'db>) -> Self { + let resolver = lexical_env.resolver(db); + let environment = resolver + .generic_def() + .map_or_else(|| TraitEnvironment::empty(resolver.krate()), |d| db.trait_environment(d)); + TypeNs { env: environment, ty } + } + + pub fn to_type(&self, _db: &'db dyn HirDatabase) -> Type<'db> { + Type { env: self.env.clone(), ty: self.ty } + } + + // FIXME: Find better API that also handles const generics + pub fn impls_trait(&self, infcx: InferCtxt<'db>, trait_: Trait, args: &[TypeNs<'db>]) -> bool { + let args = GenericArgs::new_from_iter( + infcx.interner, + [self.ty].into_iter().chain(args.iter().map(|t| t.ty)).map(|t| t.into()), + ); + let trait_ref = hir_ty::next_solver::TraitRef::new(infcx.interner, trait_.id.into(), args); + + let pred_kind = rustc_type_ir::Binder::dummy(rustc_type_ir::PredicateKind::Clause( + rustc_type_ir::ClauseKind::Trait(rustc_type_ir::TraitPredicate { + trait_ref, + polarity: rustc_type_ir::PredicatePolarity::Positive, + }), + )); + let predicate = hir_ty::next_solver::Predicate::new(infcx.interner, pred_kind); + let goal = hir_ty::next_solver::Goal::new( + infcx.interner, + hir_ty::next_solver::ParamEnv::empty(), + predicate, + ); + let res = hir_ty::traits::next_trait_solve_in_ctxt(&infcx, goal); + res.map_or(false, |res| matches!(res.1, rustc_type_ir::solve::Certainty::Yes)) + } + + pub fn is_bool(&self) -> bool { + matches!(self.ty.kind(), rustc_type_ir::TyKind::Bool) } } @@ -5857,39 +5836,43 @@ impl InlineAsmOperand { #[derive(Debug)] pub struct Callable<'db> { ty: Type<'db>, - sig: CallableSig, - callee: Callee, + sig: PolyFnSig<'db>, + callee: Callee<'db>, /// Whether this is a method that was called with method call syntax. is_bound_method: bool, } #[derive(Clone, PartialEq, Eq, Hash, Debug)] -enum Callee { +enum Callee<'db> { Def(CallableDefId), - Closure(ClosureId, Substitution), + Closure(InternedClosureId, GenericArgs<'db>), + CoroutineClosure(InternedCoroutineId, GenericArgs<'db>), FnPtr, FnImpl(FnTrait), } -pub enum CallableKind { +pub enum CallableKind<'db> { Function(Function), TupleStruct(Struct), TupleEnumVariant(Variant), - Closure(Closure), + Closure(Closure<'db>), FnPtr, FnImpl(FnTrait), } impl<'db> Callable<'db> { - pub fn kind(&self) -> CallableKind { + pub fn kind(&self) -> CallableKind<'db> { match self.callee { Callee::Def(CallableDefId::FunctionId(it)) => CallableKind::Function(it.into()), Callee::Def(CallableDefId::StructId(it)) => CallableKind::TupleStruct(it.into()), Callee::Def(CallableDefId::EnumVariantId(it)) => { CallableKind::TupleEnumVariant(it.into()) } - Callee::Closure(id, ref subst) => { - CallableKind::Closure(Closure { id, subst: subst.clone() }) + Callee::Closure(id, subst) => { + CallableKind::Closure(Closure { id: AnyClosureId::ClosureId(id), subst }) + } + Callee::CoroutineClosure(id, subst) => { + CallableKind::Closure(Closure { id: AnyClosureId::CoroutineClosureId(id), subst }) } Callee::FnPtr => CallableKind::FnPtr, Callee::FnImpl(fn_) => CallableKind::FnImpl(fn_), @@ -5901,25 +5884,31 @@ impl<'db> Callable<'db> { _ => return None, }; let func = Function { id: func }; - Some((func.self_param(db)?, self.ty.derived(self.sig.params()[0].clone()))) + Some(( + func.self_param(db)?, + self.ty.derived(self.sig.skip_binder().inputs_and_output.inputs()[0]), + )) } pub fn n_params(&self) -> usize { - self.sig.params().len() - if self.is_bound_method { 1 } else { 0 } + self.sig.skip_binder().inputs_and_output.inputs().len() + - if self.is_bound_method { 1 } else { 0 } } pub fn params(&self) -> Vec<Param<'db>> { self.sig - .params() + .skip_binder() + .inputs_and_output + .inputs() .iter() .enumerate() .skip(if self.is_bound_method { 1 } else { 0 }) - .map(|(idx, ty)| (idx, self.ty.derived(ty.clone()))) + .map(|(idx, ty)| (idx, self.ty.derived(*ty))) .map(|(idx, ty)| Param { func: self.callee.clone(), idx, ty }) .collect() } pub fn return_type(&self) -> Type<'db> { - self.ty.derived(self.sig.ret().clone()) + self.ty.derived(self.sig.skip_binder().output()) } - pub fn sig(&self) -> &CallableSig { + pub fn sig(&self) -> impl Eq { &self.sig } @@ -5937,7 +5926,7 @@ impl Layout { } pub fn align(&self) -> u64 { - self.0.align.abi.bytes() + self.0.align.bytes() } pub fn niches(&self) -> Option<u128> { @@ -6244,12 +6233,6 @@ impl HasCrate for Trait { } } -impl HasCrate for TraitAlias { - fn krate(&self, db: &dyn HirDatabase) -> Crate { - self.module(db).krate() - } -} - impl HasCrate for Static { fn krate(&self, db: &dyn HirDatabase) -> Crate { self.module(db).krate() @@ -6343,12 +6326,6 @@ impl HasContainer for Trait { } } -impl HasContainer for TraitAlias { - fn container(&self, db: &dyn HirDatabase) -> ItemContainer { - ItemContainer::Module(Module { id: self.id.lookup(db).container }) - } -} - impl HasContainer for ExternBlock { fn container(&self, db: &dyn HirDatabase) -> ItemContainer { ItemContainer::Module(Module { id: self.id.lookup(db).container }) @@ -6482,3 +6459,38 @@ pub fn resolve_absolute_path<'a, I: Iterator<Item = Symbol> + Clone + 'a>( fn as_name_opt(name: Option<impl AsName>) -> Name { name.map_or_else(Name::missing, |name| name.as_name()) } + +fn generic_args_from_tys<'db>( + interner: DbInterner<'db>, + def_id: SolverDefId, + args: impl IntoIterator<Item = Ty<'db>>, +) -> GenericArgs<'db> { + let mut args = args.into_iter(); + GenericArgs::for_item(interner, def_id, |_, id, _| { + if matches!(id, GenericParamId::TypeParamId(_)) + && let Some(arg) = args.next() + { + arg.into() + } else { + next_solver::GenericArg::error_from_id(interner, id) + } + }) +} + +fn has_non_default_type_params(db: &dyn HirDatabase, generic_def: GenericDefId) -> bool { + let params = db.generic_params(generic_def); + let defaults = db.generic_defaults(generic_def); + params + .iter_type_or_consts() + .filter(|(_, param)| matches!(param, TypeOrConstParamData::TypeParamData(_))) + .map(|(local_id, _)| TypeOrConstParamId { parent: generic_def, local_id }) + .any(|param| { + let Some(param) = hir_ty::param_idx(db, param) else { + return false; + }; + defaults.get(param).is_none() + }) +} + +pub use hir_ty::next_solver; +pub use hir_ty::setup_tracing; |