Skip to content

Duplicate errors for overflowing division / remainder #69020

Closed

Description

@RalfJung

When there is an overflowing division or remainder operation that const_prop can detect, the error is duplicated in release mode only:

#![deny(const_err)]

use std::i32;

fn main() {
    let _ = i32::MIN / -1;
}

shows

error: attempt to divide with overflow
 --> src/main.rs:6:13
  |
6 |     let _ = i32::MIN / -1;
  |             ^^^^^^^^^^^^^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(const_err)]
  |         ^^^^^^^^^

error: this expression will panic at runtime
 --> src/main.rs:6:13
  |
6 |     let _ = i32::MIN / -1;
  |             ^^^^^^^^^^^^^ attempt to divide with overflow

This is because !overflow_check is true in release mode but divison and remainder still get overflow checks.

(This test case already covers the problem, so a fix does not need a new test case, it just needs to adjust the existing test case to no longer expect two ERROR per line.)

Cc @oli-obk @wesleywiser

Activity

  1. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Feb 10, 2020
  2. RalfJung commented on Feb 10, 2020

    @RalfJung
    MemberAuthor

    Basically the same issue also leads to duplicate errors for Neg, though that requires associated consts to actually trigger:

    use std::i32;
    
    pub trait Foo {
        const N: i32;
    }
    
    impl<T: Foo> Foo for Vec<T> {
        const N: i32 = -i32::MIN + T::N;
    }

    shows

    error: attempt to negate with overflow
     --> src/lib.rs:8:20
      |
    8 |     const N: i32 = -i32::MIN + T::N;
      |                    ^^^^^^^^^
      |
      = note: `#[deny(const_err)]` on by default
    
    error: this expression will panic at runtime
     --> src/lib.rs:8:20
      |
    8 |     const N: i32 = -i32::MIN + T::N;
      |                    ^^^^^^^^^ attempt to negate with overflow
    

    Here, the cause is that even in release builds, const bodies use checked arithmetic. (The assoc const is needed because otherwise the const body just gets evaluated, not analyzed statically by const-prop.)

  3. added
    D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.
    on Feb 10, 2020
  4. added a commit that references this issue on Feb 20, 2020
  5. added 2 commits that reference this issue on Apr 2, 2020
  6. added a commit that references this issue on Apr 3, 2020
  7. added a commit that references this issue on Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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