Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/hir-ty/src/builtin_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ pub fn impl_trait<'db>(
}
}

#[inline]
pub fn impl_super_outlives<'db>(
_interner: DbInterner<'db>,
_id: BuiltinDeriveImplId,
) -> EarlyBinder<'db, &'db [Clause<'db>]> {
// Currently no builtin-derived traits have super outlive bounds.
EarlyBinder::bind(&[])
}

#[salsa::tracked(returns(ref))]
pub fn predicates(db: &dyn HirDatabase, impl_: BuiltinDeriveImplId) -> GenericPredicates {
let loc = impl_.loc(db);
Expand Down
9 changes: 4 additions & 5 deletions crates/hir-ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ use crate::{
dyn_compatibility::DynCompatibilityViolation,
layout::{Layout, LayoutError},
lower::{
ConstParamTypes, FieldTypes, GenericDefaults, TrackedStructToken, TypeAliasBounds,
ConstParamTypes, FieldTypes, GenericDefaults, SelfAndAssocBounds, TrackedStructToken,
WithDefinedOpaques,
},
mir::{MirBody, MirLowerError},
next_solver::{
Allocation, Clause, EarlyBinder, GenericArgs, ParamEnv, PolyFnSig, StoredClauses,
StoredEarlyBinder, StoredGenericArgs, StoredPolyFnSig, StoredTraitRef, StoredTy, TraitRef,
Ty, VariancesOf,
Allocation, Clause, EarlyBinder, GenericArgs, ParamEnv, PolyFnSig, StoredEarlyBinder,
StoredGenericArgs, StoredPolyFnSig, StoredTraitRef, StoredTy, TraitRef, Ty, VariancesOf,
},
traits::{ParamEnvAndCrate, StoredParamEnvAndCrate},
};
Expand Down Expand Up @@ -319,7 +318,7 @@ pub trait HirDatabase: SourceDatabase + 'static {
fn type_alias_bounds_with_diagnostics<'db>(
&'db self,
type_alias: TypeAliasId,
) -> &'db TyLoweringResult<'db, TypeAliasBounds<StoredEarlyBinder<StoredClauses>>> {
) -> &'db TyLoweringResult<'db, SelfAndAssocBounds> {
let db = self.as_dyn();
crate::lower::type_alias_bounds_with_diagnostics(db, type_alias)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ impl<'db> HirDisplay<'db> for Ty<'db> {
));
}
let impl_trait_id = opaque_ty_id.loc(db);
let data = impl_trait_id.predicates(db);
let data = impl_trait_id.all_bounds(db);
let bounds = data
.iter_instantiated_copied(interner, alias_ty.args.as_slice())
.map(Unnormalized::skip_norm_wip)
Expand Down
52 changes: 25 additions & 27 deletions crates/hir-ty/src/dyn_compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
},
};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DynCompatibilityViolation {
SizedSelf,
SelfReferential,
Expand All @@ -42,7 +42,7 @@ pub enum DynCompatibilityViolation {
HasNonCompatibleSuperTrait(TraitId),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MethodViolationCode {
StaticMethod,
ReferencesSelfInput,
Expand Down Expand Up @@ -122,7 +122,7 @@ where
ControlFlow::Continue(())
}

#[salsa::tracked(returns(clone))]
#[salsa::tracked(returns(copy))]
pub fn dyn_compatibility_of_trait_query(
db: &dyn HirDatabase,
trait_: TraitId,
Expand All @@ -136,33 +136,31 @@ pub fn dyn_compatibility_of_trait_query(
res
}

pub fn generics_require_sized_self(db: &dyn HirDatabase, def: GenericDefId) -> bool {
let krate = def.module(db).krate(db);
#[salsa::tracked(returns(copy))]
pub fn generics_require_sized_self(db: &dyn HirDatabase, def_id: GenericDefId) -> bool {
let krate = def_id.krate(db);
let interner = DbInterner::new_with(db, krate);
let Some(sized) = interner.lang_items().Sized else {
return false;
let Some(sized_def_id) = interner.lang_items().Sized else {
return false; /* No Sized trait, can't require it! */
};

let predicates = GenericPredicates::query_explicit(db, def);
// FIXME: We should use `explicit_predicates_of` here, which hasn't been implemented to
// rust-analyzer yet
// https://github.com/rust-lang/rust/blob/ddaf12390d3ffb7d5ba74491a48f3cd528e5d777/compiler/rustc_hir_analysis/src/collect/predicates_of.rs#L490
elaborate::elaborate(interner, predicates.iter_identity().map(Unnormalized::skip_norm_wip)).any(
|pred| match pred.kind().skip_binder() {
ClauseKind::Trait(trait_pred) => {
if sized == trait_pred.def_id().0
&& let rustc_type_ir::TyKind::Param(param_ty) =
trait_pred.trait_ref.self_ty().kind()
&& param_ty.index == 0
{
true
} else {
false
}
}
_ => false,
},
)
// Search for a clause like `Self: Sized` amongst the trait bounds.
let clauses = GenericPredicates::query_own_explicit(db, def_id)
.iter_identity()
.map(Unnormalized::skip_norm_wip);
elaborate::elaborate(interner, clauses).any(|clause| match clause.kind().skip_binder() {
ClauseKind::Trait(ref trait_pred) => {
trait_pred.def_id().0 == sized_def_id && trait_pred.self_ty().is_param(0)
}
ClauseKind::RegionOutlives(_)
| ClauseKind::TypeOutlives(_)
| ClauseKind::Projection(_)
| ClauseKind::ConstArgHasType(_, _)
| ClauseKind::WellFormed(_)
| ClauseKind::ConstEvaluatable(_)
| ClauseKind::UnstableFeature(_)
| ClauseKind::HostEffect(..) => false,
})
}

// rustc gathers all the spans that references `Self` for error rendering,
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/dyn_compatibility/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ trait Bar: Sized {
fn bar<T>(&self, t: T);
}
"#,
[("Bar", vec![SizedSelf])],
[("Bar", vec![SizedSelf, Method(Generic)])],
);

check_dyn_compatibility(
Expand All @@ -221,7 +221,7 @@ trait Bar
fn bar<T>(&self, t: T);
}
"#,
[("Bar", vec![SizedSelf])],
[("Bar", vec![SizedSelf, Method(Generic)])],
);
}

Expand Down
Loading