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 | 252 |
1 files changed, 131 insertions, 121 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 951728617d..a029ed4031 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -416,15 +416,10 @@ impl_from!( for ModuleDef ); -impl From<Variant> for ModuleDef { - fn from(var: Variant) -> Self { - match var { - Variant::Struct(t) => Adt::from(t).into(), - Variant::Union(t) => Adt::from(t).into(), - Variant::EnumVariant(t) => t.into(), - } - } -} +impl_from!( + Variant { Struct => Adt, Union => Adt, EnumVariant => EnumVariant } + for ModuleDef +); impl ModuleDef { pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { @@ -706,7 +701,7 @@ impl Module { self, db: &dyn HirDatabase, visible_from: Option<Module>, - ) -> Vec<(Name, ScopeDef)> { + ) -> Vec<(Name, ScopeDef<'_>)> { self.id.def_map(db)[self.id] .scope .entries() @@ -1367,18 +1362,18 @@ pub struct Field { } #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)] -pub struct TupleField { - pub owner: InferBodyId, +pub struct TupleField<'db> { + pub owner: InferBodyId<'db>, pub tuple: TupleId, pub index: u32, } -impl TupleField { +impl<'db> TupleField<'db> { pub fn name(&self) -> Name { Name::new_tuple_field(self.index as usize) } - pub fn ty<'db>(&self, db: &'db dyn HirDatabase) -> Type<'db> { + pub fn ty(&self, db: &'db dyn HirDatabase) -> Type<'db> { let interner = DbInterner::new_no_crate(db); let ty = InferenceResult::of(db, self.owner) .tuple_field_access_type(self.tuple) @@ -1703,7 +1698,7 @@ impl EnumVariant { self.source(db)?.value.const_arg()?.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()) } @@ -1870,21 +1865,24 @@ impl Variant { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct AnonConst { - id: AnonConstId, +pub struct AnonConst<'db> { + id: AnonConstId<'db>, } -impl AnonConst { +impl<'db> AnonConst<'db> { pub fn owner(self, db: &dyn HirDatabase) -> ExpressionStoreOwner { self.id.loc(db).owner.into() } - pub fn ty<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> { + pub fn ty(self, db: &'db dyn HirDatabase) -> Type<'db> { let loc = self.id.loc(db); Type { owner: self.id.into(), ty: loc.ty.get() } } - pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst<'_>, ConstEvalError> { + pub fn eval( + self, + db: &'db dyn HirDatabase, + ) -> Result<EvaluatedConst<'db>, ConstEvalError<'db>> { let interner = DbInterner::new_no_crate(db); let ty = self.id.loc(db).ty.get().instantiate_identity().skip_norm_wip(); db.anon_const_eval(self.id, GenericArgs::empty(interner), None).map(|it| EvaluatedConst { @@ -1896,9 +1894,9 @@ impl AnonConst { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum InferBody { +pub enum InferBody<'db> { Body(DefWithBody), - AnonConst(AnonConst), + AnonConst(AnonConst<'db>), } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -1920,19 +1918,14 @@ impl From<DefWithBody> for ExpressionStoreOwner { } } -impl From<ExpressionStoreOwnerId> for ExpressionStoreOwner { - fn from(v: ExpressionStoreOwnerId) -> Self { - match v { - ExpressionStoreOwnerId::Signature(generic_def_id) => { - Self::Signature(generic_def_id.into()) - } - ExpressionStoreOwnerId::Body(def_with_body_id) => Self::Body(def_with_body_id.into()), - ExpressionStoreOwnerId::VariantFields(variant_id) => { - Self::VariantFields(variant_id.into()) - } - } +impl_from!( + ExpressionStoreOwnerId { + Signature => Signature, + Body => Body, + VariantFields => VariantFields, } -} + for ExpressionStoreOwner +); impl ExpressionStoreOwner { pub fn module(self, db: &dyn HirDatabase) -> Module { @@ -1996,7 +1989,7 @@ impl DefWithBody { } #[deprecated = "you should really not use this, this is exported for analysis-stats only"] - pub fn run_mir_body(self, db: &dyn HirDatabase) -> Result<(), MirLowerError> { + pub fn run_mir_body(self, db: &dyn HirDatabase) -> Result<(), MirLowerError<'_>> { let Some(id) = self.id() else { return Ok(()) }; db.mir_body(id.into()).map(drop) } @@ -2375,7 +2368,7 @@ impl Function { } } - fn fn_sig<'db>(self, db: &'db dyn HirDatabase) -> (TypeOwnerId, PolyFnSig<'db>) { + fn fn_sig<'db>(self, db: &'db dyn HirDatabase) -> (TypeOwnerId<'db>, PolyFnSig<'db>) { let fn_ptr = self.fn_ptr_type(db); let TyKind::FnPtr(sig_tys, hdr) = fn_ptr.ty.skip_binder().kind() else { unreachable!(); @@ -2383,7 +2376,7 @@ impl Function { (fn_ptr.owner, sig_tys.with(hdr)) } - fn erased_fn_sig<'db>(self, db: &'db dyn HirDatabase) -> (TypeOwnerId, FnSig<'db>) { + fn erased_fn_sig<'db>(self, db: &'db dyn HirDatabase) -> (TypeOwnerId<'db>, FnSig<'db>) { let (owner, sig) = self.fn_sig(db); let sig = DbInterner::new_no_crate(db).instantiate_bound_regions_with_erased(sig); (owner, sig) @@ -2646,7 +2639,7 @@ impl Function { self, db: &dyn HirDatabase, span_formatter: impl Fn(FileId, TextRange) -> String, - ) -> Result<String, ConstEvalError> { + ) -> Result<String, ConstEvalError<'_>> { let AnyFunctionId::FunctionId(id) = self.id else { return Err(ConstEvalError::MirEvalError(MirEvalError::NotSupported( "evaluation of builtin derive impl methods is not supported".to_owned(), @@ -2739,7 +2732,7 @@ impl<'db> Param<'db> { Some(self.as_local(db)?.name(db)) } - pub fn as_local(&self, db: &dyn HirDatabase) -> Option<Local> { + pub fn as_local(&self, db: &'db dyn HirDatabase) -> Option<Local<'db>> { match self.func { Callee::Def(CallableDefId::FunctionId(it)) => { let parent = DefWithBodyId::FunctionId(it); @@ -2926,7 +2919,7 @@ impl Const { } /// Evaluate the constant. - pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst<'_>, ConstEvalError> { + pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst<'_>, ConstEvalError<'_>> { let interner = DbInterner::new_no_crate(db); let ty = db.value_ty(self.id.into()).unwrap().instantiate_identity().skip_norm_wip(); db.const_eval(self.id, GenericArgs::empty(interner), None).map(|it| EvaluatedConst { @@ -2944,7 +2937,7 @@ impl HasVisibility for Const { } pub struct EvaluatedConst<'db> { - def: InferBodyId, + def: InferBodyId<'db>, allocation: hir_ty::next_solver::Allocation<'db>, ty: Ty<'db>, } @@ -2954,7 +2947,7 @@ impl<'db> EvaluatedConst<'db> { format!("{}", self.allocation.display(db, display_target)) } - pub fn render_debug(&self, db: &'db dyn HirDatabase) -> Result<String, MirEvalError> { + pub fn render_debug(&self, db: &'db dyn HirDatabase) -> Result<String, MirEvalError<'db>> { let ty = self.allocation.ty.kind(); if let TyKind::Int(_) | TyKind::Uint(_) = ty { let b = &self.allocation.memory; @@ -3007,7 +3000,7 @@ impl Static { } /// Evaluate the static initializer. - pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst<'_>, ConstEvalError> { + pub fn eval(self, db: &dyn HirDatabase) -> Result<EvaluatedConst<'_>, ConstEvalError<'_>> { let ty = db.value_ty(self.id.into()).unwrap().instantiate_identity().skip_norm_wip(); db.const_eval_static(self.id).map(|it| EvaluatedConst { allocation: it, @@ -3475,17 +3468,21 @@ impl From<Macro> for ItemInNs { } } -impl From<ModuleDef> for ItemInNs { - fn from(module_def: ModuleDef) -> Self { - match module_def { - ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => { - ItemInNs::Values(module_def) - } - ModuleDef::Macro(it) => ItemInNs::Macros(it), - _ => ItemInNs::Types(module_def), - } - } -} +impl_from!( + ModuleDef { + Module => Types, + Function => Values, + Adt => Types, + EnumVariant => Types, + Const => Values, + Static => Values, + Trait => Types, + TypeAlias => Types, + BuiltinType => Types, + Macro => Macros, + } + for ItemInNs +); impl ItemInNs { pub fn into_module_def(self) -> ModuleDef { @@ -3828,15 +3825,7 @@ impl HasVisibility for AssocItem { } } -impl From<AssocItem> for ModuleDef { - fn from(assoc: AssocItem) -> Self { - match assoc { - AssocItem::Function(it) => ModuleDef::Function(it), - AssocItem::Const(it) => ModuleDef::Const(it), - AssocItem::TypeAlias(it) => ModuleDef::TypeAlias(it), - } - } -} +impl_from!(AssocItem { Function, Const, TypeAlias } for ModuleDef); #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] pub enum GenericDef { @@ -4011,17 +4000,21 @@ impl GenericDef { // We cannot call this `Substitution` unfortunately... #[derive(Debug)] pub struct GenericSubstitution<'db> { - owner: TypeOwnerId, + owner: TypeOwnerId<'db>, def: GenericDefId, subst: GenericArgs<'db>, } impl<'db> GenericSubstitution<'db> { - fn new(def: GenericDefId, subst: GenericArgs<'db>, owner: TypeOwnerId) -> Self { + fn new(def: GenericDefId, subst: GenericArgs<'db>, owner: TypeOwnerId<'db>) -> Self { Self { owner, def, subst } } - fn new_from_fn(def: Function, subst: GenericArgs<'db>, owner: TypeOwnerId) -> Option<Self> { + fn new_from_fn( + def: Function, + subst: GenericArgs<'db>, + owner: TypeOwnerId<'db>, + ) -> Option<Self> { match def.id { AnyFunctionId::FunctionId(def) => Some(Self::new(def.into(), subst, owner)), AnyFunctionId::BuiltinDeriveImplMethod { .. } => None, @@ -4082,18 +4075,18 @@ impl<'db> GenericSubstitution<'db> { /// A single local definition. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct Local { +pub struct Local<'db> { pub(crate) parent: ExpressionStoreOwnerId, - pub(crate) parent_infer: InferBodyId, + pub(crate) parent_infer: InferBodyId<'db>, pub(crate) binding_id: BindingId, } -pub struct LocalSource { - pub local: Local, +pub struct LocalSource<'db> { + pub local: Local<'db>, pub source: InFile<Either<ast::IdentPat, ast::SelfParam>>, } -impl LocalSource { +impl<'db> LocalSource<'db> { pub fn as_ident_pat(&self) -> Option<&ast::IdentPat> { match &self.source.value { Either::Left(it) => Some(it), @@ -4129,7 +4122,7 @@ impl LocalSource { } } -impl Local { +impl<'db> Local<'db> { pub fn is_param(self, db: &dyn HirDatabase) -> bool { // FIXME: This parses! let src = self.primary_source(db); @@ -4184,7 +4177,7 @@ impl Local { self.binding_id.into_raw().into_u32() } - pub fn ty(self, db: &dyn HirDatabase) -> Type<'_> { + pub fn ty(self, db: &'db dyn HirDatabase) -> Type<'db> { let def = self.parent; let infer = InferenceResult::of(db, self.parent_infer); let ty = infer.binding_ty(self.binding_id); @@ -4192,7 +4185,7 @@ impl Local { } /// All definitions for this local. Example: `let (a$0, _) | (_, a$0) = it;` - pub fn sources(self, db: &dyn HirDatabase) -> Vec<LocalSource> { + pub fn sources(self, db: &dyn HirDatabase) -> Vec<LocalSource<'db>> { let b; let (_, source_map) = match self.parent { ExpressionStoreOwnerId::Signature(generic_def_id) => { @@ -4233,7 +4226,7 @@ impl Local { } /// The leftmost definition for this local. Example: `let (a$0, _) | (_, a) = it;` - pub fn primary_source(self, db: &dyn HirDatabase) -> LocalSource { + pub fn primary_source(self, db: &dyn HirDatabase) -> LocalSource<'db> { let b; let (_, source_map) = match self.parent { ExpressionStoreOwnerId::Signature(generic_def_id) => { @@ -4274,13 +4267,13 @@ impl Local { } } -impl PartialOrd for Local { +impl PartialOrd for Local<'_> { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { Some(self.cmp(other)) } } -impl Ord for Local { +impl Ord for Local<'_> { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.binding_id.cmp(&other.binding_id) } @@ -4743,7 +4736,7 @@ impl Impl { pub fn all_for_trait(db: &dyn HirDatabase, trait_: Trait) -> Vec<Impl> { let module = trait_.module(db).id; let mut all = Vec::new(); - let mut handle_impls = |impls: &TraitImpls| { + let mut handle_impls = |impls: &TraitImpls<'_>| { impls.for_trait(trait_.id, |impls| match impls { Either::Left(impls) => all.extend(impls.iter().copied().map(Impl::from)), Either::Right(impls) => all.extend(impls.iter().copied().map(Impl::from)), @@ -4867,7 +4860,7 @@ impl Impl { #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct TraitRef<'db> { - owner: TypeOwnerId, + owner: TypeOwnerId<'db>, trait_ref: hir_ty::next_solver::TraitRef<'db>, } @@ -4898,15 +4891,15 @@ impl<'db> TraitRef<'db> { } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -enum AnyClosureId { - ClosureId(InternedClosureId), - CoroutineClosureId(InternedCoroutineClosureId), +enum AnyClosureId<'db> { + ClosureId(InternedClosureId<'db>), + CoroutineClosureId(InternedCoroutineClosureId<'db>), } #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Closure<'db> { - owner: TypeOwnerId, - id: AnyClosureId, + owner: TypeOwnerId<'db>, + id: AnyClosureId<'db>, subst: GenericArgs<'db>, } @@ -4961,20 +4954,20 @@ impl<'db> Closure<'db> { /// A coroutine expression, including async, generator, and async-generator coroutines. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct Coroutine { - id: InternedCoroutineId, +pub struct Coroutine<'db> { + id: InternedCoroutineId<'db>, } -impl Coroutine { +impl<'db> Coroutine<'db> { /// Returns the values captured by this coroutine. - pub fn captured_items<'db>(&self, db: &'db dyn HirDatabase) -> Vec<ClosureCapture<'db>> { + pub fn captured_items(&self, db: &'db dyn HirDatabase) -> Vec<ClosureCapture<'db>> { captured_items(db, self.id.loc(db)) } } fn captured_items<'db>( db: &'db dyn HirDatabase, - closure: InternedClosure, + closure: InternedClosure<'db>, ) -> Vec<ClosureCapture<'db>> { let InternedClosure { owner: infer_owner, expr: closure, .. } = closure; let infer = InferenceResult::of(db, infer_owner); @@ -5055,13 +5048,13 @@ impl FnTrait { #[derive(Clone, Debug, PartialEq, Eq)] pub struct ClosureCapture<'db> { owner: ExpressionStoreOwnerId, - infer_owner: InferBodyId, + infer_owner: InferBodyId<'db>, closure: ExprId, capture: &'db hir_ty::closure_analysis::CapturedPlace, } impl<'db> ClosureCapture<'db> { - pub fn local(&self) -> Local { + pub fn local(&self) -> Local<'db> { Local { parent: self.owner, parent_infer: self.infer_owner, @@ -5249,16 +5242,23 @@ impl CaptureUsageSource { } #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] -enum TypeOwnerId { +enum TypeOwnerId<'db> { GenericDefId(GenericDefId), BuiltinDeriveImplId(BuiltinDeriveImplId), - AnonConstId(AnonConstId), + AnonConstId(AnonConstId<'db>), // FIXME: What do when we unify two different crates? Currently we just randomly keep one. NoParams(base_db::Crate), } -impl_from!(GenericDefId, BuiltinDeriveImplId, AnonConstId for TypeOwnerId); -impl TypeOwnerId { +impl_from!( + impl<'db> + GenericDefId, + BuiltinDeriveImplId, + AnonConstId<'db> + for TypeOwnerId<'db> +); + +impl TypeOwnerId<'_> { fn unify(self, other: Self) -> Option<Self> { match (self, other) { (TypeOwnerId::NoParams(_), owner) => Some(owner), @@ -5324,7 +5324,7 @@ impl TypeOwnerId { /// with types of different origins will cause errors or panics. Instead, use the `instantiate` methods. #[derive(Clone, Debug)] pub struct Type<'db> { - owner: TypeOwnerId, + owner: TypeOwnerId<'db>, ty: EarlyBinder<'db, Ty<'db>>, } @@ -5519,7 +5519,7 @@ impl<'db> Type<'db> { tys: impl IntoIterator<Item: Borrow<Type<'db>>>, ) -> Self { let interner = DbInterner::new_no_crate(db); - let mut owner = None::<TypeOwnerId>; + let mut owner = None::<TypeOwnerId<'db>>; let ty = EarlyBinder::bind(Ty::new_tup_from_iter( interner, tys.into_iter().map(|ty| { @@ -5976,7 +5976,7 @@ impl<'db> Type<'db> { } /// Returns this type as a coroutine. - pub fn as_coroutine(&self) -> Option<Coroutine> { + pub fn as_coroutine(&self) -> Option<Coroutine<'db>> { match self.ty.skip_binder().kind() { TyKind::Coroutine(id, _) => Some(Coroutine { id: id.0 }), _ => None, @@ -6070,7 +6070,10 @@ impl<'db> Type<'db> { } // FIXME: We should probably remove this. - pub fn fingerprint_for_trait_impl(&self, db: &'db dyn HirDatabase) -> Option<SimplifiedType> { + pub fn fingerprint_for_trait_impl( + &self, + db: &'db dyn HirDatabase, + ) -> Option<SimplifiedType<'db>> { fast_reject::simplify_type( DbInterner::new_no_crate(db), self.ty.skip_binder(), @@ -6573,7 +6576,7 @@ impl<'db> Type<'db> { pub fn walk(&self, db: &'db dyn HirDatabase, callback: impl FnMut(Type<'db>)) { struct Visitor<'db, F> { db: &'db dyn HirDatabase, - owner: TypeOwnerId, + owner: TypeOwnerId<'db>, callback: F, visited: FxHashSet<Ty<'db>>, } @@ -6706,8 +6709,8 @@ pub struct Callable<'db> { #[derive(Clone, PartialEq, Eq, Hash, Debug)] enum Callee<'db> { Def(CallableDefId), - Closure(InternedClosureId, GenericArgs<'db>), - CoroutineClosure(InternedCoroutineClosureId, GenericArgs<'db>), + Closure(InternedClosureId<'db>, GenericArgs<'db>), + CoroutineClosure(InternedCoroutineClosureId<'db>, GenericArgs<'db>), FnPtr, FnImpl(traits::FnTrait), BuiltinDeriveImplMethod { method: BuiltinDeriveImplMethod, impl_: BuiltinDeriveImplId }, @@ -6915,17 +6918,17 @@ pub enum BindingMode { /// For IDE only #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -pub enum ScopeDef { +pub enum ScopeDef<'db> { ModuleDef(ModuleDef), GenericParam(GenericParam), ImplSelfType(Impl), AdtSelfType(Adt), - Local(Local), + Local(Local<'db>), Label(Label), Unknown, } -impl ScopeDef { +impl ScopeDef<'_> { pub fn all_items(def: PerNs) -> ArrayVec<Self, 3> { let mut items = ArrayVec::new(); @@ -6982,15 +6985,11 @@ impl ScopeDef { } } -impl From<ItemInNs> for ScopeDef { - fn from(item: ItemInNs) -> Self { - match item { - ItemInNs::Types(id) => ScopeDef::ModuleDef(id), - ItemInNs::Values(id) => ScopeDef::ModuleDef(id), - ItemInNs::Macros(id) => ScopeDef::ModuleDef(ModuleDef::Macro(id)), - } - } -} +impl_from!( + impl<'db> + ItemInNs { Types => ModuleDef, Values => ModuleDef, Macros => ModuleDef } + for ScopeDef<'db> +); #[derive(Clone, Debug, PartialEq, Eq)] pub struct Adjustment<'db> { @@ -7040,7 +7039,7 @@ pub enum PredicatePolarity { #[derive(Debug, Clone, PartialEq, Eq)] pub struct TraitPredicate<'db> { inner: hir_ty::next_solver::TraitPredicate<'db>, - owner: TypeOwnerId, + owner: TypeOwnerId<'db>, } impl<'db> TraitPredicate<'db> { @@ -7163,7 +7162,7 @@ impl HasCrate for Module { } } -impl HasCrate for AnonConst { +impl<'db> HasCrate for AnonConst<'db> { fn krate(&self, db: &dyn HirDatabase) -> Crate { hir_def::HasModule::krate(&self.id.loc(db).owner, db).into() } @@ -7285,7 +7284,6 @@ impl_has_name!( Macro, ExternAssocItem, AssocItem, - Local, DeriveHelper, ToolModule, Label, @@ -7309,7 +7307,19 @@ macro_rules! impl_has_name_no_db { }; } -impl_has_name_no_db!(TupleField, StaticLifetime, BuiltinType, BuiltinAttr); +impl_has_name_no_db!(StaticLifetime, BuiltinType, BuiltinAttr); + +impl HasName for Local<'_> { + fn name(&self, db: &dyn HirDatabase) -> Option<Name> { + (*self).name(db).into() + } +} + +impl HasName for TupleField<'_> { + fn name(&self, _db: &dyn HirDatabase) -> Option<Name> { + (*self).name().into() + } +} impl HasName for Param<'_> { fn name(&self, db: &dyn HirDatabase) -> Option<Name> { @@ -7445,10 +7455,10 @@ fn as_name_opt(name: Option<impl AsName>) -> Name { #[track_caller] fn generic_args_from_tys<'db>( interner: DbInterner<'db>, - def_id: SolverDefId, + def_id: SolverDefId<'db>, args: impl IntoIterator<Item: Borrow<Type<'db>>>, -) -> (GenericArgs<'db>, TypeOwnerId) { - let mut owner = None::<TypeOwnerId>; +) -> (GenericArgs<'db>, TypeOwnerId<'db>) { + let mut owner = None::<TypeOwnerId<'db>>; let mut args = args.into_iter(); let args = GenericArgs::for_item(interner, def_id, |_, id, _, _| { if matches!(id, GenericParamId::TypeParamId(_)) |