Skip to content

if ... as T < ... && ... >= ... is treated as const generics #88416

Description

@alianse777

I'm not sure if it is expected behavior or not.

I tried this code:

fn main() {
    if 1 as f64 < 0.0 && 1 >= 0 {
        
    }
}

I expected to see this happen: the code compiles without errors

Instead, this happened:

Compiling playground v0.0.1 (/playground)
error: expressions must be enclosed in braces to be used as const generic arguments
 --> src/main.rs:3:19
  |
3 |     if 1 as f64 < 0.0 && 1 >= 0 {
  |                   ^^^^^^^^
  |
help: enclose the `const` expression in braces
  |
3 |     if 1 as f64 < { 0.0 && 1 } >= 0 {
  |                   +          +

error[E0109]: const arguments are not allowed for this type
 --> src/main.rs:3:19
  |
3 |     if 1 as f64 < 0.0 && 1 >= 0 {
  |                   ^^^^^^^^ const argument not allowed

error[E0308]: mismatched types
 --> src/main.rs:3:8
  |
3 |     if 1 as f64 < 0.0 && 1 >= 0 {
  |        ^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
  |
help: you might have meant to use pattern matching
  |
3 |     if let 1 as f64 < 0.0 && 1 >= 0 {
  |        +++

error[E0308]: mismatched types
 --> src/main.rs:3:19
  |
3 |     if 1 as f64 < 0.0 && 1 >= 0 {
  |                   ^^^ expected `bool`, found floating-point number

error[E0308]: mismatched types
 --> src/main.rs:3:26
  |
3 |     if 1 as f64 < 0.0 && 1 >= 0 {
  |                          ^ expected `bool`, found integer

Some errors have detailed explanations: E0109, E0308.
For more information about an error, try `rustc --explain E0109`.
error: could not compile `playground` due to 5 previous errors

rustc 1.54.0 stable.

If I change to (1 as f64) < 0.0 && 1 >= 0 it compiles.

Link to playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=48dbdf8fc80dde053da67f6b3db3d143

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions