Skip to content

hrtb + infer types break auto traits with return type notation  #109924

Open

Description

@compiler-errors

I tried this code:

#![feature(async_fn_in_trait)]
#![feature(return_type_notation)]

trait Foo {
    async fn bar(&self);
}

struct Bar;
impl Foo for Bar {
    async fn bar(&self) {}
}

fn build<T>(_: T) where T: Foo<bar(..): Send> {}

fn main() {
    build(Bar);
}

I expected it to compile, instead:

error[E0277]: `impl Future<Output = ()>` cannot be sent between threads safely
  --> src/main.rs:16:11
   |
16 |     build(Bar);
   |     ----- ^^^ `impl Future<Output = ()>` cannot be sent between threads safely
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `for<'a> Send` is not implemented for `impl Future<Output = ()>`
note: required by a bound in `build`
  --> src/main.rs:13:41
   |
13 | fn build<T>(_: T) where T: Foo<bar(..): Send> {}
   |                                         ^^^^ required by this bound in `build`

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

This error message is not super clear, but it's essentially equvialent to this code:

trait Foo {
    type Bar<'a> where Self: 'a;

    fn bar(&self) -> Self::Bar<'_>;
}

struct Bar;
impl Foo for Bar {
    type Bar<'a> = ();

    fn bar(&self) {}
}

fn build<T>(_: T) where T: Foo, for<'a> T::Bar<'a>: Send {}

fn main() {
    build(Bar);
}

Which results in:

error[E0277]: `<_ as Foo>::Bar<'a>` cannot be sent between threads safely
  --> src/main.rs:17:11
   |
17 |     build(Bar);
   |     ----- ^^^ `<_ as Foo>::Bar<'a>` cannot be sent between threads safely
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `for<'a> Send` is not implemented for `<_ as Foo>::Bar<'a>`
note: required by a bound in `build`
  --> src/main.rs:14:53
   |
14 | fn build<T>(_: T) where T: Foo, for<'a> T::Bar<'a>: Send {}
   |                                                     ^^^^ required by this bound in `build`

So this is related to other higher-ranked projection normalization problems.

Activity

  1. added
    C-bug
    F-return_type_notation
    on Apr 4, 2023
  2. changed the title [-]Higher-ranked types + return type notation [/-] [+]Auto trait issue with return type notation [/+] on Apr 4, 2023
  3. changed the title [-]Auto trait issue with return type notation [/-] [+]hrtb + infer types break auto traits with return type notation [/+] on Apr 4, 2023
  4. added
    T-compiler
    on Apr 5, 2023
  5. compiler-errors commented on Sep 11, 2023

    @compiler-errors
    ContributorAuthor

    I've put up a PR that improves the explanation of this error: #115625

    And one that makes the RTN future a bit easier to understand: #115624

  6. added a commit that references this issue on Oct 14, 2023
  7. added
    A-auto-traits
    A-higher-ranked
    on Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-auto-traitsA-higher-rankedC-bugF-return_type_notationT-compiler

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions