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

Missing else Clause in if expr That Returns Should Produce Type Mismatch Error #3230

Open
nobel-sh opened this issue Oct 28, 2024 · 0 comments · May be fixed by #3306
Open

Missing else Clause in if expr That Returns Should Produce Type Mismatch Error #3230

nobel-sh opened this issue Oct 28, 2024 · 0 comments · May be fixed by #3306

Comments

@nobel-sh
Copy link
Contributor

If the if expr returns a type T then else is expected to produce the same type T but in absence of else branch it return a () type. However, gccrs does not enforce this check.

fn foo(pred: bool) -> u8 {
    if pred {
        1
    }
    3
}
fn main(){
}

rustc:

error[E0308]: mismatched types
 --> <source>:3:9
  |
2 | /     if pred {
3 | |         1
  | |         ^ expected `()`, found integer
4 | |     }
  | |_____- expected this to be `()`
  |
help: you might have meant to return this value
  |
3 |         return 1;
  |         ++++++  +

error: aborting due to 1 previous error

gccrs: Compiles sucessfully

godbolt: https://godbolt.org/z/xnczfjznT

@Kamiinarii78 Kamiinarii78 linked a pull request Dec 16, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant