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

Show values that lead to an arithmetic overflow when computing constants #71134

Closed
shepmaster opened this issue Apr 14, 2020 · 0 comments · Fixed by #73513
Closed

Show values that lead to an arithmetic overflow when computing constants #71134

shepmaster opened this issue Apr 14, 2020 · 0 comments · Fixed by #73513
Assignees
Labels
A-const-eval Area: constant evaluation (mir interpretation) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@shepmaster
Copy link
Member

shepmaster commented Apr 14, 2020

In Rust 1.42, this code correctly fails due to a misuse of bitwise-XOR as exponentiation:

const N: usize = 10;
const N_CELLS: usize = (N ^ 2) - 50;
error: any use of this value will cause an error
 --> src/lib.rs:2:24
  |
2 | const N_CELLS: usize = (N ^ 2) - 50;
  | -----------------------^^^^^^^^^^^^-
  |                        |
  |                        attempt to subtract with overflow
  |
  = note: `#[deny(const_err)]` on by default

However, the error message could be enhanced to show the values that lead to the overflow. One rough idea:

2 | const N_CELLS: usize = (N ^ 2) - 50;
  |                        ^^^^^^^   ^^
  |                        |         |
  |                        evaluated to 8
  |                                  |
  |                                  evaluated to 50

This originated from a Stack Overflow question: How can I guarantee that overflow will not occur in const variables?

/cc @oli-obk

@shepmaster shepmaster added A-diagnostics Area: Messages for errors, warnings, and lints C-feature-request Category: A feature request, i.e: not implemented / a PR. A-const-eval Area: constant evaluation (mir interpretation) D-papercut Diagnostics: An error or lint that needs small tweaks. labels Apr 14, 2020
@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 14, 2020
@JohnTitor JohnTitor added C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Apr 16, 2020
@oli-obk oli-obk added E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. and removed E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Jun 19, 2020
@oli-obk oli-obk self-assigned this Jun 19, 2020
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 22, 2020
…ebank

Show the values and computation that would overflow a const evaluation or propagation

Fixes rust-lang#71134

In contrast to the example in the issue it doesn't use individual spans for each operand. The effort required to implement that is quite high compared to the little (if at all) benefit it would bring to diagnostics.

cc @shepmaster

The way this is implemented it is also fairly easy to do the same for overflow panics at runtime, but that should be done in a separate PR since it may have runtime performance implications.
@bors bors closed this as completed in 7750c3d Jun 26, 2020
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 (mir interpretation) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants