Skip to content

Incorrect object safety hints with GAT lifetimes #18156

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

Closed
RCoder01 opened this issue Sep 20, 2024 · 1 comment · Fixed by #18162
Closed

Incorrect object safety hints with GAT lifetimes #18156

RCoder01 opened this issue Sep 20, 2024 · 1 comment · Fixed by #18162
Assignees
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug

Comments

@RCoder01
Copy link

RCoder01 commented Sep 20, 2024

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 (94b526f 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`.
@RCoder01 RCoder01 added the C-bug Category: bug label Sep 20, 2024
@DropDemBits DropDemBits added the A-ty type system / type inference / traits / method resolution label Sep 21, 2024
@ChayimFriedman2
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants