Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
scottmcm opened this issue Mar 25, 2018 · 1 comment
Closed

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

scottmcm opened this issue Mar 25, 2018 · 1 comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

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 😅

@scottmcm scottmcm added A-inference Area: Type inference A-coercions Area: implicit and explicit `expr as Type` coercions labels Mar 25, 2018
@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jun 29, 2018
@cjgillot
Copy link
Contributor

cjgillot commented Mar 4, 2022

The diagnostic is now correctly normalized

error[E0271]: type mismatch resolving `<Result<&str, i32> as Try>::Output == &String`
 --> <source>:5:9
  |
5 |         std::ops::Try::from_output(&my_string)
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `str`, found struct `String`
  |
  = note: expected reference `&str`
             found reference `&String`

@cjgillot cjgillot closed this as completed Mar 4, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants