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

Identity operation enforced by compiler? #56923

Closed
emabee opened this issue Dec 17, 2018 · 5 comments
Closed

Identity operation enforced by compiler? #56923

emabee opened this issue Dec 17, 2018 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-grammar Area: The grammar of Rust

Comments

@emabee
Copy link

emabee commented Dec 17, 2018

fn main() {
    println!("> {}",type_code(true));
    println!("> {}",type_code(false));
}

fn type_code(switch: bool) -> u8 {
    // why is the leading "0_u8 + " necessary? Compiler bug?
    0_u8 + if switch { 128 } else { 0 } + base_type_code()
}

fn base_type_code() -> u8 {
    111
}

(Playground)

Output:

> 239
> 111

Shouldn't the above code also compile without the "0_u8 + "?
Note that clippy even complains about this identity operation.

@zackmdavis zackmdavis added the A-grammar Area: The grammar of Rust label Dec 17, 2018
@zackmdavis
Copy link
Member

Parentheses work:

fn type_code(switch: bool) -> u8 {
    (if switch { 128 } else { 0 }) + base_type_code()
}

I'm not sure how the parser determines when conditionals-as-expressions need parentheses; I agree that it's very counterintuitive for a leading 0 + to determine whether a subsequent expression even parses (!).

@emabee
Copy link
Author

emabee commented Dec 17, 2018

Not yet good, but already much better ;-)
Thanks!

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 17, 2018
@nagisa
Copy link
Member

nagisa commented Dec 17, 2018

This is an expected behaviour of semi-colon insertion for semi-statements.

#29071
#49865

At best this is just a diagnostics issue.

@hellow554
Copy link
Contributor

hellow554 commented Dec 20, 2018

Related: #54186

@nagisa
Copy link
Member

nagisa commented Dec 20, 2018

I‘m gonna close this as a duplicate of #54186 as that seems to be pretty much exactly the same thing, with a different operator.

@nagisa nagisa closed this as completed Dec 20, 2018
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 A-grammar Area: The grammar of Rust
Projects
None yet
Development

No branches or pull requests

5 participants