Skip to content

rustc hangs when resolving trait impl with invalid type parameters #142863

Open
@sososopy

Description

@sososopy

The code is generated by a fuzzer and reduced manually

I tried this code:
When implementing a trait for a generic type, replacing the expected generic parameters with undefined identifiers (eg. NonExistentTrait) causes the Rust compiler to hang during coherence checking

trait Trait<T: ?Sized, V: ?Sized, D: ?Sized> {}
struct A<T: ?Sized>(T);
struct B<T: ?Sized>(T);

trait IncompleteGuidance<T: ?Sized, V: ?Sized> {}

trait ImplGuidance<T: ?Sized, V: ?Sized> {}
impl<T: ?Sized> ImplGuidance<u32, u8> for T {}

impl<T: ?Sized, U: ?Sized, V: ?Sized, D: ?Sized> Trait<U, V, D> for A<T>
where
    T: IncompleteGuidance<U, V>,
    A<T>: Trait<U, D, V>,
    B<T>: Trait<U, V, D>,
    (): ToU8<D>,
{
}

trait ToU8<T: ?Sized> {}
impl ToU8<u8> for () {}

impl<T: ?Sized, U: ?Sized, V: ?Sized, D: ?Sized>
    Trait<NonExistentTrait, NonExistentTrait, NonExistentTrait> for B<T>
where
    T: ImplGuidance<U, V>,
    A<T>: Trait<U, V, D>,
{
}

fn impls_trait<T: ?Sized + Trait<U, V, D>, U: ?Sized, V: ?Sized, D: ?Sized>() {}
fn with_bound<X>()
{
    impls_trait::<B<X>, _, _, _>();
}
fn main() {
    with_bound::<u32>();
}

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (d4e1159b8 2025-06-21)
binary: rustc
commit-hash: d4e1159b8c97478778b09a4cc1c7adce5653b8bf
commit-date: 2025-06-21
host: x86_64-pc-windows-msvc
release: 1.89.0-nightly
LLVM version: 20.1.7

-Z time-passe

time:   0.001; rss:   16MB ->   17MB (   +1MB)  parse_crate
time:   0.000; rss:   20MB ->   20MB (   +0MB)  crate_injection
time:   0.007; rss:   20MB ->   23MB (   +3MB)  expand_crate
time:   0.008; rss:   20MB ->   23MB (   +3MB)  macro_expand_crate
time:   0.000; rss:   23MB ->   23MB (   +0MB)  maybe_building_test_harness
time:   0.000; rss:   23MB ->   23MB (   +0MB)  AST_validation
time:   0.000; rss:   23MB ->   23MB (   +0MB)  finalize_imports
time:   0.000; rss:   23MB ->   23MB (   +0MB)  compute_effective_visibilities
time:   0.000; rss:   23MB ->   23MB (   +0MB)  finalize_macro_resolutions
time:   0.012; rss:   23MB ->   36MB (  +13MB)  late_resolve_crate
time:   0.000; rss:   36MB ->   36MB (   +0MB)  resolve_main
time:   0.000; rss:   36MB ->   36MB (   +0MB)  resolve_check_unused
error[E0412]: cannot find type `NonExistentTrait` in this scope
  --> .\test.rs:23:11
   |
23 |     Trait<NonExistentTrait, NonExistentTrait, NonExistentTrait> for B<T>
   |           ^^^^^^^^^^^^^^^^ not found in this scope
   |
help: you might be missing a type parameter
   |
22 | impl<T: ?Sized, U: ?Sized, V: ?Sized, D: ?Sized, NonExistentTrait>
   |                                                ++++++++++++++++++

error[E0412]: cannot find type `NonExistentTrait` in this scope
  --> .\test.rs:23:29
   |
23 |     Trait<NonExistentTrait, NonExistentTrait, NonExistentTrait> for B<T>
   |                             ^^^^^^^^^^^^^^^^ not found in this scope
   |
help: you might be missing a type parameter
   |
22 | impl<T: ?Sized, U: ?Sized, V: ?Sized, D: ?Sized, NonExistentTrait>
   |                                                ++++++++++++++++++

error[E0412]: cannot find type `NonExistentTrait` in this scope
  --> .\test.rs:23:47
   |
23 |     Trait<NonExistentTrait, NonExistentTrait, NonExistentTrait> for B<T>
   |                                               ^^^^^^^^^^^^^^^^ not found in this scope
   |
help: you might be missing a type parameter
   |
22 | impl<T: ?Sized, U: ?Sized, V: ?Sized, D: ?Sized, NonExistentTrait>
   |                                                ++++++++++++++++++

time:   0.048; rss:   36MB ->   37MB (   +0MB)  resolve_report_errors
time:   0.000; rss:   37MB ->   37MB (   +0MB)  resolve_postprocess
time:   0.068; rss:   23MB ->   37MB (  +14MB)  resolve_crate
time:   0.000; rss:   34MB ->   34MB (   +0MB)  write_dep_info
time:   0.000; rss:   34MB ->   34MB (   +0MB)  complete_gated_feature_checking
time:   0.000; rss:   35MB ->   35MB (   +0MB)  drop_ast
time:   0.000; rss:   35MB ->   35MB (   +0MB)  looking_for_entry_point
time:   0.000; rss:   35MB ->   35MB (   +0MB)  looking_for_derive_registrar
time:   0.000; rss:   36MB ->   36MB (   +0MB)  unused_lib_feature_checking
time:   0.003; rss:   35MB ->   36MB (   +1MB)  misc_checking_1
time:   0.002; rss:   36MB ->   41MB (   +5MB)  coherence_checking

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.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.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions