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

Unclear error message with extra semicolon #30497

Closed
pjmlp opened this issue Dec 20, 2015 · 4 comments
Closed

Unclear error message with extra semicolon #30497

pjmlp opened this issue Dec 20, 2015 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@pjmlp
Copy link

pjmlp commented Dec 20, 2015

If I take the following line from the Rust book and make a function out of it

let secret_number = rand::thread_rng().gen_range(1, 101);

Meaning something like

let secret_number = generate_secret_number() ;
// ...
fn generate_secret_number() -> u32 {
    rand::thread_rng().gen_range(1, 101);
}

Then the compiler fails with

src/main.rs:41:1: 45:2 error: not all control paths return a value [E0269]
src/main.rs:41 fn generate_secret_number() -> u32 {
src/main.rs:42     rand::thread_rng().gen_range(1, 101);
src/main.rs:43 }
src/main.rs:41:1: 43:2 help: run `rustc --explain E0269` to see a detailed explanation
error: aborting due to previous error
Could not compile `guessing_game`.

However if I add the return statement, which should be optional. It works

let secret_number = generate_secret_number() ;
// ...
fn generate_secret_number() -> u32 {
    return rand::thread_rng().rnd.gen_range(1, 101);
}

Shouldn't this be considered an expression by the compiler?

@gereeter
Copy link
Contributor

The crucial thing here is the semicolon after your statement. A block consists of a bunch of statements, terminated with semicolons, possibly followed by an expression, which is not terminated by a semicolon. Therefore, putting the semicolon after rand::thread_rng().gen_range(1, 101) made the compiler view the expression as a statement, with no terminating expression on the block. If you leave off the semicolon, it should work:

fn generate_secret_number() -> u32 {
    rand::thread_rng().gen_range(1, 101)
}

Note the lack of a semicolon. I hope that helps!


This bug might still be useful as a documentation bug - the long explaination for E0269 does not mention this pitfall, while it seems like a common case that should be addressed.

@jonas-schievink
Copy link
Contributor

There's usually a suggestion printed telling you to remove the trailing semicolon. It only works when the type of the last statement-expression is the same as the function return type, which isn't known in this case since the return type of gen_range is inferred.

cc #29396, which attempted to fix this

@pjmlp
Copy link
Author

pjmlp commented Dec 20, 2015

I see, still getting the hang of when semicolons are required.

But maybe a clearer error message is required.

@Aatch Aatch changed the title Unreachable code in a single line function. Unclear error message with extra semicolon Dec 27, 2015
@Aatch Aatch added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 27, 2015
@Aatch
Copy link
Contributor

Aatch commented Dec 27, 2015

I updated the title to make it accurately reflect the issue. The compiler never considers the line to be unreachable either way, it just says that not all control paths return a value. Which is true, none of the 1 control paths return a value.

birkenfeld added a commit to birkenfeld/rust that referenced this issue May 2, 2016
In situations where the value of the last expression must be inferred,
rustc will not emit the "you might need to remove the semicolon" warning,
so at least note this in the extended description.

Fixes: rust-lang#30497
Manishearth added a commit to Manishearth/rust that referenced this issue May 2, 2016
…Gomez

E0269: add suggestion to check for trailing semicolons

In situations where the value of the last expression must be inferred,
rustc will not emit the "you might need to remove the semicolon" warning,
so at least note this in the extended description.

Fixes: rust-lang#30497
Manishearth added a commit to Manishearth/rust that referenced this issue May 3, 2016
…Gomez

E0269: add suggestion to check for trailing semicolons

In situations where the value of the last expression must be inferred,
rustc will not emit the "you might need to remove the semicolon" warning,
so at least note this in the extended description.

Fixes: rust-lang#30497
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
Projects
None yet
Development

No branches or pull requests

4 participants