Skip to content

arbitrary self types causes unexpected error messages #69069

Open
@nikomatsakis

Description

@nikomatsakis

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.F-arbitrary_self_types`#![feature(arbitrary_self_types)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions