Skip to content

Code location for assert!(false) is incorrect. #466

Closed

Description

@celinval

RMC is giving the wrong code location of assert!(false) statements.

I tried this code:

fn magic() -> i32 {     // L1
    5                   // L2
}                       // L3
                        // L4
pub fn main() {         // L5
    if magic() == 5 {   // L6
        assert!(false); // L7
    }                   // L8
    println!("Magic!"); // L9
}                       // L10

using the following command line invocation:

rmc --input dummy.rs

with RMC version:

The following output is printed:

[main.assertion.1] line 6 assertion failed: false: FAILURE

Note that the MIR is correct:

> rustc dummy.rs --emit mir
> grep panic dummy.mir
     core::panicking::panic(const "assertion failed: false"); // scope 0 at dummy.rs:7:9: 7:24

Root cause:

The assert!() macro is translated to if !cond { begin_panic; }. However, for assert!(false), rustc optimizes the generated code and it only emit a panic statement.

During RMC code generation of panic statements, we assume that if the basic block only has one predecessor, it is an assertion. There are many cases that this is not true. See this code.

Activity

  1. added
    [C] Bug
    on Sep 1, 2021
  2. changed the title [-]Code location for assert(false) is incorrect.[/-] [+]Code location for `assert!(false)` is incorrect.[/+] on Sep 1, 2021
  3. celinval commented on Oct 4, 2021

    @celinval
    ContributorAuthor
  4. added a commit that references this issue on Dec 10, 2021
    56f2aae
  5. self-assigned this
    on Dec 10, 2021
  6. added a commit that references this issue on Dec 14, 2021
    625990c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

[C] Bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions