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

thread 'rustc' panicked at 'attempt to subtract with overflow', rust/compiler/rustc_resolve/src/diagnostics.rs:458:49 #90878

Closed
Badel2 opened this issue Nov 13, 2021 · 1 comment · Fixed by #90930
Assignees
Labels
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.

Comments

@Badel2
Copy link
Contributor

Badel2 commented Nov 13, 2021

Code

 fn main() {
    |x: usize| [0; x];
    // (note the space before "fn")
}

When rustc is compiled with debug assertions, this results in a subtract with overflow on this line:

let sp = sp.with_lo(BytePos(sp.lo().0 - current.len() as u32));

And if compiled without debug assertions, you get a warning "Invalid span".

Introduced in #80801

Affected versions: stable 1.56.1, nightly 2021-11-12

Backtrace

With debug assertions:

thread 'rustc' panicked at 'attempt to subtract with overflow', rust/compiler/rustc_resolve/src/diagnostics.rs:458:49
stack backtrace:
   0: rust_begin_unwind
             at /rustc/46b8e7488eae116722196e8390c1bd2ea2e396cf/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/46b8e7488eae116722196e8390c1bd2ea2e396cf/library/core/src/panicking.rs:106:14
   2: core::panicking::panic
             at /rustc/46b8e7488eae116722196e8390c1bd2ea2e396cf/library/core/src/panicking.rs:47:5
   3: rustc_resolve::diagnostics::<impl rustc_resolve::Resolver>::into_struct_error
   4: rustc_resolve::diagnostics::<impl rustc_resolve::Resolver>::report_error
   5: rustc_resolve::Resolver::resolve_ident_in_lexical_scope
   6: rustc_resolve::Resolver::resolve_path_with_ribs::{{closure}}
   7: rustc_resolve::Resolver::resolve_path_with_ribs
   8: rustc_resolve::late::LateResolutionVisitor::resolve_qpath_anywhere
   9: rustc_resolve::late::LateResolutionVisitor::smart_resolve_path_fragment
  10: rustc_resolve::late::LateResolutionVisitor::resolve_expr
  11: rustc_resolve::late::LateResolutionVisitor::resolve_anon_const
  12: rustc_resolve::late::LateResolutionVisitor::resolve_expr
  13: <rustc_resolve::late::LateResolutionVisitor as rustc_ast::visit::Visitor>::visit_fn
  14: rustc_ast::visit::walk_expr
  15: rustc_resolve::late::LateResolutionVisitor::resolve_expr
  16: rustc_resolve::late::LateResolutionVisitor::resolve_block
  17: <rustc_resolve::late::LateResolutionVisitor as rustc_ast::visit::Visitor>::visit_fn
  18: rustc_ast::visit::walk_item
  19: <rustc_resolve::late::LateResolutionVisitor as rustc_ast::visit::Visitor>::visit_item
  20: rustc_resolve::Resolver::resolve_crate::{{closure}}
  21: rustc_resolve::Resolver::resolve_crate
  22: rustc_interface::passes::configure_and_expand
  23: rustc_interface::queries::Queries::expansion
  24: rustc_interface::interface::run_compiler::{{closure}}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Without debug assertions, it only shows a warning "Invalid span":

   Compiling playground v0.0.1 (/playground)
WARN rustc_errors::emitter Invalid span src/main.rs:2:7: 292:4282133246 (#0), error=DistinctSources(DistinctSources { begin: (Real(LocalPath("src/main.rs")), BytePos(0)), end: (Real(Remapped { local_path: None, virtual_name: "/rustc/e90c5fbbc5df5c81267747daeb937d4e955ce6ad/library/unwind/src/libunwind.rs" }), BytePos(12823298)) })
error[E0435]: attempt to use a non-constant value in a constant
   --> src/main.rs:2:20
    |
2   |       |x: usize| [0; x];
    |         -            ^ non-constant value
    |  _______|
    | |
3   | |     // (note the space before "fn")
4   | | }
...   |

For more information about this error, try `rustc --explain E0435`.
error: could not compile `playground` due to previous error

This issue was found thanks to fuzz-rustc, but the actual minimized code was hard to understand ( #![l=|x|[b;x) so I unminimized it a bit. A related issue I found is that the span is wrong when there is whitespace between "let" and "x" here:

fn main() {
    let          x = 0;
    [0; x];
}
error[E0435]: attempt to use a non-constant value in a constant
 --> src/main.rs:3:9
  |
2 |     let          x = 0;
  |              ----- help: consider using `const` instead of `let`: `const x`
3 |     [0; x];
  |         ^ non-constant value
@Badel2 Badel2 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 Nov 13, 2021
@Noratrieb
Copy link
Member

@rustbot claim

Noratrieb added a commit to Noratrieb/rust that referenced this issue Nov 15, 2021
This also fixes the same suggestion, which was kind of broken, because it just searched for the last occurence of `const` to replace with a `let`. This works great in some cases, but when there is no const and a leading space to the file, it doesn't work and panic with overflow because it thought that it had found a const.

I also changed the suggestion to only trigger if the `const` and the non-constant value are on the same line, because if they aren't, the suggestion is very likely to be wrong.

Also don't trigger the suggestion if the found `const` is on line 0, because that triggers the ICE.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 16, 2021
…r=estebank

Fix `non-constant value` ICE (rust-lang#90878)

This also fixes the same suggestion, which was kind of broken, because it just searched for the last occurence of `const` to replace with a `let`. This works great in some cases, but when there is no const and a leading space to the file, it doesn't work and panic with overflow because it thought that it had found a const.

I also changed the suggestion to only trigger if the `const` and the non-constant value are on the same line, because if they aren't, the suggestion is very likely to be wrong.

Also don't trigger the suggestion if the found `const` is on line 0, because that triggers the ICE.

Asking Esteban to review since he was the last one to change the relevant code.

r? `@estebank`

Fixes rust-lang#90878
Noratrieb added a commit to Noratrieb/rust that referenced this issue Nov 16, 2021
…ewline

I cannot provide a test for that thanks to tidy.
Noratrieb added a commit to Noratrieb/rust that referenced this issue Nov 17, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 20, 2021
…r=estebank

Fix `non-constant value` ICE (rust-lang#90878)

This also fixes the same suggestion, which was kind of broken, because it just searched for the last occurence of `const` to replace with a `let`. This works great in some cases, but when there is no const and a leading space to the file, it doesn't work and panic with overflow because it thought that it had found a const.

I also changed the suggestion to only trigger if the `const` and the non-constant value are on the same line, because if they aren't, the suggestion is very likely to be wrong.

Also don't trigger the suggestion if the found `const` is on line 0, because that triggers the ICE.

Asking Esteban to review since he was the last one to change the relevant code.

r? `@estebank`

Fixes rust-lang#90878
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 20, 2021
…askrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#90575 (Improve suggestions for compatible variants on type mismatch.)
 - rust-lang#90628 (Clarify error messages caused by re-exporting `pub(crate)` visibility to outside)
 - rust-lang#90930 (Fix `non-constant value` ICE (rust-lang#90878))
 - rust-lang#90983 (Make scrollbar in the sidebar always visible for visual consistency)
 - rust-lang#91021 (Elaborate `Future::Output` when printing opaque `impl Future` type)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 7993571 Nov 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants