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

Block expression should warning about missing return value #26577

Closed
hfiguiere opened this issue Jun 25, 2015 · 8 comments · Fixed by #58743
Closed

Block expression should warning about missing return value #26577

hfiguiere opened this issue Jun 25, 2015 · 8 comments · Fixed by #58743
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-feature-request Category: A feature request, i.e: not implemented / a PR. 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

@hfiguiere
Copy link

This is part of the Rust tutorial session. The whole purpose of the exercise was to find a mistake in the block expression.

See Exercise 2.1
http://pnkfelix.github.io/cyot/tutorial/exercises/ex_part_2

With rust compile this:

fn foo() -> i32 {
   42
}

pub fn main() {

    let the_sum :i32 = {
        foo();
    };

    let the_other_sum = {
        foo();
    };

}

It outputs this error:

bug_block.rs:7:24: 9:6 error: mismatched types:
 expected `i32`,
    found `()`
(expected i32,
    found ()) [E0308]
bug_block.rs:7     let the_sum :i32 = {
bug_block.rs:8         foo();
bug_block.rs:9     };
error: aborting due to previous error

It should also put a warning for line 12: "possibly missing return value". This would catch the bug.

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 25, 2015
@steveklabnik
Copy link
Member

Possibly related to #26576

@hfiguiere
Copy link
Author

It was found during the same thing. My understanding of what's happening is that the return type is being inferred so it assume that returning '()' is valid. Since I get the error when I actually specify a type for the block expression.

@hfiguiere
Copy link
Author

Here is my suggestion:

If the type of the block expression is inferred and there no return value, emit a warning.

Suggested warning removal: specify an explicit return value as

let the_other_sum = {
    foo();
    ()
};

@arielb1
Copy link
Contributor

arielb1 commented Jun 26, 2015

@hfiguiere

lints run after type-checking, so this won't help.

@pnkfelix
Copy link
Member

Another, more heavy handed approach: During parsing, if a block expression occurs in an syntactic context that is expecting a value, then do the warning (at parse time) suggesting that one put in an explicit return value.

So:

fn quux() {
  {
    hi();
  } // this is fine

  let x = {
    foo();
  }; // this causes the warning

  bar({
    foo();
  }); // this causes the warning too
}

Update: I'm not 100% sure what I would then suggest for the block that forms a closure's body; we cannot tell before type-inference whether a closure is meant to return a value or not...

@apasel422 apasel422 changed the title Block expression should warning about missin return value Block expression should warning about missing return value May 26, 2016
@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 Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 22, 2017
@memoryruins
Copy link
Contributor

Triage: the error message has improved

error[E0308]: mismatched types
 --> src/main.rs:7:24
  |
7 |       let the_sum :i32 = {
  |  ________________________^
8 | |         foo();
  | |              - help: consider removing this semicolon
9 | |     };
  | |_____^ expected i32, found ()
  |
  = note: expected type `i32`
             found type `()`

error: aborting due to previous error

rustc: 1.32.0 / rust version 1.34.0-nightly (c1c3c4e95 2019-01-29)

@hfiguiere
Copy link
Author

Yes this is so much better.

@arielb1
Copy link
Contributor

arielb1 commented Feb 6, 2019

Fixed in #57381, so soft-duplicate of #57348nope it's not, it was fixed in some other place.

So this needs a test - I coudn't find a UI test for a non-function-return.

@arielb1 arielb1 added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Feb 6, 2019
varkor added a commit to varkor/rust that referenced this issue Feb 26, 2019
Centril added a commit to Centril/rust that referenced this issue Feb 27, 2019
…hton

Add tests for several E-needstest issues

This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise.

Closes rust-lang#10876.
Closes rust-lang#22892.
Closes rust-lang#26448.
Closes rust-lang#26577.
Closes rust-lang#26619.
Closes rust-lang#27054.
Closes rust-lang#28587.
Closes rust-lang#44127.
Closes rust-lang#44255.
Closes rust-lang#55731.
Closes rust-lang#57781.
varkor added a commit to varkor/rust that referenced this issue Feb 27, 2019
pietroalbini added a commit to pietroalbini/rust that referenced this issue Mar 1, 2019
…hton

Add tests for several E-needstest issues

This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise.

Closes rust-lang#10876.
Closes rust-lang#22892.
Closes rust-lang#26448.
Closes rust-lang#26577.
Closes rust-lang#26619.
Closes rust-lang#27054.
Closes rust-lang#28587.
Closes rust-lang#44127.
Closes rust-lang#44255.
Closes rust-lang#55731.
Closes rust-lang#57781.
Centril added a commit to Centril/rust that referenced this issue Mar 9, 2019
…hton

Add tests for several E-needstest issues

This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise.

Closes rust-lang#10876.
Closes rust-lang#22892.
Closes rust-lang#26448.
Closes rust-lang#26577.
Closes rust-lang#26619.
Closes rust-lang#27054.
Closes rust-lang#28587.
Closes rust-lang#44127.
Closes rust-lang#44255.
Closes rust-lang#55731.
Closes rust-lang#57781.
Centril added a commit to Centril/rust that referenced this issue Mar 9, 2019
…hton

Add tests for several E-needstest issues

This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise.

Closes rust-lang#10876.
Closes rust-lang#22892.
Closes rust-lang#26448.
Closes rust-lang#26577.
Closes rust-lang#26619.
Closes rust-lang#27054.
Closes rust-lang#28587.
Closes rust-lang#44127.
Closes rust-lang#44255.
Closes rust-lang#55731.
Closes rust-lang#57781.
varkor added a commit to varkor/rust that referenced this issue Mar 11, 2019
bors added a commit that referenced this issue Mar 12, 2019
Add tests for several E-needstest issues

This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise.

Closes #10876.
Closes #26448.
Closes #26577.
Closes #26619.
Closes #27054.
Closes #44127.
Closes #44255.
Closes #55731.
Closes #57781.
varkor added a commit to varkor/rust that referenced this issue Mar 12, 2019
pietroalbini added a commit to pietroalbini/rust that referenced this issue Mar 12, 2019
…hton

Add tests for several E-needstest issues

This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise.

Closes rust-lang#10876.
Closes rust-lang#26448.
Closes rust-lang#26577.
Closes rust-lang#26619.
Closes rust-lang#27054.
Closes rust-lang#44127.
Closes rust-lang#44255.
Closes rust-lang#55731.
Closes rust-lang#57781.
bors added a commit that referenced this issue Mar 12, 2019
Add tests for several E-needstest issues

This PR adds a number of tests for various `E-needstest` errors. These tend to have been left open for a long time and seem unlikely to be closed otherwise.

Closes #10876.
Closes #26448.
Closes #26577.
Closes #26619.
Closes #27054.
Closes #44127.
Closes #44255.
Closes #55731.
Closes #57781.
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 C-feature-request Category: A feature request, i.e: not implemented / a PR. 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

Successfully merging a pull request may close this issue.

6 participants