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

async fn presence affects an unrelated error message #66312

Open
tguser402 opened this issue Nov 11, 2019 · 6 comments

Comments

@tguser402
Copy link

@tguser402 tguser402 commented Nov 11, 2019

The following code

trait Test<T> {
    fn is_some(self: T);
}

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

produces an expected error:

error[E0307]: invalid `self` parameter type: T
 --> src/lib.rs:2:22
  |
2 |     fn is_some(self: T);
  |                      ^
  |
  = note: type of `self` must be `Self` or a type that dereferences to it
  = help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

If I make f async,

trait Test<T> {
    fn is_some(self: T);
}

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

the original error is replaced with this:

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

Meta

rustc --version --verbose:

rustc 1.39.0 (4560ea788 2019-11-04)
binary: rustc
commit-hash: 4560ea788cb760f0a34127156c78e2552949f734
commit-date: 2019-11-04
host: x86_64-unknown-linux-gnu
release: 1.39.0
LLVM version: 9.0
@jonas-schievink

This comment has been minimized.

Copy link
Member

@jonas-schievink jonas-schievink commented Nov 11, 2019

Nominating because:

  • The trait impl does not exist, the compiler hallucinated it into existence
  • Inherent methods are supposed to take precedence over trait methods

This might indicate a quite serious bug in the compiler.

@Centril

This comment has been minimized.

Copy link
Member

@Centril Centril commented Nov 12, 2019

I would suggest stable- and beta-nominating the fix for this PR; it seems like this could warrant a point release. cc @rust-lang/release @rust-lang/lang

@Centril Centril added the P-high label Nov 12, 2019
@pietroalbini

This comment has been minimized.

Copy link
Member

@pietroalbini pietroalbini commented Nov 12, 2019

@rustbot modify labels: beta-nominated stable-nominated

@Centril

This comment has been minimized.

Copy link
Member

@Centril Centril commented Nov 12, 2019

Phew; looks like precedence works correctly in non-error code at least:

trait Test {
    fn is_some(&self) -> u8;
}

impl<T> Test for Option<T> {
    fn is_some(&self) -> u8 {
        self.is_some() as u8
    }
}

async fn f() {
    let x = Some(2);
    let _: () = x.is_some(); // type is bool.
}

Crisis averted? :)

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

@nikomatsakis nikomatsakis commented Nov 12, 2019

@rustbot assign @csmoe

@pnkfelix

This comment has been minimized.

Copy link
Member

@pnkfelix pnkfelix commented Nov 14, 2019

unnominating for back ports; we don't backport nominate issues, just Pull Requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.