rust-lang / rust-lang/rust

rustc hangs when resolving recursive trait impls with undeclared generic types

Open
#143,018 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:
It may be duplicated with #142863

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

impl<V, D> Trait<U, V, D> for A<T>
where
    A<T>: Trait<NewBg, V, D>,
    B<T>: Trait<NewBg, V, D>,
{
}

impl<V, D> Trait<U, V, D> for B<T>
where
    A<T>: Trait<NewBg, V, D>,
{
}

fn impls_trait<T: Trait<U, V, D>, U, V, D>() {}
fn main() {
    impls_trait::<A<X>, _, _, _>();
}
Meta

rustc --version --verbose:

rustc 1.90.0-nightly (28f1c8079 2025-06-24)
binary: rustc
commit-hash: 28f1c807911c63f08d98e7b468cfcf15a441e34b
commit-date: 2025-06-24
host: x86_64-pc-windows-msvc
release: 1.90.0-nightly
LLVM version: 20.1.7
-Z time-passes
time:   0.000; rss:   16MB ->   17MB (   +1MB)  parse_crate    
time:   0.000; rss:   20MB ->   20MB (   +0MB)  crate_injection
time:   0.008; rss:   20MB ->   23MB (   +3MB)  expand_crate
time:   0.010; 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.023; rss:   23MB ->   41MB (  +18MB)  late_resolve_crate
time:   0.000; rss:   41MB ->   41MB (   +0MB)  resolve_main
time:   0.000; rss:   41MB ->   41MB (   +0MB)  resolve_check_unused
error[E0412]: cannot find type `U` in this scope
 --> .\src\test.rs:5:18
  |
2 | struct A<T>(T);
  | --------------- similarly named struct `A` defined here
...
5 | impl<V, D> Trait<U, V, D> for A<T>
  |                  ^
  |

error[E0412]: cannot find type `T` in this scope
 --> .\src\test.rs:5:33
  |
2 | struct A<T>(T);
  | --------------- similarly named struct `A` defined here
...
5 | impl<V, D> Trait<U, V, D> for A<T>
  |                                 ^
  |

error[E0412]: cannot find type `T` in this scope
 --> .\src\test.rs:7:7
  |
2 | struct A<T>(T);
  | --------------- similarly named struct `A` defined here
...
7 |     A<T>: Trait<NewBg, V, D>,
  |       ^
  |

error[E0412]: cannot find type `NewBg` in this scope
 --> .\src\test.rs:7:17
  |
7 |     A<T>: Trait<NewBg, V, D>,
  |                 ^^^^^ not found in this scope
  |
help: you might be missing a type parameter
  |
5 | impl<V, D, NewBg> Trait<U, V, D> for A<T>
  |          +++++++

error[E0412]: cannot find type `T` in this scope
 --> .\src\test.rs:8:7
  |
2 | struct A<T>(T);
  | --------------- similarly named struct `A` defined here
...
8 |     B<T>: Trait<NewBg, V, D>,
  |       ^
  |

error[E0412]: cannot find type `NewBg` in this scope
 --> .\src\test.rs:8:17
  |
8 |     B<T>: Trait<NewBg, V, D>,
  |                 ^^^^^ not found in this scope
  |

error[E0412]: cannot find type `U` in this scope
  --> .\src\test.rs:12:18
   |
2  | struct A<T>(T);
   | --------------- similarly named struct `A` defined here
...
12 | impl<V, D> Trait<U, V, D> for B<T>
   |                  ^
   |


error[E0412]: cannot find type `T` in this scope
  --> .\src\test.rs:12:33
   |
2  | struct A<T>(T);
   | --------------- similarly named struct `A` defined here
...
12 | impl<V, D> Trait<U, V, D> for B<T>
   |                                 ^
   |

error[E0412]: cannot find type `T` in this scope
  --> .\src\test.rs:14:7
   |
2  | struct A<T>(T);
   | --------------- similarly named struct `A` defined here
...
14 |     A<T>: Trait<NewBg, V, D>,
   |       ^
   |

error[E0412]: cannot find type `NewBg` in this scope
  --> .\src\test.rs:14:17
   |
14 |     A<T>: Trait<NewBg, V, D>,
   |                 ^^^^^ not found in this scope
   |
help: you might be missing a type parameter
   |
12 | impl<V, D, NewBg> Trait<U, V, D> for B<T>
   |          +++++++

error[E0412]: cannot find type `X` in this scope
  --> .\src\test.rs:20:21
   |
2  | struct A<T>(T);
   | --------------- similarly named struct `A` defined here
...
20 |     impls_trait::<A<X>, _, _, _>();
   |                     ^ help: a struct with a similar name exists: `A`

time:   0.266; rss:   41MB ->   41MB (   +0MB)  resolve_report_errors
time:   0.000; rss:   41MB ->   41MB (   +0MB)  resolve_postprocess
time:   0.298; rss:   23MB ->   41MB (  +19MB)  resolve_crate
time:   0.000; rss:   39MB ->   39MB (   +0MB)  write_dep_info
time:   0.000; rss:   39MB ->   39MB (   +0MB)  complete_gated_feature_checking
time:   0.000; rss:   40MB ->   40MB (   +0MB)  drop_ast
time:   0.000; rss:   40MB ->   40MB (   +0MB)  looking_for_entry_point
time:   0.000; rss:   40MB ->   40MB (   +0MB)  looking_for_derive_registrar
time:   0.000; rss:   41MB ->   41MB (   +0MB)  unused_lib_feature_checking
time:   0.004; rss:   40MB ->   41MB (   +1MB)  misc_checking_1
time:   0.003; rss:   41MB ->   46MB (   +5MB)  coherence_checking
time:   0.000; rss:   46MB ->   46MB (   +0MB)  emit_ast_lowering_delayed_lints

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 with the reported nightly toolchain and confirm the hang around recursive trait resolution and coherence checking. Trace the compiler's trait-solving path for the undeclared generic types; done means the program terminates without hanging while emitting appropriate diagnostics.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.