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

a53c250
Opened by alianse777 at 2024-02-16 05:29:18

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

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

  1. 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

    alianse777 at 2021-08-29 05:32:12

  2. If I change to if 1 as f64 <= 0.0 && 1 >= 0 it also compiles, why is that?

    goldwind-ting at 2021-08-28 08:27:52

  3. If I change to if 1 as f64 <= 0.0 && 1 >= 0 it also compiles, why is that?

    Because of <= instead of <

    alianse777 at 2021-08-28 10:08:50

  4. Output on rust 1.48.0 (godbolt):

    error: `<` is interpreted as a start of generic arguments for `f64`, not a comparison
     --> <source>:2:17
      |
    2 |     if 1 as f64 < 0.0 && 1 >= 0 {
      |        -------- ^ ------ interpreted as generic arguments
      |        |        |
      |        |        not interpreted as comparison
      |        help: try comparing the cast value: `(1 as f64)`
    
    error: aborting due to previous error
    

    SNCPlay42 at 2021-08-28 16:21:07

  5. Sadly, this seems to have regressed again (stable 1.75, nightly 1.77):

    error: expressions must be enclosed in braces to be used as const generic arguments
     --> src/main.rs:2:19
      |
    2 |     if 1 as f64 < 0.0 && 1 >= 0 {
      |                   ^^^^^^^^
      |
    help: enclose the `const` expression in braces
      |
    2 |     if 1 as f64 < { 0.0 && 1 } >= 0 {
      |                   +          +
    
    error[E0109]: const arguments are not allowed on builtin type `f64`
     --> src/main.rs:2:19
      |
    2 |     if 1 as f64 < 0.0 && 1 >= 0 {
      |             ---   ^^^^^^^^ const argument not allowed
      |             |
      |             not allowed on builtin type `f64`
      |
    help: primitive type `f64` doesn't have generic parameters
      |
    2 -     if 1 as f64 < 0.0 && 1 >= 0 {
    2 +     if 1 as f64= 0 {
      |
    
    error[E0308]: mismatched types
     --> src/main.rs:2:8
      |
    2 |     if 1 as f64 < 0.0 && 1 >= 0 {
      |        ^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
    
    error[E0308]: mismatched types
     --> src/main.rs:2:19
      |
    2 |     if 1 as f64 < 0.0 && 1 >= 0 {
      |                   ^^^ expected `bool`, found floating-point number
    
    error[E0308]: mismatched types
     --> src/main.rs:2:26
      |
    2 |     if 1 as f64 < 0.0 && 1 >= 0 {
      |                          ^ expected `bool`, found integer
    

    León Orell Valerian Liehr at 2024-01-23 23:51:25

  6. Triage: no change.

    Esteban Kuber at 2024-02-16 05:29:18