Returning an impl Trait in associated type position results in error at point-of-use instead of point-of-definition · Issue #72614 · rust-lang/rust (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

@Nemo157

Description

@Nemo157

Example (playground):

pub trait Foo { type Bar: std::ops::AddSelf::Bar; fn bar(self) -> Self::Bar; }

impl<T: std::ops::Add> Foo for T { type Bar = T; fn bar(self) -> Self::Bar { self } }

pub fn foo() -> impl Foo<Bar = impl std::ops::Sub> { 5usize }

fn main() { foo().bar() - 4usize; }

This should error at the definition of foo since impl std::ops::Sub<usize> does not satisfy the std::ops::Add bound required by Foo::Bar. Instead it errors at the use-site in main, and if that line is deleted (e.g. if this is a pub fn in a library) there is no error.

error[E0277]: cannot add `impl std::ops::Sub<usize>` to `impl std::ops::Sub<usize>`
  --> src/main.rs🔞11
   |
18 |     foo().bar() - 4usize;
   |           ^^^ no implementation for `impl std::ops::Sub<usize> + impl std::ops::Sub<usize>`
   |
   = help: the trait `std::ops::Add` is not implemented for `impl std::ops::Sub<usize>`