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

Closed
tguser402 opened this issue Nov 11, 2019 · 10 comments · Fixed by #70376
Closed

async fn presence affects an unrelated error message #66312

tguser402 opened this issue Nov 11, 2019 · 10 comments · Fixed by #70376
Assignees
Labels
A-async-await Area: Async & Await A-traits Area: Trait system AsyncAwait-Polish Async-await issues that are part of the "polish" area AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tguser402
Copy link

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 jonas-schievink added A-async-await Area: Async & Await A-traits Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-nominated labels Nov 11, 2019
@jonas-schievink
Copy link
Contributor

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
Copy link
Contributor

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 High priority label Nov 12, 2019
@pietroalbini
Copy link
Member

@rustbot modify labels: beta-nominated stable-nominated

@rustbot rustbot added beta-nominated Nominated for backporting to the compiler in the beta channel. stable-nominated Nominated for backporting to the compiler in the stable channel. labels Nov 12, 2019
@Centril
Copy link
Contributor

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
Copy link
Contributor

@rustbot assign @csmoe

@nikomatsakis nikomatsakis added AsyncAwait-Polish Async-await issues that are part of the "polish" area AsyncAwait-OnDeck AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. and removed I-nominated labels Nov 12, 2019
@pnkfelix
Copy link
Member

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

@pnkfelix pnkfelix removed beta-nominated Nominated for backporting to the compiler in the beta channel. stable-nominated Nominated for backporting to the compiler in the stable channel. labels Nov 14, 2019
@nikomatsakis
Copy link
Contributor

@rfcbot claim

I'll take a look, since @csmoe is finding this to be mighty tricky to track down

@tguser402
Copy link
Author

I managed to get similar symptoms without async but with #![feature(arbitrary_self_types)]. But this time the trait compiles.

#![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

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

@nikomatsakis
Copy link
Contributor

I am 99% sure that the diagnosis I posted on #67651 also applies here.

@nikomatsakis
Copy link
Contributor

The async case is fixed by #68884, I think we should break out the arbitrary self types case into a distinct bug.

@tmandry tmandry assigned tmandry and unassigned nikomatsakis Mar 24, 2020
Centril added a commit to Centril/rust that referenced this issue Mar 25, 2020
Centril added a commit to Centril/rust that referenced this issue Mar 25, 2020
Centril added a commit to Centril/rust that referenced this issue Mar 25, 2020
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 25, 2020
Rollup of 7 pull requests

Successful merges:

 - rust-lang#70331 (Increase verbosity when using update syntax with private fields)
 - rust-lang#70349 (move `hir_id_validation` to `rustc_passes` + simplify `hir::map` code)
 - rust-lang#70361 (Update backtrace crate to 0.3.46)
 - rust-lang#70364 (resolve: Remove `rustc_attrs` as a standalone feature gate)
 - rust-lang#70369 (Fix smaller issues with invalid placeholder type errors)
 - rust-lang#70373 (normalize some imports & prefer direct ones)
 - rust-lang#70376 (Add test for rust-lang#66312)

Failed merges:

r? @ghost
@bors bors closed this as completed in 1a21c28 Mar 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-traits Area: Trait system AsyncAwait-Polish Async-await issues that are part of the "polish" area AsyncAwait-Triaged Async-await issues that have been triaged during a working group meeting. C-bug Category: This is a bug. P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

9 participants