Skip to content

-C debug-assertions=off does not apply on all consteval cases #113122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yanliu18 opened this issue Jun 28, 2023 · 4 comments
Open

-C debug-assertions=off does not apply on all consteval cases #113122

yanliu18 opened this issue Jun 28, 2023 · 4 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@yanliu18
Copy link

Not sure if this is a bug, but I'd post it anyways.

When experimenting turning off optimizations for MIR output, for example, disabling debug-assertions in the following code, the output is confusing. The overflow checking is removed from function foo, but not from const A evaluation.

It is less confusing if the debug assertions are turned off in all places.

BTW, is there a way to turn off const-eval? -Zmir-opt-level=0 is not useful.

I tried this code:

const fn foo(x: i32, y: i32) -> i32 {
    x + y
}

fn main() {
    const A: i32 = foo(1, 2 + 34);
    assert!(A == 37);
    return
}

Emitted mir for the code using

rustc test.rs --emit=mir -C debug-assertions=off

with rustc version

rustc 1.72.0-nightly (fe7454bf4 2023-06-19)

I expected to see the CheckedAdd replaced by Add and the assertion removed in the following code:

const A: i32 = {
    let mut _0: i32;
    let mut _1: i32;
    let mut _2: (i32, bool);

    bb0: {
        StorageLive(_1);
        _2 = CheckedAdd(const 2_i32, const 34_i32);
        assert(!move (_2.1: bool), "attempt to compute `{} + {}`, which would overflow", const 2_i32, const 34_i32) -> bb1;
    }

    bb1: {
        _1 = move (_2.0: i32);
        ConstEvalCounter;
        _0 = foo(const 1_i32, move _1) -> bb2;
    }

    bb2: {
        StorageDead(_1);
        return;
    }
}
@jyn514
Copy link
Member

jyn514 commented Jun 30, 2023

this is controlled by -C overflow-checks, not -C debug-assertions.

@jyn514 jyn514 added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. labels Jun 30, 2023
@yanliu18
Copy link
Author

@jyn514 -C overflow-checks produced same result as -C debug-assertions here. The CheckedAdd is not replaced to Add in Const A.

@Noratrieb
Copy link
Member

Rust considers overflow to always be a bug. The codegen flags control whether checks are produced, because overflow checks can be expensive. Consteval has all kinds of extra checks, for example also checks for some undefined behavior, which isn't checked at runtime either. Overflow checks are therefore also done in consteval. If you want wrapping behavior, use wrapping_*.

@yanliu18
Copy link
Author

yanliu18 commented Jul 3, 2023

@Nilstrieb thanks for the explanation. It makes sense to turn them off at runtime. (One quick clarification, when you mentioned runtime, you are mentioning the release model of code compilation, right?)

However, it does not answer my original question yet. I was looking for solutions to turn off the debug-assertions (same for overflow-checks) in the const-eval process without modifying the Rust code, to be precise.

If you could try the example posted in the issue, the generated MIR for const foo() has CheckedAdd replaced by Add when -Zdebug-assertion=off is passed to rustc. But the const-eval does not respond similarly.

I think it is a bug since const-eval does not respond to rustc flags unless there are other ways specially designed to turn off the debug-assertions for const-eval?

@Enselic Enselic added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

4 participants