Skip to content
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

Invalid suggest for methods in auto trait #105788

Closed
chenyukang opened this issue Dec 16, 2022 · 2 comments · Fixed by #105817
Closed

Invalid suggest for methods in auto trait #105788

chenyukang opened this issue Dec 16, 2022 · 2 comments · Fixed by #105817
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints F-auto_traits `#![feature(auto_traits)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@chenyukang
Copy link
Member

chenyukang commented Dec 16, 2022

Given the following code

#![feature(auto_traits)]

auto trait Foo {
    fn g(&self);
}

trait Bar  {
    fn f(&self) {
        self.g();
    }
}

fn main() {}

The current output is:

error[E0380]: auto traits cannot have associated items
 --> src/main.rs:4:8
  |
3 | auto trait Foo {
  |            --- auto trait cannot have associated items
4 |     fn g(&self);
  |     ---^-------- help: remove these associated items

error[E0554]: `#![feature]` may not be used on the stable release channel
 --> src/main.rs:1:1
  |
1 | #![feature(auto_traits)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: the method `g` exists for reference `&Self`, but its trait bounds were not satisfied
 --> src/main.rs:9:14
  |
9 |         self.g();
  |              ^
  |
note: trait bound `&Self: Foo` was not satisfied
  = note: the following trait bounds were not satisfied:
          `Self: Foo`
          which is required by `&Self: Foo`
  = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `g`, perhaps you need to add a supertrait for it:
  |
7 | trait Bar: Foo  {
  |          +++++

Some errors have detailed explanations: E0380, E0554, E0599.
For more information about an error, try `rustc --explain E0380`.
error: could not compile `playground` due to 3 previous errors

The second error is not reasonable, since we don't allow associated items in auto trait.
Ideally the output should look like:

error[E0599]: no method named `g` found for reference `&Self` in the current scope
 --> ./p/e.rs:9:14
  |
9 |         self.g();
  |
@chenyukang chenyukang 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 Dec 16, 2022
@chenyukang
Copy link
Member Author

@compiler-errors
I spent some time to investigate it, I tried to filter the candidates from auto trait.
But seems unsatisfied_predicates here have two predicates, and the message for the second error comes from the first TraitPredicate:

[
    (Binder(TraitPredicate(<Self as Foo>, polarity:Positive), []), Some(Binder(TraitPredicate(<&Self as Foo>, polarity:Positive), [])), Some(ObligationCause { span: ./p/e.rs:9:14: 9:15 (#0), body_id: HirId { owner: OwnerId { def_id: DefId(0:6 ~ e[8a4c]::Bar::f) }, local_id: 9 }, code: BuiltinDerivedObligation(DerivedObligationCause { parent_trait_pred: Binder(TraitPredicate(<_ as Foo>, polarity:Positive), []), parent_code: MiscObligation }) })),

    (Binder(TraitPredicate(<&Self as Foo>, polarity:Positive), []), None, Some(ObligationCause { span: ./p/e.rs:9:14: 9:15 (#0), body_id: HirId { owner: OwnerId { def_id: DefId(0:6 ~ e[8a4c]::Bar::f) }, local_id: 9 }, code: ImplDerivedObligation(ImplDerivedObligationCause { derived: DerivedObligationCause { parent_trait_pred: Binder(TraitPredicate(<_ as Foo>, polarity:Positive), []), parent_code: BuiltinDerivedObligation(DerivedObligationCause { parent_trait_pred: Binder(TraitPredicate(<_ as Foo>, polarity:Positive), []), parent_code: MiscObligation }) }, impl_def_id: DefId(0:3 ~ e[8a4c]::Foo), span: no-location (#0) }) }))
]

I'm still trying to understand the code.

@Noratrieb Noratrieb added requires-nightly This issue requires a nightly compiler in some way. F-auto_traits `#![feature(auto_traits)]` labels Dec 16, 2022
@chenyukang
Copy link
Member Author

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 27, 2022
…for-auto-trait, r=TaKO8Ki

Remove unreasonable help message for auto trait

Fixes rust-lang#105788
@bors bors closed this as completed in 90753de Dec 27, 2022
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 F-auto_traits `#![feature(auto_traits)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants