rust-lang / rust-lang/rust

rustc hangs when resolving trait impl with invalid type parameters

Open
#142,863 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug fixed-by-next-solver I-hang S-has-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the reduced Rust reproducer from the issue with the reported nightly toolchain and observe the coherence_checking phase. Trace the compiler's coherence-checking path for the invalid NonExistentTrait parameters; done means the case terminates and reports the invalid identifiers without hanging, with a regression test for the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.