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

Implicit return with block expression causes compilation to fail #133594

Closed
FineFindus opened this issue Nov 28, 2024 · 4 comments
Closed

Implicit return with block expression causes compilation to fail #133594

FineFindus opened this issue Nov 28, 2024 · 4 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@FineFindus
Copy link

I tried this code:

fn foo() -> i32 {
    1
}

fn bar() -> bool {
    { foo() } == 1
}

I expected to see this happen: The code should compile, as the block expression should return value returned by the function.

Instead, this happened: The compilation fails. However, when assigning the value first or using the return keyword, the code successfully compiles.

fn bar() -> bool {
   // both of the compile
   let val =  { foo() } == 1;
   val
   return { foo() } == 1;
}

Meta

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1
Backtrace

error: expected expression, found `==`
 --> src/lib.rs:6:15
  |
6 |     { foo() } == 1
  |               ^^ expected expression

warning: unnecessary braces around block return value
 --> src/lib.rs:6:5
  |
6 |     { foo() } == 1
  |     ^^     ^^
  |
  = note: `#[warn(unused_braces)]` on by default
help: remove these braces
  |
6 -     { foo() } == 1
6 +     foo() == 1
  |

error[E0308]: mismatched types
 --> src/lib.rs:6:7
  |
6 |     { foo() } == 1
  |       ^^^^^- help: consider using a semicolon here: `;`
  |       |
  |       expected `()`, found `i32`

For more information about this error, try `rustc --explain E0308`.

@FineFindus FineFindus added the C-bug Category: This is a bug. label Nov 28, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 28, 2024
@cyrgani
Copy link
Contributor

cyrgani commented Nov 28, 2024

Notably, 1 == { 1 } compiles as expected, but { 1 } == 1 fails.

@ChayimFriedman2
Copy link
Contributor

This is known and documented: https://doc.rust-lang.org/stable/reference/statements.html#expression-statements.

@rustbot label -C-bug +C-discussion

@rustbot rustbot added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. labels Nov 28, 2024
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 29, 2024
@jieyouxu
Copy link
Member

jieyouxu commented Nov 29, 2024

This specific case is also known way back in like #28379 and friends.

@jieyouxu
Copy link
Member

This is very much intentional. Thank you for the report.

@jieyouxu jieyouxu closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

5 participants