rust-lang / rust-lang/rust

Strange behavior with duplicated supertraits and lifetimes.

Open
#158,566 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-higher-ranked A-lifetimes A-trait-system C-bug fixed-by-next-solver T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I'm not sure if this is a bug or not.

The following code does not compile:

trait Super<'a, T> {}
trait Sub<'a, 'b>: Super<'a, i32> + Super<'b, i32> {}

Error output:

error[E0283]: type annotations needed: cannot satisfy `Self: Super<'a, i32>`
 --> src/lib.rs:2:20
  |
2 | trait Sub<'a, 'b>: Super<'a, i32> + Super<'b, i32> {}
  |                    ^^^^^^^^^^^^^^
  |
note: multiple `impl`s or `where` clauses satisfying `Self: Super<'a, i32>` found
 --> src/lib.rs:2:20
  |
2 | trait Sub<'a, 'b>: Super<'a, i32> + Super<'b, i32> {}
  |                    ^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0283`.

The following code doesn't compile:

trait Super<'a, T> {}
trait Sub<'a, 'b, T, U>: Super<'a, T> + Super<'b, U> {}
trait Lol<'a, 'b>: Sub<'a, 'b, i32, i32> {}

Error output:

error[E0283]: type annotations needed: cannot satisfy `Self: Super<'a, i32>`
 --> src/lib.rs:3:20
  |
3 | trait Lol<'a, 'b>: Sub<'a, 'b, i32, i32> {}
  |                    ^^^^^^^^^^^^^^^^^^^^^
  |
note: multiple `impl`s or `where` clauses satisfying `Self: Super<'a, i32>` found
 --> src/lib.rs:3:20
  |
3 | trait Lol<'a, 'b>: Sub<'a, 'b, i32, i32> {}
  |                    ^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `Sub`
 --> src/lib.rs:2:26
  |
2 | trait Sub<'a, 'b, T, U>: Super<'a, T> + Super<'b, U> {}
  |                          ^^^^^^^^^^^^ required by this bound in `Sub`

For more information about this error, try `rustc --explain E0283`.

The following code compiles

trait Super<'a, T> {}
trait Sub<'a, 'b, T, U>: Super<'a, T> + Super<'b, U> {}
trait Lol: for<'a, 'b> Sub<'a, 'b, i32, i32> {}
fn wut(_: &dyn Lol) {}

The following code also compiles:

trait Super<'a, T> {}
trait Sub<'a, 'b, T, U>: Super<'a, T> + Super<'b, U> {}
trait Lol: Sub<'static, 'static, i32, i32> {}
fn wut(_: &dyn Lol) {}

The following code does NOT compile:

trait Super<'a, T> {}
trait Sub<'a, 'b, T, U>: Super<'a, T> + Super<'b, U> {}
trait Lol: for<'a> Sub<'a, 'static, i32, i32> {}
fn wut(_: &dyn Lol) {}

Error output:

error[E0283]: type annotations needed: cannot satisfy `Self: Super<'static, i32>`
 --> src/lib.rs:3:12
  |
3 | trait Lol: for<'a> Sub<'a, 'static, i32, i32> {}
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: multiple `impl`s or `where` clauses satisfying `Self: Super<'static, i32>` found
 --> src/lib.rs:3:12
  |
3 | trait Lol: for<'a> Sub<'a, 'static, i32, i32> {}
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `Sub`
 --> src/lib.rs:2:41
  |
2 | trait Sub<'a, 'b, T, U>: Super<'a, T> + Super<'b, U> {}
  |                                         ^^^^^^^^^^^^ required by this bound in `Sub`

For more information about this error, try `rustc --explain E0283`.

I don't understand what the compiler is doing.

Discovered while investigating #154662.

Meta

Reproducible on the playground with rust 1.96.0.

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

No repository file or test is identified. Start by running the listed Rust 1.96.0 playground reproductions and compare the successful and failing lifetime cases, then review the related investigation in issue #154662. Done means explaining or correcting the duplicated-supertrait and lifetime behavior with a regression test covering the examples.

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
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.