Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Error message for missing return value with Result type is unclear #24889

Closed
bguiz opened this issue Apr 28, 2015 · 6 comments
Closed

Error message for missing return value with Result type is unclear #24889

bguiz opened this issue Apr 28, 2015 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bguiz
Copy link
Contributor

bguiz commented Apr 28, 2015

The error message that I get looks like this:

src/main.rs:18:9: 18:11 error: unable to infer enough type information about `_`; type annotations required [E0282]
src/main.rs: 18         Ok(Response::with( (status::Ok, payload) ));
error: aborting due to previous error

The highlight is on the leading Ok, which is misleading. The actual fix was to either precede the statement with return, or to drop the trailing ;. It would be great if the compiler were to suggest this as well, as the current error message alone is cryptic.

@bguiz
Copy link
Contributor Author

bguiz commented Apr 28, 2015

CC @huonw @michaelsproul

@huonw huonw added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 28, 2015
@huonw
Copy link
Member

huonw commented Apr 28, 2015

Code example:

fn foo() -> Result<u8, u64> {
    Ok(1);
}

fn main() {}

@bguiz
Copy link
Contributor Author

bguiz commented Apr 28, 2015

Perhaps this should trigger [E0269] instead

@michaelsproul
Copy link
Contributor

An ideal error message would be like the one generated here:

fn thing() -> u32 {
    7u32;
}

fn main() {}
test2.rs:1:1: 3:2 error: not all control paths return a value [E0269]
test2.rs:1 fn thing() -> u32 {
test2.rs:2     7u32;
test2.rs:3 }
test2.rs:2:9: 2:10 help: consider removing this semicolon:
test2.rs:2     7u32;
                   ^
error: aborting due to previous error

@steveklabnik
Copy link
Member

Triage: here's today's error, no real change:

error[E0282]: unable to infer enough type information about `_`
 --> <anon>:2:5
  |
2 |     Ok(1);
  |     ^^ cannot infer type for `_`
  |
  = note: type annotations or generic parameter binding required

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

Going to go ahead and mark this as E-needstest since it feels easy to regress on and medium-level bad if we do.

In particular, I think adding to the existing UI test src/test/ui/coercion-missing-tail-expected-type.rs is the right thing to do. The example code given here should be added to the existing UI test, the test run, and then the stderr file updated by cd-ing into src/test/ui and running the update-all-references.sh script. Given these instructions, marking as E-easy and E-mentor.

error[E0308]: mismatched types
 --> test.rs:1:29
  |
1 |   fn foo() -> Result<u8, u64> {
  |  _____________________________^
2 | |     Ok(1);
3 | | }
  | |_^ expected enum `std::result::Result`, found ()
  |
  = note: expected type `std::result::Result<u8, u64>`
             found type `()`
help: consider removing this semicolon:
 --> test.rs:2:10
  |
2 |     Ok(1);
  |          ^

error: aborting due to previous error(s)

@Mark-Simulacrum Mark-Simulacrum added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. labels May 31, 2017
MaloJaffre added a commit to MaloJaffre/rust that referenced this issue Jun 18, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jun 18, 2017
frewsxcv added a commit to frewsxcv/rust that referenced this issue Jun 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants