Skip to content

Error in trait solving #80982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
b-naber opened this issue Jan 13, 2021 · 2 comments
Open

Error in trait solving #80982

b-naber opened this issue Jan 13, 2021 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@b-naber
Copy link
Contributor

b-naber commented Jan 13, 2021

Related to #78113. In the following example the trait system cannot resolve the trait object A in Box<dyn A + 'a> to Foo, even though Foo implements A:

#![feature(generic_associated_types)]

trait A {}

struct Foo {}

impl A for Foo {}

impl<'a> A for &'a Box<dyn A + 'a> {}

trait X {
  type T<'a> : A;
}

struct B {}

impl X for B {
  type T<'a> = &'a Box<dyn A + 'a>;
}

impl B {
  fn foo<'a>(&self, arg : &'a Box<Foo>) -> <Self as X>::T<'a> {
    arg
  }

}

fn main() {}

This yields the error:

error[E0308]: mismatched types
  --> /Users/bn/Documents/Rust/rust_error_samples/gat_type_alias_lifetime.rs:23:5
   |
23 |     arg
   |     ^^^ expected trait object `dyn A`, found struct `Foo`
   |
   = note: expected reference `&'a Box<(dyn A + 'a)>`
              found reference `&'a Box<Foo>`

error: aborting due to previous error; 1 warning emitted

If we don't provide a lifetime bound in the implementation of A for &'a Box<dyn A> (impl<'a> A for &'a Box<dyn A>) then we get an additional lifetime error since this trait impl is resolved to impl<'a> A for &'a Box<dyn A + 'static>, resulting in two errors:

error[E0308]: mismatched types
  --> /Users/bn/Documents/Rust/rust_error_samples/gat_type_alias_lifetime.rs:18:3
   |
18 |   type T<'a> = &'a Box<dyn A + 'a>;
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `A`
              found trait `A`
note: the lifetime `'a` as defined on the associated item at 18:10...
  --> /Users/bn/Documents/Rust/rust_error_samples/gat_type_alias_lifetime.rs:18:10
   |
18 |   type T<'a> = &'a Box<dyn A + 'a>;
   |          ^^
   = note: ...does not necessarily outlive the static lifetime

error[E0308]: mismatched types
  --> /Users/bn/Documents/Rust/rust_error_samples/gat_type_alias_lifetime.rs:23:5
   |
23 |     arg
   |     ^^^ expected trait object `dyn A`, found struct `Foo`
   |
   = note: expected reference `&'a Box<(dyn A + 'a)>`
              found reference `&'a Box<Foo>`

error: aborting due to 2 previous errors; 1 warning emitted
@jonas-schievink jonas-schievink added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Jan 13, 2021
@matthewjasper matthewjasper added D-confusing Diagnostics: Confusing error or lint that should be reworked. and removed F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs labels Feb 6, 2021
@matthewjasper
Copy link
Contributor

The error here is correct, there is no coercion from &'a Box<Foo> to &'a Box<(dyn A + 'a)> because there are two layers of indirection here. The diagnostic could probably be more helpful though.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 23, 2021
@estebank
Copy link
Contributor

Current output:

error[E0308]: mismatched types
  --> src/main.rs:21:5
   |
20 |   fn foo<'a>(&self, arg : &'a Box<Foo>) -> <Self as X>::T<'a> {
   |                                            ------------------ expected `&'a Box<(dyn A + 'a)>` because of return type
21 |     arg
   |     ^^^ expected `&Box<dyn A>`, found `&Box<Foo>`
   |
   = note: expected reference `&'a Box<(dyn A + 'a)>`
              found reference `&'a Box<Foo>`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants