Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22611 from ada4a/lt-for-interned_opaque_ty_id
internal: Add a database lifetime to `InternedOpaqueTyId`
| -rw-r--r-- | crates/hir-ty/src/db.rs | 2 | ||||
| -rw-r--r-- | crates/hir-ty/src/dyn_compatibility.rs | 4 | ||||
| -rw-r--r-- | crates/hir-ty/src/infer.rs | 2 | ||||
| -rw-r--r-- | crates/hir-ty/src/lower.rs | 8 | ||||
| -rw-r--r-- | crates/hir-ty/src/next_solver/def_id.rs | 201 | ||||
| -rw-r--r-- | crates/hir-ty/src/next_solver/infer/select.rs | 2 | ||||
| -rw-r--r-- | crates/hir-ty/src/next_solver/interner.rs | 6 | ||||
| -rw-r--r-- | crates/hir-ty/src/next_solver/solver.rs | 2 | ||||
| -rw-r--r-- | crates/hir-ty/src/next_solver/ty.rs | 2 | ||||
| -rw-r--r-- | crates/hir-ty/src/opaques.rs | 12 |
10 files changed, 80 insertions, 161 deletions
diff --git a/crates/hir-ty/src/db.rs b/crates/hir-ty/src/db.rs index 9944079cd3..2954af0b52 100644 --- a/crates/hir-ty/src/db.rs +++ b/crates/hir-ty/src/db.rs @@ -346,7 +346,7 @@ fn hir_database_is_dyn_compatible() { fn _assert_dyn_compatible(_: &dyn HirDatabase) {} } -#[salsa_macros::interned(no_lifetime, debug, revisions = usize::MAX)] +#[salsa_macros::interned(debug, revisions = usize::MAX)] #[derive(PartialOrd, Ord)] pub struct InternedOpaqueTyId { pub loc: ImplTraitId, diff --git a/crates/hir-ty/src/dyn_compatibility.rs b/crates/hir-ty/src/dyn_compatibility.rs index 3920a84639..751e1424ab 100644 --- a/crates/hir-ty/src/dyn_compatibility.rs +++ b/crates/hir-ty/src/dyn_compatibility.rs @@ -498,9 +498,9 @@ fn contains_illegal_impl_trait_in_trait<'db>( db: &'db dyn HirDatabase, sig: &EarlyBinder<'db, Binder<'db, rustc_type_ir::FnSig<DbInterner<'db>>>>, ) -> Option<MethodViolationCode> { - struct OpaqueTypeCollector(FxHashSet<InternedOpaqueTyId>); + struct OpaqueTypeCollector<'db>(FxHashSet<InternedOpaqueTyId<'db>>); - impl<'db> rustc_type_ir::TypeVisitor<DbInterner<'db>> for OpaqueTypeCollector { + impl<'db> rustc_type_ir::TypeVisitor<DbInterner<'db>> for OpaqueTypeCollector<'db> { type Result = ControlFlow<()>; fn visit_ty( diff --git a/crates/hir-ty/src/infer.rs b/crates/hir-ty/src/infer.rs index 462f81a304..5db5be0fd1 100644 --- a/crates/hir-ty/src/infer.rs +++ b/crates/hir-ty/src/infer.rs @@ -767,7 +767,7 @@ pub struct InferenceResult<'db> { pub(crate) type_of_pat: ArenaMap<PatId, StoredTy>, pub(crate) type_of_binding: ArenaMap<BindingId, StoredTy>, pub(crate) type_of_type_placeholder: FxHashMap<TypeRefId, StoredTy>, - pub(crate) type_of_opaque: FxHashMap<InternedOpaqueTyId, StoredTy>, + pub(crate) type_of_opaque: FxHashMap<InternedOpaqueTyId<'db>, StoredTy>, /// Whether there are any type-mismatching errors in the result. // FIXME: This isn't as useful as initially thought due to us falling back placeholders to diff --git a/crates/hir-ty/src/lower.rs b/crates/hir-ty/src/lower.rs index f68358af94..edc92b1e09 100644 --- a/crates/hir-ty/src/lower.rs +++ b/crates/hir-ty/src/lower.rs @@ -1267,7 +1267,11 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> { } } - fn lower_impl_trait(&mut self, def_id: InternedOpaqueTyId, bounds: &[TypeBound]) -> ImplTrait { + fn lower_impl_trait( + &mut self, + def_id: InternedOpaqueTyId<'db>, + bounds: &[TypeBound], + ) -> ImplTrait { let interner = self.interner; cov_mark::hit!(lower_rpit); let args = GenericArgs::identity_for_item(interner, def_id.into()); @@ -1526,7 +1530,7 @@ impl ImplTraitId { } } -impl InternedOpaqueTyId { +impl InternedOpaqueTyId<'_> { #[inline] pub fn predicates<'db>(self, db: &'db dyn HirDatabase) -> EarlyBinder<'db, &'db [Clause<'db>]> { self.loc(db).predicates(db) diff --git a/crates/hir-ty/src/next_solver/def_id.rs b/crates/hir-ty/src/next_solver/def_id.rs index f7d831d7fc..b122a8cddc 100644 --- a/crates/hir-ty/src/next_solver/def_id.rs +++ b/crates/hir-ty/src/next_solver/def_id.rs @@ -43,7 +43,7 @@ pub enum SolverDefId<'db> { InternedClosureId(InternedClosureId<'db>), InternedCoroutineId(InternedCoroutineId<'db>), InternedCoroutineClosureId(InternedCoroutineClosureId<'db>), - InternedOpaqueTyId(InternedOpaqueTyId), + InternedOpaqueTyId(InternedOpaqueTyId<'db>), EnumVariantId(EnumVariantId), Ctor(Ctor), } @@ -136,7 +136,7 @@ impl_from!( InternedClosureId<'db>, InternedCoroutineId<'db>, InternedCoroutineClosureId<'db>, - InternedOpaqueTyId, + InternedOpaqueTyId<'db>, EnumVariantId, Ctor for SolverDefId<'db> @@ -387,162 +387,77 @@ macro_rules! declare_id_wrapper { }; } -declare_id_wrapper!(TraitIdWrapper, TraitId); -declare_id_wrapper!(TypeAliasIdWrapper, TypeAliasId); -#[derive(Clone, Copy, PartialEq, Eq, Hash)] -pub struct ClosureIdWrapper<'db>(pub InternedClosureId<'db>); - -impl std::fmt::Debug for ClosureIdWrapper<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::fmt::Debug::fmt(&SolverDefId::from(self.0), f) - } -} - -impl<'db> From<ClosureIdWrapper<'db>> for InternedClosureId<'db> { - #[inline] - fn from(value: ClosureIdWrapper<'db>) -> InternedClosureId<'db> { - value.0 - } -} - -impl<'db> From<InternedClosureId<'db>> for ClosureIdWrapper<'db> { - #[inline] - fn from(value: InternedClosureId<'db>) -> ClosureIdWrapper<'db> { - Self(value) - } -} +/// This is similar to [`declare_id_wrapper`], but handles ids which have the `'db` lifetime. +macro_rules! declare_id_wrapper_with_lt { + ($name:ident, $wraps:ident) => { + declare_id_wrapper_with_lt!($name, $wraps, SolverDefId<'db>); + }; -impl<'db> From<ClosureIdWrapper<'db>> for SolverDefId<'db> { - #[inline] - fn from(value: ClosureIdWrapper<'db>) -> SolverDefId<'db> { - value.0.into() - } -} + ($name:ident, $wraps:ident, $local:ty) => { + declare_id_wrapper_with_lt!($name, $wraps, $local, no_try_from); -impl<'db> TryFrom<SolverDefId<'db>> for ClosureIdWrapper<'db> { - type Error = (); + impl<'db> TryFrom<SolverDefId<'db>> for $name<'db> { + type Error = (); - #[inline] - fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> { - match value { - SolverDefId::InternedClosureId(it) => Ok(Self(it)), - _ => Err(()), + #[inline] + fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> { + match value { + SolverDefId::$wraps(it) => Ok(Self(it)), + _ => Err(()), + } + } } - } -} - -impl<'db> inherent::DefId<DbInterner<'db>, SolverDefId<'db>> for ClosureIdWrapper<'db> { - fn as_local(self) -> Option<SolverDefId<'db>> { - Some(self.into()) - } - fn is_local(self) -> bool { - true - } -} - -#[derive(Clone, Copy, PartialEq, Eq, Hash)] -pub struct CoroutineIdWrapper<'db>(pub InternedCoroutineId<'db>); - -impl std::fmt::Debug for CoroutineIdWrapper<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::fmt::Debug::fmt(&SolverDefId::from(self.0), f) - } -} - -impl<'db> From<CoroutineIdWrapper<'db>> for InternedCoroutineId<'db> { - #[inline] - fn from(value: CoroutineIdWrapper<'db>) -> InternedCoroutineId<'db> { - value.0 - } -} - -impl<'db> From<InternedCoroutineId<'db>> for CoroutineIdWrapper<'db> { - #[inline] - fn from(value: InternedCoroutineId<'db>) -> CoroutineIdWrapper<'db> { - Self(value) - } -} - -impl<'db> From<CoroutineIdWrapper<'db>> for SolverDefId<'db> { - #[inline] - fn from(value: CoroutineIdWrapper<'db>) -> SolverDefId<'db> { - value.0.into() - } -} + }; -impl<'db> TryFrom<SolverDefId<'db>> for CoroutineIdWrapper<'db> { - type Error = (); + ($name:ident, $wraps:ident, $local:ty, no_try_from) => { + #[derive(Clone, Copy, PartialEq, Eq, Hash)] + pub struct $name<'db>(pub $wraps<'db>); - #[inline] - fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> { - match value { - SolverDefId::InternedCoroutineId(it) => Ok(Self(it)), - _ => Err(()), + impl std::fmt::Debug for $name<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Debug::fmt(&SolverDefId::from(self.0), f) + } } - } -} -impl<'db> inherent::DefId<DbInterner<'db>, SolverDefId<'db>> for CoroutineIdWrapper<'db> { - fn as_local(self) -> Option<SolverDefId<'db>> { - Some(self.into()) - } - fn is_local(self) -> bool { - true - } -} - -#[derive(Clone, Copy, PartialEq, Eq, Hash)] -pub struct CoroutineClosureIdWrapper<'db>(pub InternedCoroutineClosureId<'db>); - -impl std::fmt::Debug for CoroutineClosureIdWrapper<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::fmt::Debug::fmt(&SolverDefId::from(self.0), f) - } -} - -impl<'db> From<CoroutineClosureIdWrapper<'db>> for InternedCoroutineClosureId<'db> { - #[inline] - fn from(value: CoroutineClosureIdWrapper<'db>) -> InternedCoroutineClosureId<'db> { - value.0 - } -} - -impl<'db> From<InternedCoroutineClosureId<'db>> for CoroutineClosureIdWrapper<'db> { - #[inline] - fn from(value: InternedCoroutineClosureId<'db>) -> CoroutineClosureIdWrapper<'db> { - Self(value) - } -} + impl<'db> From<$name<'db>> for $wraps<'db> { + #[inline] + fn from(value: $name<'db>) -> $wraps<'db> { + value.0 + } + } -impl<'db> From<CoroutineClosureIdWrapper<'db>> for SolverDefId<'db> { - #[inline] - fn from(value: CoroutineClosureIdWrapper<'db>) -> SolverDefId<'db> { - value.0.into() - } -} + impl<'db> From<$wraps<'db>> for $name<'db> { + #[inline] + fn from(value: $wraps<'db>) -> $name<'db> { + Self(value) + } + } -impl<'db> TryFrom<SolverDefId<'db>> for CoroutineClosureIdWrapper<'db> { - type Error = (); + impl<'db> From<$name<'db>> for SolverDefId<'db> { + #[inline] + fn from(value: $name<'db>) -> SolverDefId<'db> { + value.0.into() + } + } - #[inline] - fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> { - match value { - SolverDefId::InternedCoroutineClosureId(it) => Ok(Self(it)), - _ => Err(()), + impl<'db> inherent::DefId<DbInterner<'db>, $local> for $name<'db> { + fn as_local(self) -> Option<$local> { + Some(self.into()) + } + fn is_local(self) -> bool { + true + } } - } + }; } -impl<'db> inherent::DefId<DbInterner<'db>, SolverDefId<'db>> for CoroutineClosureIdWrapper<'db> { - fn as_local(self) -> Option<SolverDefId<'db>> { - Some(self.into()) - } - fn is_local(self) -> bool { - true - } -} +declare_id_wrapper!(TraitIdWrapper, TraitId); +declare_id_wrapper!(TypeAliasIdWrapper, TypeAliasId); +declare_id_wrapper_with_lt!(ClosureIdWrapper, InternedClosureId); +declare_id_wrapper_with_lt!(CoroutineIdWrapper, InternedCoroutineId); +declare_id_wrapper_with_lt!(CoroutineClosureIdWrapper, InternedCoroutineClosureId); declare_id_wrapper!(AdtIdWrapper, AdtId); -declare_id_wrapper!(OpaqueTyIdWrapper, InternedOpaqueTyId, OpaqueTyIdWrapper); +declare_id_wrapper_with_lt!(OpaqueTyIdWrapper, InternedOpaqueTyId, OpaqueTyIdWrapper<'db>); macro_rules! declare_ty_const_pair { ( $ty_id_name:ident, $const_id_name:ident, $term_id_name:ident ) => { diff --git a/crates/hir-ty/src/next_solver/infer/select.rs b/crates/hir-ty/src/next_solver/infer/select.rs index d6f0379c11..1462f29205 100644 --- a/crates/hir-ty/src/next_solver/infer/select.rs +++ b/crates/hir-ty/src/next_solver/infer/select.rs @@ -40,7 +40,7 @@ pub enum SelectionError<'db> { /// Computing an opaque type's hidden type caused an error (e.g. a cycle error). /// We can thus not know whether the hidden type implements an auto trait, so /// we should not presume anything about it. - OpaqueTypeAutoTraitLeakageUnknown(InternedOpaqueTyId), + OpaqueTypeAutoTraitLeakageUnknown(InternedOpaqueTyId<'db>), /// Error for a `ConstArgHasType` goal ConstArgHasWrongType { ct: Const<'db>, ct_ty: Ty<'db>, expected_ty: Ty<'db> }, } diff --git a/crates/hir-ty/src/next_solver/interner.rs b/crates/hir-ty/src/next_solver/interner.rs index ab6ce31256..a7216a034c 100644 --- a/crates/hir-ty/src/next_solver/interner.rs +++ b/crates/hir-ty/src/next_solver/interner.rs @@ -894,8 +894,8 @@ impl<'db> Interner for DbInterner<'db> { type TraitAssocTyId = TraitAssocTyId; type TraitAssocConstId = TraitAssocConstId; type TraitAssocTermId = TraitAssocTermId; - type OpaqueTyId = OpaqueTyIdWrapper; - type LocalOpaqueTyId = OpaqueTyIdWrapper; + type OpaqueTyId = OpaqueTyIdWrapper<'db>; + type LocalOpaqueTyId = OpaqueTyIdWrapper<'db>; type FreeTyAliasId = FreeTyAliasId; type FreeConstAliasId = FreeConstAliasId; type FreeTermAliasId = FreeTermAliasId; @@ -2345,7 +2345,7 @@ TrivialTypeTraversalImpls! { InherentAssocTyId, InherentAssocConstId, InherentAssocTermId, - OpaqueTyIdWrapper, + OpaqueTyIdWrapper<'_>, AnyImplId, GeneralConstIdWrapper<'_>, Safety, diff --git a/crates/hir-ty/src/next_solver/solver.rs b/crates/hir-ty/src/next_solver/solver.rs index b1b3a0e0dc..5486a56581 100644 --- a/crates/hir-ty/src/next_solver/solver.rs +++ b/crates/hir-ty/src/next_solver/solver.rs @@ -130,7 +130,7 @@ impl<'db> SolverDelegate for SolverContext<'db> { fn add_item_bounds_for_hidden_type( &self, - opaque_id: OpaqueTyIdWrapper, + opaque_id: OpaqueTyIdWrapper<'_>, args: GenericArgs<'db>, param_env: ParamEnv<'db>, hidden_ty: Ty<'db>, diff --git a/crates/hir-ty/src/next_solver/ty.rs b/crates/hir-ty/src/next_solver/ty.rs index 397db9375b..05f559c034 100644 --- a/crates/hir-ty/src/next_solver/ty.rs +++ b/crates/hir-ty/src/next_solver/ty.rs @@ -176,7 +176,7 @@ impl<'db> Ty<'db> { pub fn new_opaque( interner: DbInterner<'db>, - def_id: InternedOpaqueTyId, + def_id: InternedOpaqueTyId<'db>, args: GenericArgs<'db>, ) -> Self { Ty::new_alias( diff --git a/crates/hir-ty/src/opaques.rs b/crates/hir-ty/src/opaques.rs index 1d7cd1b05d..9cb0022ca6 100644 --- a/crates/hir-ty/src/opaques.rs +++ b/crates/hir-ty/src/opaques.rs @@ -20,10 +20,10 @@ use crate::{ }, }; -pub(crate) fn opaque_types_defined_by( - db: &dyn HirDatabase, +pub(crate) fn opaque_types_defined_by<'db>( + db: &'db dyn HirDatabase, def_id: InferBodyId<'_>, - result: &mut Vec<SolverDefId<'_>>, + result: &mut Vec<SolverDefId<'db>>, ) { if let Some(func) = def_id.as_function() { // A function may define its own RPITs. @@ -79,11 +79,11 @@ pub(crate) fn opaque_types_defined_by( // FIXME: Collect opaques from `#[define_opaque]`. - fn extend_with_opaques( - db: &dyn HirDatabase, + fn extend_with_opaques<'db>( + db: &'db dyn HirDatabase, opaques: &Option<Box<StoredEarlyBinder<ImplTraits>>>, mut make_impl_trait: impl FnMut(ImplTraitIdx) -> ImplTraitId, - result: &mut Vec<SolverDefId<'_>>, + result: &mut Vec<SolverDefId<'db>>, ) { if let Some(opaques) = opaques { for (opaque_idx, _) in (**opaques).as_ref().skip_binder().impl_traits.iter() { |