Skip to content

Commit 614c2ba

Browse files
authored
Rollup merge of rust-lang#90930 - Nilstrieb:fix-non-const-value-ice, 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
2 parents 2b75b61 + d64aea6 commit 614c2ba

File tree

8 files changed

+100
-1
lines changed

8 files changed

+100
-1
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,23 @@ impl<'a> Resolver<'a> {
450450
// let foo =...
451451
// ^^^ given this Span
452452
// ------- get this Span to have an applicable suggestion
453+
454+
// edit:
455+
// only do this if the const and usage of the non-constant value are on the same line
456+
// the further the two are apart, the higher the chance of the suggestion being wrong
457+
// also make sure that this line isn't the first one (ICE #90878)
458+
453459
let sp =
454460
self.session.source_map().span_extend_to_prev_str(ident.span, current, true);
455-
if sp.lo().0 == 0 {
461+
462+
let is_first_line = self
463+
.session
464+
.source_map()
465+
.lookup_line(sp.lo())
466+
.map(|file_and_line| file_and_line.line == 0)
467+
.unwrap_or(true);
468+
469+
if sp.lo().0 == 0 || self.session.source_map().is_multiline(sp) || is_first_line {
456470
err.span_label(ident.span, &format!("this would need to be a `{}`", sugg));
457471
} else {
458472
let sp = sp.with_lo(BytePos(sp.lo().0 - current.len() as u32));

compiler/rustc_span/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,7 @@ pub struct Loc {
19351935
#[derive(Debug)]
19361936
pub struct SourceFileAndLine {
19371937
pub sf: Lrc<SourceFile>,
1938+
/// Index of line, starting from 0.
19381939
pub line: usize,
19391940
}
19401941
#[derive(Debug)]

src/test/ui/consts/issue-90878-2.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![l=|x|[b;x ]] //~ ERROR unexpected token: `|x| [b; x]`
2+
//~^ ERROR cannot find attribute `l` in this scope
3+
//~^^ ERROR attempt to use a non-constant value in a constant [E0435]
4+
//~^^^ ERROR cannot find value `b` in this scope [E0425]
5+
6+
// notice the space at the start,
7+
// we can't attach any attributes to this file because it needs to be at the start
8+
9+
// this example has been slightly modified (adding ]] at the end), so that it actually works here
10+
// it still produces the same issue though
11+
12+
fn main() {}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error: unexpected token: `|x| [b; x]`
2+
--> $DIR/issue-90878-2.rs:1:7
3+
|
4+
LL | #![l=|x|[b;x ]]
5+
| ^^^^^^^^^
6+
7+
error: cannot find attribute `l` in this scope
8+
--> $DIR/issue-90878-2.rs:1:5
9+
|
10+
LL | #![l=|x|[b;x ]]
11+
| ^
12+
13+
error[E0435]: attempt to use a non-constant value in a constant
14+
--> $DIR/issue-90878-2.rs:1:13
15+
|
16+
LL | #![l=|x|[b;x ]]
17+
| - ^
18+
| |
19+
| this would need to be a `const`
20+
21+
error[E0425]: cannot find value `b` in this scope
22+
--> $DIR/issue-90878-2.rs:1:11
23+
|
24+
LL | #![l=|x|[b;x ]]
25+
| ^ help: a local variable with a similar name exists: `x`
26+
27+
error: aborting due to 4 previous errors
28+
29+
Some errors have detailed explanations: E0425, E0435.
30+
For more information about an error, try `rustc --explain E0425`.

src/test/ui/consts/issue-90878.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
|x: usize| [0; x]; //~ ERROR attempt to use a non-constant value in a constant [E0435]
3+
// (note the space before "fn")
4+
}

src/test/ui/consts/issue-90878.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0435]: attempt to use a non-constant value in a constant
2+
--> $DIR/issue-90878.rs:2:20
3+
|
4+
LL | |x: usize| [0; x];
5+
| - ^
6+
| |
7+
| this would need to be a `const`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0435`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let x = 5;
3+
const Y: i32 = x; //~ ERROR attempt to use a non-constant value in a constant [E0435]
4+
5+
let x = 5;
6+
let _ = [0; x]; //~ ERROR attempt to use a non-constant value in a constant [E0435]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0435]: attempt to use a non-constant value in a constant
2+
--> $DIR/non-const-value-in-const.rs:3:20
3+
|
4+
LL | const Y: i32 = x;
5+
| ------- ^ non-constant value
6+
| |
7+
| help: consider using `let` instead of `const`: `let Y`
8+
9+
error[E0435]: attempt to use a non-constant value in a constant
10+
--> $DIR/non-const-value-in-const.rs:6:17
11+
|
12+
LL | let x = 5;
13+
| ----- help: consider using `const` instead of `let`: `const x`
14+
...
15+
LL | let _ = [0; x];
16+
| ^ non-constant value
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0435`.

0 commit comments

Comments
 (0)