Skip to content

Hang from infinitely-self-recursive trait alias #133901

Closed

Description

@ym-xie

I tried this code:

#![feature(trait_alias)]
#![feature(impl_trait_in_assoc_type)]
trait Foo {
    type Bar: Baz<Self, Self>;
    fn bar(&self) -> Self::Bar;
}
struct X;
impl Foo for X {
    type Bar = impl Baz<Self, Self>;
    fn bar(&self) -> Self::Bar {
        |x| x
    }
}
trait Baz<A: ?Sized, B: ?Sized> = Baz(&A) -> &B;
fn main() {}

It mutates from the following file, but the following code will not cause a hang.

#![feature(trait_alias)]
#![feature(impl_trait_in_assoc_type)]

trait Foo {
    type Bar: Baz<Self, Self>;

    fn bar(&self) -> Self::Bar;
}

struct X;

impl Foo for X {
    type Bar = impl Baz<Self, Self>;

    fn bar(&self) -> Self::Bar {
        |x| x
    }
}

trait Baz<A: ?Sized, B: ?Sized> = Fn(&A) -> &B;

fn main() {}

Also, I used impl_trait_in_assoc_type and Baz as search keywords but didn’t find any related issues with open.

Meta

rustc --version --verbose:

rustc 1.85.0-nightly (acabb5248 2024-12-04)
binary: rustc
commit-hash: acabb5248231987ae1f0c215208d1005a5db402d
commit-date: 2024-12-04
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.5
Backtrace

<backtrace>

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 5, 2024
  2. cyrgani commented on Dec 5, 2024

    @cyrgani
    Contributor

    Reduced:

    trait Foo {
        type Bar: Baz<()>;
    }
    impl Foo for () {
        type Bar = ();
    }
    trait Baz<A> = Baz<&'static A>;
  3. theemathas commented on Dec 5, 2024

    @theemathas
    Contributor

    Minimized:

    #![feature(trait_alias)]
    fn foo<T: Baz<i32>>() {}
    trait Baz<A> = Baz<Option<A>>;

    (Hangs without producing any errors or warnings. If the foo definition is moved after the Baz definition, then there's a cycle error before the hang.)

  4. added
    F-trait_alias`#![feature(trait_alias)]`
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    I-hangIssue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    on Dec 5, 2024
  5. jieyouxu commented on Dec 5, 2024

    @jieyouxu
    Member

    Triage notes:

    trait Foo {
        type Bar: Baz<()>;
    }
    impl Foo for () {
        type Bar = ();
    }
    trait Baz<A> = Baz<&'static A>;
    

    hangs on stable (1.83) without the feature gate, which is a bit different from the other minimization which involves the feature gate

    #![feature(trait_alias)]
    fn foo<T: Baz<i32>>() {}
    trait Baz<A> = Baz<Option<A>>;
    
  6. added
    S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 5, 2024
  7. changed the title [-]Hang[/-] [+]Hang from infinitely-self-recursive trait alias[/+] on Dec 5, 2024
  8. jieyouxu commented on Dec 5, 2024

    @jieyouxu
    Member

    I feel like this probably is already reported but I can't find an earlier report.

  9. lqd commented on Dec 5, 2024

    @lqd
    Member

    I feel like this probably is already reported

    I had the same feeling, we have many reports of superficially similar looking hangs. It could be interesting to bisect this one though, because this looks like a somewhat recent regression (from 1.78).

  10. lqd commented on Dec 10, 2024

    @lqd
    Member

    bisects to nightly-2024-03-09

  11. lqd commented on Dec 10, 2024

    @lqd
    Member

    backtrace looks to be in wf checks so I'll just soft ping @oli-obk for #121500, but other PRs and rollups on that nightly also changed related codepaths in rustc_hir_analysis.

    (gdb) bt
    #0  0x00007ffff5eb7c5c in _RNvMs1E_NtNtCs19hci70W1NE_12rustc_middle2ty7contextNtB6_6TyCtxt7mk_args () from /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/librustc_driver-dd95e9de7e099d94.so
    #1  0x00007ffff3b299b7 in _RINvXsh_NtNtCs19hci70W1NE_12rustc_middle2ty12generic_argsRINtNtB8_4list7RawListuNtB6_10GenericArgEINtNtCsf5lKsw415YT_13rustc_type_ir4fold12TypeFoldableNtNtB8_7context6TyCtxtE13try_fold_withNtNtNtCsga6qfZtIyPw_21rustc_trait_selection6traits9normalize19AssocTypeNormalizerECshAiFD47KJB4_18rustc_hir_analysis ()
       from /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/librustc_driver-dd95e9de7e099d94.so
    #2  0x00007ffff397225d in _RINvXNvNtCsf5lKsw415YT_13rustc_type_ir14predicate_kinds4_1__INtB5_13PredicateKindNtNtNtCs19hci70W1NE_12rustc_middle2ty7context6TyCtxtEINtNtB7_4fold12TypeFoldableB1h_E13try_fold_withNtNtNtCsga6qfZtIyPw_21rustc_trait_selection6traits9normalize19AssocTypeNormalizerECshAiFD47KJB4_18rustc_hir_analysis ()
       from /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/librustc_driver-dd95e9de7e099d94.so
    #3  0x00007ffff39c8fce in _RINvXsw_NtNtCs19hci70W1NE_12rustc_middle2ty16structural_implsNtNtB8_9predicate9PredicateINtNtCsf5lKsw415YT_13rustc_type_ir4fold17TypeSuperFoldableNtNtB8_7context6TyCtxtE19try_super_fold_withNtNtNtCsga6qfZtIyPw_21rustc_trait_selection6traits9normalize19AssocTypeNormalizerECshAiFD47KJB4_18rustc_hir_analysis ()
       from /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/librustc_driver-dd95e9de7e099d94.so
    #4  0x00007ffff3a95bfc in _RINvNtNtCsga6qfZtIyPw_21rustc_trait_selection6traits9normalize20normalize_with_depthNtNtNtCs19hci70W1NE_12rustc_middle2ty9predicate9PredicateECshAiFD47KJB4_18rustc_hir_analysis ()
       from /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/librustc_driver-dd95e9de7e099d94.so
    #5  0x00007ffff3aef8ec in _RINvXs0_NtNtCsga6qfZtIyPw_21rustc_trait_selection6traits9normalizeNtNtNtCsfBarEMO41MX_11rustc_infer5infer2at2AtNtB6_12NormalizeExt9normalizeNtNtNtCs19hci70W1NE_12rustc_middle2ty9predicate9PredicateECshAiFD47KJB4_18rustc_hir_analysis () from /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/librustc_driver-dd95e9de7e099d94.so
    #6  0x00007ffff3b81fd8 in _RNvNtNtCshAiFD47KJB4_18rustc_hir_analysis5check17compare_impl_item17check_type_bounds ()
       from /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/librustc_driver-dd95e9de7e099d94.so
    #7  0x00007ffff3b700a9 in _RNvNtNtCshAiFD47KJB4_18rustc_hir_analysis5check5check30check_impl_items_against_trait ()
       from /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/librustc_driver-dd95e9de7e099d94.so
    #8  0x00007ffff3b66514 in _RNvNtNtCshAiFD47KJB4_18rustc_hir_analysis5check5check15check_item_type () from /checkout/obj/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/librustc_driver-dd95e9de7e099d94.so
    #9  0x00007ffff3aa091d in _RNvNtNtCshAiFD47KJB4_18rustc_hir_analysis5check7wfcheck17check_well_formed ()  
  12. oli-obk commented on Dec 19, 2024

    @oli-obk
    Contributor

    Trying to debug locally it got stuck in the debug-assert-only validation happening in Binder::bind_with_vars 😆

  13. oli-obk commented on Dec 19, 2024

    @oli-obk
    Contributor

    Ok, so it's not an infinite recursion, just an infinite loop growing a value linearly that is then fully visited a few times. Elaboration goes as follows:

    • T: Baz<i32> -> add implied bounds
    • T: Baz<Option<A>> -> instantiate -> T: Baz<Option<i32>> -> add implied bounds
    • T: Baz<Option<Options<A>>> -> ...
  14. added a commit that references this issue on Jan 16, 2025
    99db273
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-trait_alias`#![feature(trait_alias)]`I-hangIssue: The compiler never terminates, due to infinite loops, deadlock, livelock, etc.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions