Skip to content
Merged
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
3 changes: 3 additions & 0 deletions crates/hir-ty/src/dyn_compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ pub fn generics_require_sized_self(db: &dyn HirDatabase, def: GenericDefId) -> b

let interner = DbInterner::new_with(db, Some(krate), None);
let predicates = db.generic_predicates_ns(def);
// FIXME: We should use `explicit_predicates_of` here, which hasn't been implemented to
// rust-analyzer yet
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes a regression in the following test, though I think it's not a big deal for now, since having <Self> in type bounds is also reported as dyn-incompatible due to SelfRenferential but we should fix this eventually

// https://github.com/rust-lang/rust/blob/ddaf12390d3ffb7d5ba74491a48f3cd528e5d777/compiler/rustc_hir_analysis/src/collect/predicates_of.rs#L490
elaborate::elaborate(interner, predicates.iter().copied()).any(|pred| {
match pred.kind().skip_binder() {
ClauseKind::Trait(trait_pred) => {
Expand Down
3 changes: 2 additions & 1 deletion crates/hir-ty/src/dyn_compatibility/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ trait Bar<T> {
trait Baz : Bar<Self> {
}
"#,
[("Bar", vec![]), ("Baz", vec![SizedSelf, SelfReferential])],
// FIXME: We should also report `SizedSelf` here
[("Bar", vec![]), ("Baz", vec![SelfReferential])],
);
}

Expand Down
3 changes: 3 additions & 0 deletions crates/hir-ty/src/lower_nextsolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,9 @@ where
}
}

// FIXME: rustc gathers more predicates by recursing through resulting trait predicates.
// See https://github.com/rust-lang/rust/blob/76c5ed2847cdb26ef2822a3a165d710f6b772217/compiler/rustc_hir_analysis/src/collect/predicates_of.rs#L689-L715

(
GenericPredicates(predicates.is_empty().not().then(|| predicates.into())),
create_diagnostics(ctx.diagnostics),
Expand Down
23 changes: 22 additions & 1 deletion crates/hir-ty/src/next_solver/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use base_db::Crate;
use chalk_ir::{ProgramClauseImplication, SeparatorTraitRef, Variances};
use hir_def::lang_item::LangItem;
use hir_def::signatures::{FieldData, FnFlags, ImplFlags, StructFlags, TraitFlags};
use hir_def::{AdtId, BlockId, TypeAliasId, VariantId};
use hir_def::{AdtId, BlockId, GenericDefId, TypeAliasId, VariantId};
use hir_def::{AttrDefId, Lookup};
use hir_def::{CallableDefId, EnumVariantId, ItemContainerId, StructId, UnionId};
use intern::sym::non_exhaustive;
Expand Down Expand Up @@ -1334,6 +1334,13 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
.db()
.generic_predicates_ns(def_id.0.into())
.iter()
.filter(|p| match p.kind().skip_binder() {
rustc_type_ir::ClauseKind::Trait(tr) => match tr.self_ty().kind() {
rustc_type_ir::TyKind::Param(param) => param.index == 0,
_ => false,
},
_ => true,
})
.cloned()
.map(|p| (p, Span::dummy()))
.collect();
Expand All @@ -1345,10 +1352,24 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> {
self,
def_id: Self::DefId,
) -> EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>> {
fn is_self_or_assoc(ty: Ty<'_>) -> bool {
match ty.kind() {
rustc_type_ir::TyKind::Param(param) => param.index == 0,
rustc_type_ir::TyKind::Alias(rustc_type_ir::AliasTyKind::Projection, alias) => {
is_self_or_assoc(alias.self_ty())
}
_ => false,
}
}

let predicates: Vec<(Clause<'db>, Span)> = self
.db()
.generic_predicates_ns(def_id.try_into().unwrap())
.iter()
.filter(|p| match p.kind().skip_binder() {
rustc_type_ir::ClauseKind::Trait(tr) => is_self_or_assoc(tr.self_ty()),
_ => true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, that doesn't seem correct? E.g. if we got an AliasRelate we must filter it out if it's not constraining Self, otherwise trouble will happen.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just found your comment by now. Yes, seems that I missed it.
To be clear, <T as Trait>::Assoc = Ty should be filtered out conditionally when T is not Self for both explicit_super_predicates_of and explicit_implied_predicates_of, unlike <T as Trait>::Assoc: Ty, which is always filtered out for explicit_super_predicates_of
https://github.com/rust-lang/rust/blob/52618eb338609df44978b0ca4451ab7941fd1c7a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs#L525-L608

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open a followup for this once I'm back home from work 😅

})
.cloned()
.map(|p| (p, Span::dummy()))
.collect();
Expand Down
31 changes: 31 additions & 0 deletions crates/hir-ty/src/tests/regression/new_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,34 @@ fn main() {
"#]],
);
}

#[test]
fn no_infinite_loop_on_super_predicates_elaboration() {
check_infer(
r#"
//- minicore: sized
trait DimMax<Other: Dimension> {
type Output: Dimension;
}

trait Dimension: DimMax<<Self as Dimension>:: Smaller, Output = Self> {
type Smaller: Dimension;
}

fn test<T, U>(t: T)
where
T: DimMax<U>,
U: Dimension,
{
let t: <T as DimMax<U>>::Output = loop {};
}
Copy link
Member Author

@ShoyuVanilla ShoyuVanilla Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this will blow up your rust-analyzer like in #20504 😨

I feel getting better on minimizing hangs/panics on real world projects 😄
These were actually monstrously large traits like in https://docs.rs/ndarray/0.16.1/ndarray/trait.Dimension.html

"#,
expect![[r#"
182..183 't': T
230..280 '{ ... {}; }': ()
240..241 't': <T as DimMax<U>>::Output
270..277 'loop {}': !
275..277 '{}': ()
"#]],
)
}
Loading