Skip to content

Strange behavior with duplicated supertraits and lifetimes. #158566

Description

@theemathas

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions