1
Fork 0

Rollup merge of #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 #80012.
This commit is contained in:
Yuki Okushi 2021-01-10 16:55:59 +09:00 committed by GitHub
commit 700f3f23d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 146 additions and 64 deletions

View file

@ -398,20 +398,30 @@ impl<'a> Resolver<'a> {
err.help("use the `|| { ... }` closure form instead"); err.help("use the `|| { ... }` closure form instead");
err err
} }
ResolutionError::AttemptToUseNonConstantValueInConstant(ident, sugg) => { ResolutionError::AttemptToUseNonConstantValueInConstant(ident, sugg, current) => {
let mut err = struct_span_err!( let mut err = struct_span_err!(
self.session, self.session,
span, span,
E0435, E0435,
"attempt to use a non-constant value in a constant" "attempt to use a non-constant value in a constant"
); );
err.span_suggestion( // let foo =...
ident.span, // ^^^ given this Span
&sugg, // ------- get this Span to have an applicable suggestion
"".to_string(), let sp =
Applicability::MaybeIncorrect, self.session.source_map().span_extend_to_prev_str(ident.span, current, true);
); if sp.lo().0 == 0 {
err.span_label(span, "non-constant value"); err.span_label(ident.span, &format!("this would need to be a `{}`", sugg));
} else {
let sp = sp.with_lo(BytePos(sp.lo().0 - current.len() as u32));
err.span_suggestion(
sp,
&format!("consider using `{}` instead of `{}`", sugg, current),
format!("{} {}", sugg, ident),
Applicability::MaybeIncorrect,
);
err.span_label(span, "non-constant value");
}
err err
} }
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => { ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => {

View file

@ -210,7 +210,11 @@ enum ResolutionError<'a> {
/// Error E0434: can't capture dynamic environment in a fn item. /// Error E0434: can't capture dynamic environment in a fn item.
CannotCaptureDynamicEnvironmentInFnItem, CannotCaptureDynamicEnvironmentInFnItem,
/// Error E0435: attempt to use a non-constant value in a constant. /// Error E0435: attempt to use a non-constant value in a constant.
AttemptToUseNonConstantValueInConstant(Ident, String), AttemptToUseNonConstantValueInConstant(
Ident,
/* suggestion */ &'static str,
/* current */ &'static str,
),
/// Error E0530: `X` bindings cannot shadow `Y`s. /// Error E0530: `X` bindings cannot shadow `Y`s.
BindingShadowsSomethingUnacceptable(&'static str, Symbol, &'a NameBinding<'a>), BindingShadowsSomethingUnacceptable(&'static str, Symbol, &'a NameBinding<'a>),
/// Error E0128: type parameters with a default cannot use forward-declared identifiers. /// Error E0128: type parameters with a default cannot use forward-declared identifiers.
@ -2614,18 +2618,19 @@ impl<'a> Resolver<'a> {
ConstantItemKind::Const => "const", ConstantItemKind::Const => "const",
ConstantItemKind::Static => "static", ConstantItemKind::Static => "static",
}; };
let sugg = format!( (
"consider using `let` instead of `{}`", span,
kind_str AttemptToUseNonConstantValueInConstant(
); ident, "let", kind_str,
(span, AttemptToUseNonConstantValueInConstant(ident, sugg)) ),
)
} else { } else {
let sugg = "consider using `const` instead of `let`";
( (
rib_ident.span, rib_ident.span,
AttemptToUseNonConstantValueInConstant( AttemptToUseNonConstantValueInConstant(
original_rib_ident_def, original_rib_ident_def,
sugg.to_string(), "const",
"let",
), ),
) )
}; };

View file

@ -671,7 +671,9 @@ impl SourceMap {
let pat = pat.to_owned() + ws; let pat = pat.to_owned() + ws;
if let Ok(prev_source) = self.span_to_prev_source(sp) { if let Ok(prev_source) = self.span_to_prev_source(sp) {
let prev_source = prev_source.rsplit(&pat).next().unwrap_or("").trim_start(); let prev_source = prev_source.rsplit(&pat).next().unwrap_or("").trim_start();
if !prev_source.is_empty() && (!prev_source.contains('\n') || accept_newlines) { if prev_source.is_empty() && sp.lo().0 != 0 {
return sp.with_lo(BytePos(sp.lo().0 - 1));
} else if !prev_source.contains('\n') || accept_newlines {
return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32)); return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
} }
} }

View file

@ -0,0 +1,6 @@
// run-rustfix
fn main () {
#[allow(non_upper_case_globals)]
const foo: usize = 42;
let _: [u8; foo]; //~ ERROR E0435
}

View file

@ -1,4 +1,6 @@
// run-rustfix
fn main () { fn main () {
let foo = 42u32; #[allow(non_upper_case_globals)]
let foo: usize = 42;
let _: [u8; foo]; //~ ERROR E0435 let _: [u8; foo]; //~ ERROR E0435
} }

View file

@ -1,8 +1,8 @@
error[E0435]: attempt to use a non-constant value in a constant error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/E0435.rs:3:17 --> $DIR/E0435.rs:5:17
| |
LL | let foo = 42u32; LL | let foo: usize = 42;
| --- help: consider using `const` instead of `let` | ------- help: consider using `const` instead of `let`: `const foo`
LL | let _: [u8; foo]; LL | let _: [u8; foo];
| ^^^ non-constant value | ^^^ non-constant value

View file

@ -2,33 +2,33 @@ error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/bindings.rs:5:29 --> $DIR/bindings.rs:5:29
| |
LL | const foo: impl Clone = x; LL | const foo: impl Clone = x;
| --- ^ non-constant value | --------- ^ non-constant value
| | | |
| help: consider using `let` instead of `const` | help: consider using `let` instead of `const`: `let foo`
error[E0435]: attempt to use a non-constant value in a constant error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/bindings.rs:11:33 --> $DIR/bindings.rs:11:33
| |
LL | const foo: impl Clone = x; LL | const foo: impl Clone = x;
| --- ^ non-constant value | --------- ^ non-constant value
| | | |
| help: consider using `let` instead of `const` | help: consider using `let` instead of `const`: `let foo`
error[E0435]: attempt to use a non-constant value in a constant error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/bindings.rs:18:33 --> $DIR/bindings.rs:18:33
| |
LL | const foo: impl Clone = x; LL | const foo: impl Clone = x;
| --- ^ non-constant value | --------- ^ non-constant value
| | | |
| help: consider using `let` instead of `const` | help: consider using `let` instead of `const`: `let foo`
error[E0435]: attempt to use a non-constant value in a constant error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/bindings.rs:25:33 --> $DIR/bindings.rs:25:33
| |
LL | const foo: impl Clone = x; LL | const foo: impl Clone = x;
| --- ^ non-constant value | --------- ^ non-constant value
| | | |
| help: consider using `let` instead of `const` | help: consider using `let` instead of `const`: `let foo`
warning: the feature `impl_trait_in_bindings` is incomplete and may not be safe to use and/or cause compiler crashes warning: the feature `impl_trait_in_bindings` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/bindings.rs:1:12 --> $DIR/bindings.rs:1:12

View file

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

View file

@ -1,5 +1,7 @@
// run-rustfix
fn main() { fn main() {
let foo = 42u32; let foo = 42u32;
#[allow(unused_variables, non_snake_case)]
const FOO : u32 = foo; const FOO : u32 = foo;
//~^ ERROR attempt to use a non-constant value in a constant //~^ ERROR attempt to use a non-constant value in a constant
} }

View file

@ -1,10 +1,10 @@
error[E0435]: attempt to use a non-constant value in a constant error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-27433.rs:3:23 --> $DIR/issue-27433.rs:5:23
| |
LL | const FOO : u32 = foo; LL | const FOO : u32 = foo;
| --- ^^^ non-constant value | --------- ^^^ non-constant value
| | | |
| help: consider using `let` instead of `const` | help: consider using `let` instead of `const`: `let FOO`
error: aborting due to previous error error: aborting due to previous error

View file

@ -0,0 +1,9 @@
// run-rustfix
fn main() {
let foo = 100;
let y: isize = foo + 1;
//~^ ERROR attempt to use a non-constant value in a constant
println!("{}", y);
}

View file

@ -1,3 +1,4 @@
// run-rustfix
fn main() { fn main() {
let foo = 100; let foo = 100;

View file

@ -1,10 +1,10 @@
error[E0435]: attempt to use a non-constant value in a constant error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-3521-2.rs:4:23 --> $DIR/issue-3521-2.rs:5:23
| |
LL | static y: isize = foo + 1; LL | static y: isize = foo + 1;
| - ^^^ non-constant value | -------- ^^^ non-constant value
| | | |
| help: consider using `let` instead of `static` | help: consider using `let` instead of `static`: `let y`
error: aborting due to previous error error: aborting due to previous error

View file

@ -0,0 +1,13 @@
// run-rustfix
fn main() {
#[allow(non_upper_case_globals)]
const foo: isize = 100;
#[derive(Debug)]
enum Stuff {
Bar = foo
//~^ ERROR attempt to use a non-constant value in a constant
}
println!("{:?}", Stuff::Bar);
}

View file

@ -1,5 +1,7 @@
// run-rustfix
fn main() { fn main() {
let foo = 100; #[allow(non_upper_case_globals)]
let foo: isize = 100;
#[derive(Debug)] #[derive(Debug)]
enum Stuff { enum Stuff {

View file

@ -1,8 +1,8 @@
error[E0435]: attempt to use a non-constant value in a constant error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-3521.rs:6:15 --> $DIR/issue-3521.rs:8:15
| |
LL | let foo = 100; LL | let foo: isize = 100;
| --- help: consider using `const` instead of `let` | ------- help: consider using `const` instead of `let`: `const foo`
... ...
LL | Bar = foo LL | Bar = foo
| ^^^ non-constant value | ^^^ non-constant value

View file

@ -0,0 +1,8 @@
// run-rustfix
#![allow(unused_variables, dead_code)]
fn f(x:isize) {
let child: isize = x + 1;
//~^ ERROR attempt to use a non-constant value in a constant
}
fn main() {}

View file

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

View file

@ -1,10 +1,10 @@
error[E0435]: attempt to use a non-constant value in a constant error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-3668-2.rs:2:27 --> $DIR/issue-3668-2.rs:4:27
| |
LL | static child: isize = x + 1; LL | static child: isize = x + 1;
| ----- ^ non-constant value | ------------ ^ non-constant value
| | | |
| help: consider using `let` instead of `static` | help: consider using `let` instead of `static`: `let child`
error: aborting due to previous error error: aborting due to previous error

View file

@ -2,9 +2,9 @@ error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-3668.rs:8:34 --> $DIR/issue-3668.rs:8:34
| |
LL | static childVal: Box<P> = self.child.get(); LL | static childVal: Box<P> = self.child.get();
| -------- ^^^^ non-constant value | --------------- ^^^^ non-constant value
| | | |
| help: consider using `let` instead of `static` | help: consider using `let` instead of `static`: `let childVal`
error: aborting due to previous error error: aborting due to previous error

View file

@ -2,7 +2,7 @@ error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-42060.rs:3:23 --> $DIR/issue-42060.rs:3:23
| |
LL | let thing = (); LL | let thing = ();
| ----- help: consider using `const` instead of `let` | --------- help: consider using `const` instead of `let`: `const thing`
LL | let other: typeof(thing) = thing; LL | let other: typeof(thing) = thing;
| ^^^^^ non-constant value | ^^^^^ non-constant value
@ -10,7 +10,7 @@ error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-42060.rs:9:13 --> $DIR/issue-42060.rs:9:13
| |
LL | let q = 1; LL | let q = 1;
| - help: consider using `const` instead of `let` | ----- help: consider using `const` instead of `let`: `const q`
LL | <typeof(q)>::N LL | <typeof(q)>::N
| ^ non-constant value | ^ non-constant value

View file

@ -0,0 +1,11 @@
// run-rustfix
#![allow(dead_code, non_upper_case_globals)]
fn main() {
const n: usize = 0;
struct Foo;
impl Foo {
const N: usize = n;
//~^ ERROR attempt to use a non-constant value
}
}

View file

@ -1,5 +1,7 @@
// run-rustfix
#![allow(dead_code, non_upper_case_globals)]
fn main() { fn main() {
let n = 0; let n: usize = 0;
struct Foo; struct Foo;
impl Foo { impl Foo {

View file

@ -1,8 +1,8 @@
error[E0435]: attempt to use a non-constant value in a constant error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/issue-44239.rs:6:26 --> $DIR/issue-44239.rs:8:26
| |
LL | let n = 0; LL | let n: usize = 0;
| - help: consider using `const` instead of `let` | ----- help: consider using `const` instead of `let`: `const n`
... ...
LL | const N: usize = n; LL | const N: usize = n;
| ^ non-constant value | ^ non-constant value

View file

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

View file

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

View file

@ -2,9 +2,9 @@ error[E0435]: attempt to use a non-constant value in a constant
--> $DIR/type-dependent-def-issue-49241.rs:3:22 --> $DIR/type-dependent-def-issue-49241.rs:3:22
| |
LL | const l: usize = v.count(); LL | const l: usize = v.count();
| - ^ non-constant value | ------- ^ non-constant value
| | | |
| help: consider using `let` instead of `const` | help: consider using `let` instead of `const`: `let l`
error: aborting due to previous error error: aborting due to previous error