rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.28k stars 1.61k forks source link

Incorrect object safety hints with GAT lifetimes #18156

Closed RCoder01 closed 1 month ago

RCoder01 commented 1 month ago

rust-analyzer incorrectly hints that a trait with an associated type that is generic over a lifetime is object safe, which conflicts with rustc saying the trait is not objects safe.

rust-analyzer version: 0.4.2111-standalone (94b526fc8 2024-09-15)

rustc version: rustc 1.81.0 (eeb90cda1 2024-09-04)

editor or extension: VSCode rust-analyzer v0.4.2114 (pre-release)

code snippet to reproduce:

trait Foo {
    type Bar<'a>;
}

fn main() {
    let baz: &dyn Foo = todo!();
}

When hovering trait Foo (#17779), rust-analyzer claims // Object Safety: Yes; however, compiling this code yields this error which explicitly says trait Foo is not object safe:

https://doc.rust-lang.org/reference/items/traits.html#object-safety>
 --> src/main.rs:2:10
  |
1 | trait Foo {
  |       --- this trait cannot be made into an object...
2 |     type Bar<'a>;
  |          ^^^ ...because it contains the generic associated type `Bar`
  = help: consider moving `Bar` to another trait

error[E0038]: the trait `Foo` cannot be made into an object
 --> src/main.rs:6:14
  |
6 |     let baz: &dyn Foo = todo!();
  |              ^^^^^^^^ `Foo` cannot be made into an object
  |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
 --> src/main.rs:2:10
  |
1 | trait Foo {
  |       --- this trait cannot be made into an object...
2 |     type Bar<'a>;
  |          ^^^ ...because it contains the generic associated type `Bar`
  = help: consider moving `Bar` to another trait

For more information about this error, try `rustc --explain E0038`.
ChayimFriedman2 commented 1 month ago

@rustbot claim