Skip to content

rustc hangs when resolving recursive trait impls with undeclared generic types #143018

@sososopy

Description

@sososopy

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

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