Closed
Description
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:
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