Skip to content

async fn presence affects an unrelated error message #66312

Closed

Description

@tguser402

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

Activity

  1. added
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Nov 11, 2019
  2. jonas-schievink commented on Nov 11, 2019

    @jonas-schievink
    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.

  3. Centril commented on Nov 12, 2019

    @Centril
    Contributor

    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

  4. emilyalbini commented on Nov 12, 2019

    @emilyalbini
    Member

    @rustbot modify labels: beta-nominated stable-nominated

  5. added
    beta-nominatedNominated for backporting to the compiler in the beta channel.
    stable-nominatedNominated for backporting to the compiler in the stable channel.
    on Nov 12, 2019
  6. Centril commented on Nov 12, 2019

    @Centril
    Contributor

    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? :)

  7. nikomatsakis commented on Nov 12, 2019

    @nikomatsakis
    Contributor
  8. 9 remaining items

  9. tguser402 commented on Dec 24, 2019

    @tguser402
    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
    
    
  10. nikomatsakis commented on Jan 8, 2020

    @nikomatsakis
    Contributor

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

  11. nikomatsakis commented on Feb 7, 2020

    @nikomatsakis
    Contributor

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

  12. added 2 commits that reference this issue on Mar 25, 2020
  13. added a commit that references this issue on Mar 25, 2020
  14. added a commit that references this issue on Mar 25, 2020
  15. added a commit that references this issue on Mar 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitA-trait-systemArea: Trait systemAsyncAwait-PolishAsync-await issues that are part of the "polish" areaAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions