Skip to content

Trait "constructor" methods don't get coercions? #49356

Closed
@scottmcm

Description

@scottmcm

Working on #41414, I found this error that surprised me:

#![feature(try_trait)]
fn main() {
    let my_string = "hello".to_string();
    let _x: Result<&str, i32> = {
        std::ops::Try::from_ok(&my_string)
        //~^ type mismatch resolving `<std::result::Result<&str, i32> as std::ops::Try>::Ok == &std::string::String`
    };
}

I'd expected that it'd work, since the trait method is -> Self, and the result of the block is fully constrained, and writing it this way of course works:

<Result<&str, i32> as std::ops::Try>::from_ok(&my_string)

Of course, this may well just be a normal "mixing coercions and inference is annoying" case that people who understand inference wouldn't have expected to work 😅

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-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