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

arbitrary self types causes unexpected error messages #69069

Open
nikomatsakis opened this issue Feb 11, 2020 · 3 comments
Open

arbitrary self types causes unexpected error messages #69069

nikomatsakis opened this issue Feb 11, 2020 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. F-arbitrary_self_types `#![feature(arbitrary_self_types)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

This example:

#![feature(arbitrary_self_types)]
trait Test<T: core::ops::Deref<Target = Self>> {
    fn is_some(self: T);
}

fn f() {
    let x = Some(2);
    if x.is_some() {
        println!("Some");
    }
}

Playground link

gives the error

error[E0277]: the trait bound `std::option::Option<{integer}>: std::ops::Deref` is not satisfied
 --> src/lib.rs:8:10
  |
8 |     if x.is_some() {
  |          ^^^^^^^ the trait `std::ops::Deref` is not implemented for `std::option::Option<{integer}>`

error[E0308]: mismatched types
 --> src/lib.rs:8:8
  |
8 |     if x.is_some() {
  |        ^^^^^^^^^^^ expected `bool`, found `()`

error: aborting due to 2 previous errors

rather than an error because the Test trait is not implemented.

This is presumably just a case where improved diagnostics would be good. The inherent is_some method winds up not triggering because it requires auto-ref rather than being passed "by value".

Originally posted by @tguser402 in #66312 (comment)

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. F-arbitrary_self_types `#![feature(arbitrary_self_types)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 11, 2020
@estebank
Copy link
Contributor

This shouldn't be marked as [requires-nightly] because the same problem happens with Pin, which is stable.

@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Feb 12, 2020
@estebank
Copy link
Contributor

Cc #66289

@estebank
Copy link
Contributor

estebank commented Aug 4, 2023

We currently provide a suggestion that is missing parens to be fully correct:

error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): the trait bound `Option<{integer}>: Deref` is not satisfied
 --> src/lib.rs:8:10
  |
8 |     if x.is_some() {
  |          ^^^^^^^ the trait `Deref` is not implemented for `Option<{integer}>`
  |
note: required by a bound in `Test::is_some`
 --> src/lib.rs:2:15
  |
2 | trait Test<T: core::ops::Deref<Target = Self>> {
  |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Test::is_some`
3 |     fn is_some(self: T);
  |        ------- required by a bound in this associated function
help: consider borrowing here
  |
8 |     if &x.is_some() {
  |        +
8 |     if &mut x.is_some() {
  |        ++++

error[[E0308]](https://doc.rust-lang.org/nightly/error_codes/E0308.html): mismatched types
 --> src/lib.rs:8:8
  |
8 |     if x.is_some() {
  |        ^^^^^^^^^^^ expected `bool`, found `()`

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. F-arbitrary_self_types `#![feature(arbitrary_self_types)]` 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

3 participants