Skip to content

Invalid ? suggestion on mismatched Ok(T) #116967

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
eopb opened this issue Oct 20, 2023 · 1 comment · Fixed by #116968
Closed

Invalid ? suggestion on mismatched Ok(T) #116967

eopb opened this issue Oct 20, 2023 · 1 comment · Fixed by #116968
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@eopb
Copy link
Contributor

eopb commented Oct 20, 2023

Code

pub fn foo() -> Result<String, ()> {
    let out: Result<(), ()> = Ok(());
    out
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
1 | pub fn foo() -> Result<String, ()> {
  |                 ------------------ expected `Result<String, ()>` because of return type
2 |     let out: Result<(), ()> = Ok(());
3 |     out
  |     ^^^ expected `Result<String, ()>`, found `Result<(), ()>`
  |
  = note: expected enum `Result<String, _>`
             found enum `Result<(), _>`
help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result`
  |
3 |     Ok(out?)
  |     +++   ++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to previous error

Desired output

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
1 | pub fn foo() -> Result<String, ()> {
  |                 ------------------ expected `Result<String, ()>` because of return type
2 |     let out: Result<(), ()> = Ok(());
3 |     out
  |     ^^^ expected `Result<String, ()>`, found `Result<(), ()>`
  |
  = note: expected enum `Result<String, _>`
             found enum `Result<(), _>`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to previous error

Rationale and extra context

The suggestion is invalid

Other cases

No response

Anything else?

No response

@eopb eopb added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 20, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 20, 2023
@eopb
Copy link
Contributor Author

eopb commented Oct 20, 2023

@rustbot claim

@bors bors closed this as completed in 934cbe4 Oct 27, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 27, 2023
Rollup merge of rust-lang#116968 - eopb:116967, r=petrochenkov

Invalid `?` suggestion on mismatched `Ok(T)`

fixes: rust-lang#116967
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 28, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants