-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: Infinite loop while elaborting predicates #20654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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(); | ||
|
|
@@ -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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, that doesn't seem correct? E.g. if we got an
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 {}; | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😄 |
||
| "#, | ||
| expect![[r#" | ||
| 182..183 't': T | ||
| 230..280 '{ ... {}; }': () | ||
| 240..241 't': <T as DimMax<U>>::Output | ||
| 270..277 'loop {}': ! | ||
| 275..277 '{}': () | ||
| "#]], | ||
| ) | ||
| } | ||
There was a problem hiding this comment.
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 toSelfRenferentialbut we should fix this eventually