Skip to content

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

@celinval

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.

Metadata

Metadata

Assignees

Labels

[C] BugThis is a bug. Something isn't working.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions