Skip to content

Commit 7993571

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 81f3ae8 + 96c37c8 commit 7993571

File tree

11 files changed

+125
-3
lines changed

11 files changed

+125
-3
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,24 @@ 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 the pos for the suggestion is not 0 (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 pos_for_suggestion = sp.lo().0.saturating_sub(current.len() as u32);
463+
464+
if sp.lo().0 == 0
465+
|| pos_for_suggestion == 0
466+
|| self.session.source_map().is_multiline(sp)
467+
{
456468
err.span_label(ident.span, &format!("this would need to be a `{}`", sugg));
457469
} else {
458-
let sp = sp.with_lo(BytePos(sp.lo().0 - current.len() as u32));
470+
let sp = sp.with_lo(BytePos(pos_for_suggestion));
459471
err.span_suggestion(
460472
sp,
461473
&format!("consider using `{}` instead of `{}`", sugg, current),

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-3.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
fn main() {
3+
|x: usize| [0; x]; //~ ERROR attempt to use a non-constant value in a constant [E0435]
4+
// (note the newline before "fn")
5+
}
6+
// ignore-tidy-leading-newlines
+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-3.rs:3: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`.

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`.

src/tools/tidy/src/style.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ pub fn check(path: &Path, bad: &mut bool) {
266266
contains_ignore_directive(can_contain, &contents, "end-whitespace");
267267
let mut skip_trailing_newlines =
268268
contains_ignore_directive(can_contain, &contents, "trailing-newlines");
269+
let mut skip_leading_newlines =
270+
contains_ignore_directive(can_contain, &contents, "leading-newlines");
269271
let mut skip_copyright = contains_ignore_directive(can_contain, &contents, "copyright");
270272
let mut leading_new_lines = false;
271273
let mut trailing_new_lines = 0;
@@ -350,7 +352,10 @@ pub fn check(path: &Path, bad: &mut bool) {
350352
}
351353
}
352354
if leading_new_lines {
353-
tidy_error!(bad, "{}: leading newline", file.display());
355+
let mut err = |_| {
356+
tidy_error!(bad, "{}: leading newline", file.display());
357+
};
358+
suppressible_tidy_err!(err, skip_leading_newlines, "mising leading newline");
354359
}
355360
let mut err = |msg: &str| {
356361
tidy_error!(bad, "{}: {}", file.display(), msg);
@@ -395,6 +400,9 @@ pub fn check(path: &Path, bad: &mut bool) {
395400
if let Directive::Ignore(false) = skip_trailing_newlines {
396401
tidy_error!(bad, "{}: ignoring trailing newlines unnecessarily", file.display());
397402
}
403+
if let Directive::Ignore(false) = skip_leading_newlines {
404+
tidy_error!(bad, "{}: ignoring leading newlines unnecessarily", file.display());
405+
}
398406
if let Directive::Ignore(false) = skip_copyright {
399407
tidy_error!(bad, "{}: ignoring copyright unnecessarily", file.display());
400408
}

0 commit comments

Comments
 (0)