Skip to content

Impl has "stricter" requirement that is implied by existing requirement #134890

Open
@LHolten

Description

@LHolten

I tried this code:

trait Foo {
    type Assoc;

    fn bar<'x>()
    where
        Self::Assoc: 'x;
}

impl<A: Foo> Foo for Option<A> {
    type Assoc = Option<A::Assoc>;

    fn bar<'x>()
    where
        Self::Assoc: 'x,
    {
        todo!()
    }
}

I expect this to compile, but it gives the following error

error[E0276]: impl has stricter requirements than trait
  --> src/lib.rs:14:22
   |
4  | /     fn bar<'x>()
5  | |     where
6  | |         Self::Assoc: 'x;
   | |________________________- definition of `bar` from trait
...
14 |           Self::Assoc: 'x,
   |                        ^^ impl has extra requirement `<A as Foo>::Assoc: 'x`

The extra requirement <A as Foo>::Assoc: 'x that it mentions is actually implied by the real requirement Self::Assoc: 'x.
I think rust should always accept trait impls that have the exact same bounds as the trait defintion, that is why I labeled this as a bug.

Meta

rustc --version --verbose:

rustc 1.85.0-nightly (426d17342 2024-12-21)
binary: rustc
commit-hash: 426d1734238e3c5f52e935ba4f617f3a9f43b59d
commit-date: 2024-12-21
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Same result on stable

Metadata

Metadata

Labels

A-implied-boundsArea: Implied bounds / inferred outlives-boundsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-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