gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Incorrect handling of overflow in numeric types

Open nobel-sh opened this issue 5 months ago • 3 comments

There appears to be a bug in the handling of overflows for signed integer types and floating-point types. While unsigned integers are unaffected, assigning the minimum value (TYPE::MIN) to signed integer variables results in a compiler error. But in case of floats, error occurs on both TYPE::MIN and TYPE::MAX and also on values which are smaller than TYPE::MAX or larger than TYPE::MIN.

Signed Integer

// integer types
fn main() {
    let _: i8 = -128;
    let _: i16 = -32768;
    let _: i32 = -2147483648;
    let _: i64 = -9223372036854775808;
}

Expected Behavior:

The code should compile without errors.

Actual Behavior:

<source>:2:18: error: integer overflows the respective type 'i8'
    2 |     let _: i8 = -128;
      |                  ^~~
<source>:3:19: error: integer overflows the respective type 'i16'
    3 |     let _: i16 = -32768;
      |                   ^~~~~
<source>:4:19: error: integer overflows the respective type 'i32'
    4 |     let _: i32 = -2147483648;
      |                   ^~~~~~~~~~
<source>:5:19: error: integer overflows the respective type 'i64'
    5 |     let _: i64 = -9223372036854775808;
      |                   ^~~~~~~~~~~~~~~~~~~

Floating point

// floating types
fn main() {
    let _f32_min:f32 = -3.40282347E+38f32;
    let _f32_max:f32 = 3.40282347E+38f32;
    let _f64_min:f64 = 1.7976931348623157E+308f64;
    let _f64_max:f64 = -1.7976931348623157E+308f64;
    // and other float types.

    // Some values although not on the limit also seem to throw
    // compiler error.
    let _f32_random_fail_1:f32 = 1.40282347E+30f32;
    let _f32_random_fail_2:f32 = 1.40282347E+10f32;
    let _f32_random_pass:f32 = 1.40282347E+9f32; // this passes
}

Expected Behavior:

The code should compile without errors.

Actual Behavior:

<source>:9:25: error: decimal overflows the respective type 'f32'
    9 |     let _f32_min:f32 = -3.40282347E+38f32;
      |                         ^~~~~~~~~~~~~~~~~
<source>:10:24: error: decimal overflows the respective type 'f32'
   10 |     let _f32_max:f32 = 3.40282347E+38f32;
      |                        ^~~~~~~~~~~~~~~~~
<source>:11:24: error: decimal overflows the respective type 'f64'
   11 |     let _f64_min:f64 = 1.7976931348623157E+308f64;
      |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:12:25: error: decimal overflows the respective type 'f64'
   12 |     let _f64_max:f64 = -1.7976931348623157E+308f64;
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:17:34: error: decimal overflows the respective type 'f32'
   17 |     let _f32_random_fail_1:f32 = 1.40282347E+30f32;
      |                                  ^~~~~~~~~~~~~~~~~
<source>:18:34: error: decimal overflows the respective type 'f32'
   18 |     let _f32_random_fail_2:f32 = 1.40282347E+10f32;
      |                                  ^~~~~~~~~~~~~~~~~

Godbolt: https://godbolt.org/z/oMrMqoa38

nobel-sh avatar Aug 24 '24 22:08 nobel-sh