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

Internal Compiler Error: 'attempt to subtract with overflow', compiler/rustc_session/src/session.rs:478:22 #84219

Closed
dwrensha opened this issue Apr 15, 2021 · 6 comments · Fixed by #87761
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Milestone

Comments

@dwrensha
Copy link
Contributor

dwrensha commented Apr 15, 2021

Build rustc with overflow checking enabled:

RUSTFLAGS="-C overflow-checks=on" ./x.py build --stage 1

Then try compiling the following program (found by fuzz-rustc):

const M;
$ ./build/x86_64-unknown-linux-gnu/stage1/bin/rustc bug.rs 
error: free constant item without body
 --> bug.rs:1:1
  |
1 | const M;
  | ^^^^^^^-
  |        |
  |        help: provide a definition for the constant: `= <expr>;`

error[E0601]: `main` function not found in crate `bug`
 --> bug.rs:1:1
  |
1 | const M;
  | ^^^^^^^^ consider adding a `main` function to `bug.rs`

thread 'rustc' panicked at 'attempt to subtract with overflow', /home/dwrensha/src/rust/compiler/rustc_session/src/session.rs:475:22
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.51.0-dev running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0601`.

@dwrensha dwrensha added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 15, 2021
@dwrensha
Copy link
Contributor Author

The ICE is a regression, introduced in #82720.

The overflowing subtraction happens here:

// FIXME(matthewjasper) Remove this method, it should never be needed.
pub fn track_errors<F, T>(&self, f: F) -> Result<T, ErrorReported>
where
F: FnOnce() -> T,
{
let old_count = self.err_count();
let result = f();
let errors = self.err_count() - old_count;
if errors == 0 { Ok(result) } else { Err(ErrorReported) }
}

cc @matthewjasper @henryboisdequin

@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels Apr 15, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Apr 15, 2021
@jyn514 jyn514 added regression-from-stable-to-beta Performance or correctness regression from stable to beta. and removed regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels Apr 15, 2021
@apiraino
Copy link
Contributor

Assigning priority as discussed as part of the Prioritization Working Group procedure and removing I-prioritize.

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Apr 15, 2021
@pietroalbini pietroalbini added this to the 1.52.0 milestone Apr 30, 2021
@pietroalbini pietroalbini added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels May 4, 2021
@pietroalbini
Copy link
Member

This regression will unfortunately be included in 1.52.0 stable. Updating the labels.

@ehuss
Copy link
Contributor

ehuss commented May 4, 2021

This was discussed a bit on Zulip here. I did a small amount of investigation, and found that the code that checks if "errors were generated" does not handle stashed errors correctly. It keeps a count of errors, but with stashed errors that count can go up and down at various times. In this particular case, an error is stashed and then stolen.

The comment on the relevant function ("Remove this method, it should never be needed.") isn't really clear why it shouldn't be needed. In general, that error tracking logic needs to change to handle stashed errors correctly.

@estebank
Copy link
Contributor

estebank commented May 4, 2021

@ehuss track_errors is a remnant from when we desired to check at the end of each stage and stop rustc from continuing. This is the easiest and safest course of action, but leads to the "waves of errors" experience. We've been moving away from that, but just removing track_errors blindly would cause a bunch of ICEs when codepaths that still assume things are well formed are exercised.

bors added a commit to rust-lang-ci/rust that referenced this issue Aug 6, 2021
…_checks, r=Mark-Simulacrum

Add config.toml options for enabling overflow checks in rustc and std

The names are `overflow-checks` and `overflow-checks-std` and they work similar to  `debug-assertions` and `debug-assertions-std`. Once added we can measure how big the performance impact actually is and maybe enable them for CI tests.

Enabling them already makes two ui tests fail:
```
failures:
    [ui] ui/parser/item-free-const-no-body-semantic-fail.rs
    [ui] ui/parser/item-free-static-no-body-semantic-fail.rs
```
(See rust-lang#84219 and rust-lang#87761.)
JohnTitor added a commit to JohnTitor/rust that referenced this issue Aug 6, 2021
…=Mark-Simulacrum

Fix overflow in rustc happening if the `err_count()` is reduced in a stage.

This can happen if stashed diagnostics are removed or replaced with fewer errors. The semantics stay the same if built without overflow checks. Fixes rust-lang#84219.

Background: I came across this independently by running `RUSTFLAGS="-C overflow-checks=on" ./x.py test`. Fixing this will allow us to move on and find further overflow errors with testing or fuzzing.
@bors bors closed this as completed in 5ff06fb Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. 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.

7 participants