Skip to content

Type ambiguity not reported when it may exist? #21878

Closed
@alexcrichton

Description

@alexcrichton
trait Foo {}
impl Foo for () {}
impl Foo for i32 {}

struct Error;
impl Error {
    fn foo(&self) -> ! { loop {} }
}

fn bar<T: Foo>() -> Result<T, Error> { loop {} }

fn main() {
    // what type does `_` have? is it `()` or is it `i32`?
    let _ = bar().unwrap_or_else(|e| e.foo());
}

This code compiles, but I find it somewhat surprising as I have no type annotations on _ so the compiler shouldn't know what type it is (I presume it selects one of () or i32 perhaps).

This issue is motivated by docopt/docopt.rs#89 and isn't necessarily a bug per se, but it does seem somewhat surprising so I just want to make sure it is intended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemP-mediumMedium priorityT-langRelevant to the language 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