Skip to content
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

Const functions sometimes don't do overflow checks in release mode #74823

Open
Y0ba opened this issue Jul 27, 2020 · 4 comments
Open

Const functions sometimes don't do overflow checks in release mode #74823

Y0ba opened this issue Jul 27, 2020 · 4 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Y0ba
Copy link

Y0ba commented Jul 27, 2020

I tried this code in release mode:

const fn foo(arg: i32) -> i32 {
    arg + i32::MAX
}
const x: i32 = foo(1);

I expected to see this happen: error: this arithmetic operation will overflow

Instead, code successfully compiled.

If I replace arg with 1 in function than compilation failing with an overflow error.

@Y0ba Y0ba added the C-bug Category: This is a bug. label Jul 27, 2020
@tesuji
Copy link
Contributor

tesuji commented Jul 27, 2020

This is consistent with overflow check. If you run the code at debug mode, rustc will error:

error: any use of this value will cause an error
 --> src/lib.rs:2:5
  |
2 |     arg + i32::MAX
  |     ^^^^^^^^^^^^^^
  |     |
  |     attempt to add with overflow
  |     inside `foo` at src/lib.rs:2:5
  |     inside `x` at src/lib.rs:4:16
3 | }
4 | const x: i32 = foo(1);
  | ----------------------
  |
  = note: `#[deny(const_err)]` on by default

@Y0ba
Copy link
Author

Y0ba commented Jul 27, 2020

But inconsistent with other const computations (like 1 + i32::MAX which fails in release mode too). Overflow checking is disabled in release mode because of performance penalty (#47739) but does it matter in const functions?

@tesuji
Copy link
Contributor

tesuji commented Jul 27, 2020

Probably it relates to const propagation here, which is unlike 1+ i32::MAX case.

@LeSeulArtichaut LeSeulArtichaut added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 28, 2020
@matu3ba
Copy link

matu3ba commented Dec 3, 2020

But inconsistent with other const computations (like 1 + i32::MAX which fails in release mode too). Overflow checking is disabled in release mode because of performance penalty (#47739) but does it matter in const functions?

Compile-time const unwrapping soon gets implemented. This should fix the code.
It matters, because you want all checks on in compile-time evaluations/execution.

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-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants