Skip to content

Associated type bound with lifetime failure #116752

Open
@jflatow

Description

I tried this code:

// does not work

pub trait P { // -> P<'x>
    type P: std::ops::Mul;
}

impl<'x, X: S + 'x, U: A<'x, X = X>> P for U { // -> P<'x>
    type P = X::S;
}

pub trait A<'x> {
    type X: 'x;
}

// works

pub trait Q {
    type Q: std::ops::Mul;
}

impl<X: S, U: B<X = X>> Q for U {
    type Q = X::S;
}

pub trait B {
    type X: S;
}

// sort of works

pub trait R {
    type R: std::ops::Mul;
}

impl<X: S, U: for<'x> C<X<'x> = X>> R for U {
    type R = X::S;
}

pub trait C {
    type X<'x>: S + 'x;
}

pub trait S {
    type S: std::ops::Mul;
}

I expected to see these all either work or give an appropriate diagnostic message.

Instead, in the first case, the error given is that X is unconstrained, which it is not according to https://rust-lang.github.io/rfcs/0447-no-unused-impl-parameters.html. The real (unreported) issue it seems to have is that 'x is unconstrained, which is more understandable, but then in this case there doesn't seem to be a way to handle this case of type bounds with GATs.

The other 2 compile as expected. The last case attempts to abstract away the lifetime from the trait using a GAT and a HRTB, but in that case X doesn't quite seem to be bound as one might hope. When I actually try to use it, I get a lifetime mismatch.

Meta

$ rustc +nightly --version --verbose
rustc 1.75.0-nightly (187b8131d 2023-10-03)
binary: rustc
commit-hash: 187b8131d4f760f856b214fce34534903276f2ef
commit-date: 2023-10-03
host: x86_64-apple-darwin
release: 1.75.0-nightly
LLVM version: 17.0.2

$ rustc +stable --version --verbose
rustc 1.72.1 (d5c2e9c34 2023-09-13)
binary: rustc
commit-hash: d5c2e9c342b358556da91d61ed4133f6f50fc0c3
commit-date: 2023-09-13
host: x86_64-apple-darwin
release: 1.72.1
LLVM version: 16.0.5

Compiler Output

error[E0207]: the type parameter `X` is not constrained by the impl trait, self type, or predicates
 --> assoc-bounds.rs:7:10
  |
7 | impl<'x, X: S + 'x, U: A<'x, X = X>> P for U { // -> P<'x>
  |          ^ unconstrained type parameter

error: aborting due to previous error

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions