rust-lang / rust-lang/rust

Associated Type Bounds on Trait Bounds on GAT produce error

Open
#136,144 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-trait-system C-bug F-associated_type_bounds T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I hope this is a genuine bug and not just working as intended🙈

I tried this code:

trait Assoc {
	type Type;
}

trait Static: 'static {
	type Same<T>: Assoc<Type: Static>;
}

I expected to see this happen: The code compiles.
Instead, this happened: The code produces the following compile error:

error[E0310]: the associated type `<<Self as Static>::Same<T> as Assoc>::Type` may not live long enough
 --> src/lib.rs:6:28
  |
6 |     type Same<T>: Assoc<Type: Static>;
  |                               ^^^^^^
  |                               |
  |                               the associated type `<<Self as Static>::Same<T> as Assoc>::Type` must be valid for the static lifetime...
  |                               ...so that the type `<<Self as Static>::Same<T> as Assoc>::Type` will meet its required lifetime bounds...
  |
note: ...that is required by this bound
 --> src/lib.rs:5:15
  |
5 | trait Static: 'static {
  |               ^^^^^^^
help: consider adding an explicit lifetime bound
  |
6 |     type Same<T>: Assoc<Type: Static> where <<Self as Static>::Same<T> as Assoc>::Type: 'static;
  |

Note that either removing <T> or replacing it with <T: 'static> makes the error go away.

Meta

rustc --version --verbose:

rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: x86_64-unknown-linux-gnu
release: 1.84.0
LLVM version: 19.1.5

Also persists on latest nightly:

rustc 1.86.0-nightly (f85c6de55 2025-01-26)
binary: rustc
commit-hash: f85c6de55206dbee5ffedfd821df1503a7b92346
commit-date: 2025-01-26
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7

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

Reproduce the minimized example in src/lib.rs with the listed rustc versions, then trace how rustc handles the GAT associated type bound and the 'static trait bound. A fix should allow the shown trait definition to compile without the E0310 error, while preserving the existing behavior for the variants that already compile.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.