Skip to content

Commit 700f3f2

Browse files
authored
Rollup merge of rust-lang#80801 - estebank:correct-binding-sugg-span, r=petrochenkov
Use correct span for structured suggestion On structured suggestion for `let` -> `const` and `const` -> `let`, use a proper `Span` and update tests to check the correct application. Follow up to rust-lang#80012.
2 parents d64356f + 9a5dcaa commit 700f3f2

27 files changed

+146
-64
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -398,20 +398,30 @@ impl<'a> Resolver<'a> {
398398
err.help("use the `|| { ... }` closure form instead");
399399
err
400400
}
401-
ResolutionError::AttemptToUseNonConstantValueInConstant(ident, sugg) => {
401+
ResolutionError::AttemptToUseNonConstantValueInConstant(ident, sugg, current) => {
402402
let mut err = struct_span_err!(
403403
self.session,
404404
span,
405405
E0435,
406406
"attempt to use a non-constant value in a constant"
407407
);
408-
err.span_suggestion(
409-
ident.span,
410-
&sugg,
411-
"".to_string(),
412-
Applicability::MaybeIncorrect,
413-
);
414-
err.span_label(span, "non-constant value");
408+
// let foo =...
409+
// ^^^ given this Span
410+
// ------- get this Span to have an applicable suggestion
411+
let sp =
412+
self.session.source_map().span_extend_to_prev_str(ident.span, current, true);
413+
if sp.lo().0 == 0 {
414+
err.span_label(ident.span, &format!("this would need to be a `{}`", sugg));
415+
} else {
416+
let sp = sp.with_lo(BytePos(sp.lo().0 - current.len() as u32));
417+
err.span_suggestion(
418+
sp,
419+
&format!("consider using `{}` instead of `{}`", sugg, current),
420+
format!("{} {}", sugg, ident),
421+
Applicability::MaybeIncorrect,
422+
);
423+
err.span_label(span, "non-constant value");
424+
}
415425
err
416426
}
417427
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => {

compiler/rustc_resolve/src/lib.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ enum ResolutionError<'a> {
210210
/// Error E0434: can't capture dynamic environment in a fn item.
211211
CannotCaptureDynamicEnvironmentInFnItem,
212212
/// Error E0435: attempt to use a non-constant value in a constant.
213-
AttemptToUseNonConstantValueInConstant(Ident, String),
213+
AttemptToUseNonConstantValueInConstant(
214+
Ident,
215+
/* suggestion */ &'static str,
216+
/* current */ &'static str,
217+
),
214218
/// Error E0530: `X` bindings cannot shadow `Y`s.
215219
BindingShadowsSomethingUnacceptable(&'static str, Symbol, &'a NameBinding<'a>),
216220
/// Error E0128: type parameters with a default cannot use forward-declared identifiers.
@@ -2614,18 +2618,19 @@ impl<'a> Resolver<'a> {
26142618
ConstantItemKind::Const => "const",
26152619
ConstantItemKind::Static => "static",
26162620
};
2617-
let sugg = format!(
2618-
"consider using `let` instead of `{}`",
2619-
kind_str
2620-
);
2621-
(span, AttemptToUseNonConstantValueInConstant(ident, sugg))
2621+
(
2622+
span,
2623+
AttemptToUseNonConstantValueInConstant(
2624+
ident, "let", kind_str,
2625+
),
2626+
)
26222627
} else {
2623-
let sugg = "consider using `const` instead of `let`";
26242628
(
26252629
rib_ident.span,
26262630
AttemptToUseNonConstantValueInConstant(
26272631
original_rib_ident_def,
2628-
sugg.to_string(),
2632+
"const",
2633+
"let",
26292634
),
26302635
)
26312636
};

compiler/rustc_span/src/source_map.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,9 @@ impl SourceMap {
671671
let pat = pat.to_owned() + ws;
672672
if let Ok(prev_source) = self.span_to_prev_source(sp) {
673673
let prev_source = prev_source.rsplit(&pat).next().unwrap_or("").trim_start();
674-
if !prev_source.is_empty() && (!prev_source.contains('\n') || accept_newlines) {
674+
if prev_source.is_empty() && sp.lo().0 != 0 {
675+
return sp.with_lo(BytePos(sp.lo().0 - 1));
676+
} else if !prev_source.contains('\n') || accept_newlines {
675677
return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
676678
}
677679
}

src/test/ui/error-codes/E0435.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-rustfix
2+
fn main () {
3+
#[allow(non_upper_case_globals)]
4+
const foo: usize = 42;
5+
let _: [u8; foo]; //~ ERROR E0435
6+
}

src/test/ui/error-codes/E0435.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
// run-rustfix
12
fn main () {
2-
let foo = 42u32;
3+
#[allow(non_upper_case_globals)]
4+
let foo: usize = 42;
35
let _: [u8; foo]; //~ ERROR E0435
46
}

src/test/ui/error-codes/E0435.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0435]: attempt to use a non-constant value in a constant
2-
--> $DIR/E0435.rs:3:17
2+
--> $DIR/E0435.rs:5:17
33
|
4-
LL | let foo = 42u32;
5-
| --- help: consider using `const` instead of `let`
4+
LL | let foo: usize = 42;
5+
| ------- help: consider using `const` instead of `let`: `const foo`
66
LL | let _: [u8; foo];
77
| ^^^ non-constant value
88

src/test/ui/impl-trait/bindings.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ error[E0435]: attempt to use a non-constant value in a constant
22
--> $DIR/bindings.rs:5:29
33
|
44
LL | const foo: impl Clone = x;
5-
| --- ^ non-constant value
6-
| |
7-
| help: consider using `let` instead of `const`
5+
| --------- ^ non-constant value
6+
| |
7+
| help: consider using `let` instead of `const`: `let foo`
88

99
error[E0435]: attempt to use a non-constant value in a constant
1010
--> $DIR/bindings.rs:11:33
1111
|
1212
LL | const foo: impl Clone = x;
13-
| --- ^ non-constant value
14-
| |
15-
| help: consider using `let` instead of `const`
13+
| --------- ^ non-constant value
14+
| |
15+
| help: consider using `let` instead of `const`: `let foo`
1616

1717
error[E0435]: attempt to use a non-constant value in a constant
1818
--> $DIR/bindings.rs:18:33
1919
|
2020
LL | const foo: impl Clone = x;
21-
| --- ^ non-constant value
22-
| |
23-
| help: consider using `let` instead of `const`
21+
| --------- ^ non-constant value
22+
| |
23+
| help: consider using `let` instead of `const`: `let foo`
2424

2525
error[E0435]: attempt to use a non-constant value in a constant
2626
--> $DIR/bindings.rs:25:33
2727
|
2828
LL | const foo: impl Clone = x;
29-
| --- ^ non-constant value
30-
| |
31-
| help: consider using `let` instead of `const`
29+
| --------- ^ non-constant value
30+
| |
31+
| help: consider using `let` instead of `const`: `let foo`
3232

3333
warning: the feature `impl_trait_in_bindings` is incomplete and may not be safe to use and/or cause compiler crashes
3434
--> $DIR/bindings.rs:1:12

src/test/ui/issues/issue-27433.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-rustfix
2+
fn main() {
3+
let foo = 42u32;
4+
#[allow(unused_variables, non_snake_case)]
5+
let FOO : u32 = foo;
6+
//~^ ERROR attempt to use a non-constant value in a constant
7+
}

src/test/ui/issues/issue-27433.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// run-rustfix
12
fn main() {
23
let foo = 42u32;
4+
#[allow(unused_variables, non_snake_case)]
35
const FOO : u32 = foo;
46
//~^ ERROR attempt to use a non-constant value in a constant
57
}

src/test/ui/issues/issue-27433.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0435]: attempt to use a non-constant value in a constant
2-
--> $DIR/issue-27433.rs:3:23
2+
--> $DIR/issue-27433.rs:5:23
33
|
44
LL | const FOO : u32 = foo;
5-
| --- ^^^ non-constant value
6-
| |
7-
| help: consider using `let` instead of `const`
5+
| --------- ^^^ non-constant value
6+
| |
7+
| help: consider using `let` instead of `const`: `let FOO`
88

99
error: aborting due to previous error
1010

src/test/ui/issues/issue-3521-2.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-rustfix
2+
fn main() {
3+
let foo = 100;
4+
5+
let y: isize = foo + 1;
6+
//~^ ERROR attempt to use a non-constant value in a constant
7+
8+
println!("{}", y);
9+
}

src/test/ui/issues/issue-3521-2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
fn main() {
23
let foo = 100;
34

src/test/ui/issues/issue-3521-2.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0435]: attempt to use a non-constant value in a constant
2-
--> $DIR/issue-3521-2.rs:4:23
2+
--> $DIR/issue-3521-2.rs:5:23
33
|
44
LL | static y: isize = foo + 1;
5-
| - ^^^ non-constant value
6-
| |
7-
| help: consider using `let` instead of `static`
5+
| -------- ^^^ non-constant value
6+
| |
7+
| help: consider using `let` instead of `static`: `let y`
88

99
error: aborting due to previous error
1010

src/test/ui/issues/issue-3521.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
fn main() {
3+
#[allow(non_upper_case_globals)]
4+
const foo: isize = 100;
5+
6+
#[derive(Debug)]
7+
enum Stuff {
8+
Bar = foo
9+
//~^ ERROR attempt to use a non-constant value in a constant
10+
}
11+
12+
println!("{:?}", Stuff::Bar);
13+
}

src/test/ui/issues/issue-3521.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// run-rustfix
12
fn main() {
2-
let foo = 100;
3+
#[allow(non_upper_case_globals)]
4+
let foo: isize = 100;
35

46
#[derive(Debug)]
57
enum Stuff {

src/test/ui/issues/issue-3521.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0435]: attempt to use a non-constant value in a constant
2-
--> $DIR/issue-3521.rs:6:15
2+
--> $DIR/issue-3521.rs:8:15
33
|
4-
LL | let foo = 100;
5-
| --- help: consider using `const` instead of `let`
4+
LL | let foo: isize = 100;
5+
| ------- help: consider using `const` instead of `let`: `const foo`
66
...
77
LL | Bar = foo
88
| ^^^ non-constant value

src/test/ui/issues/issue-3668-2.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
#![allow(unused_variables, dead_code)]
3+
fn f(x:isize) {
4+
let child: isize = x + 1;
5+
//~^ ERROR attempt to use a non-constant value in a constant
6+
}
7+
8+
fn main() {}

src/test/ui/issues/issue-3668-2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
#![allow(unused_variables, dead_code)]
13
fn f(x:isize) {
24
static child: isize = x + 1;
35
//~^ ERROR attempt to use a non-constant value in a constant

src/test/ui/issues/issue-3668-2.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0435]: attempt to use a non-constant value in a constant
2-
--> $DIR/issue-3668-2.rs:2:27
2+
--> $DIR/issue-3668-2.rs:4:27
33
|
44
LL | static child: isize = x + 1;
5-
| ----- ^ non-constant value
6-
| |
7-
| help: consider using `let` instead of `static`
5+
| ------------ ^ non-constant value
6+
| |
7+
| help: consider using `let` instead of `static`: `let child`
88

99
error: aborting due to previous error
1010

src/test/ui/issues/issue-3668.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error[E0435]: attempt to use a non-constant value in a constant
22
--> $DIR/issue-3668.rs:8:34
33
|
44
LL | static childVal: Box<P> = self.child.get();
5-
| -------- ^^^^ non-constant value
6-
| |
7-
| help: consider using `let` instead of `static`
5+
| --------------- ^^^^ non-constant value
6+
| |
7+
| help: consider using `let` instead of `static`: `let childVal`
88

99
error: aborting due to previous error
1010

src/test/ui/issues/issue-42060.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ error[E0435]: attempt to use a non-constant value in a constant
22
--> $DIR/issue-42060.rs:3:23
33
|
44
LL | let thing = ();
5-
| ----- help: consider using `const` instead of `let`
5+
| --------- help: consider using `const` instead of `let`: `const thing`
66
LL | let other: typeof(thing) = thing;
77
| ^^^^^ non-constant value
88

99
error[E0435]: attempt to use a non-constant value in a constant
1010
--> $DIR/issue-42060.rs:9:13
1111
|
1212
LL | let q = 1;
13-
| - help: consider using `const` instead of `let`
13+
| ----- help: consider using `const` instead of `let`: `const q`
1414
LL | <typeof(q)>::N
1515
| ^ non-constant value
1616

src/test/ui/issues/issue-44239.fixed

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
#![allow(dead_code, non_upper_case_globals)]
3+
fn main() {
4+
const n: usize = 0;
5+
6+
struct Foo;
7+
impl Foo {
8+
const N: usize = n;
9+
//~^ ERROR attempt to use a non-constant value
10+
}
11+
}

src/test/ui/issues/issue-44239.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// run-rustfix
2+
#![allow(dead_code, non_upper_case_globals)]
13
fn main() {
2-
let n = 0;
4+
let n: usize = 0;
35

46
struct Foo;
57
impl Foo {

src/test/ui/issues/issue-44239.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0435]: attempt to use a non-constant value in a constant
2-
--> $DIR/issue-44239.rs:6:26
2+
--> $DIR/issue-44239.rs:8:26
33
|
4-
LL | let n = 0;
5-
| - help: consider using `const` instead of `let`
4+
LL | let n: usize = 0;
5+
| ----- help: consider using `const` instead of `let`: `const n`
66
...
77
LL | const N: usize = n;
88
| ^ non-constant value

src/test/ui/non-constant-expr-for-arr-len.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error[E0435]: attempt to use a non-constant value in a constant
22
--> $DIR/non-constant-expr-for-arr-len.rs:5:22
33
|
44
LL | fn bar(n: usize) {
5-
| - help: consider using `const` instead of `let`
5+
| - this would need to be a `const`
66
LL | let _x = [0; n];
7-
| ^ non-constant value
7+
| ^
88

99
error: aborting due to previous error
1010

src/test/ui/repeat_count.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0435]: attempt to use a non-constant value in a constant
22
--> $DIR/repeat_count.rs:5:17
33
|
44
LL | let n = 1;
5-
| - help: consider using `const` instead of `let`
5+
| ----- help: consider using `const` instead of `let`: `const n`
66
LL | let a = [0; n];
77
| ^ non-constant value
88

src/test/ui/type/type-dependent-def-issue-49241.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error[E0435]: attempt to use a non-constant value in a constant
22
--> $DIR/type-dependent-def-issue-49241.rs:3:22
33
|
44
LL | const l: usize = v.count();
5-
| - ^ non-constant value
6-
| |
7-
| help: consider using `let` instead of `const`
5+
| ------- ^ non-constant value
6+
| |
7+
| help: consider using `let` instead of `const`: `let l`
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)