]> git.lizzy.rs Git - rust.git/commitdiff
Use correct span for structured suggestion
authorEsteban Küber <esteban@kuber.com.ar>
Fri, 8 Jan 2021 00:44:08 +0000 (16:44 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 8 Jan 2021 00:52:44 +0000 (16:52 -0800)
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.

27 files changed:
compiler/rustc_resolve/src/diagnostics.rs
compiler/rustc_resolve/src/lib.rs
compiler/rustc_span/src/source_map.rs
src/test/ui/error-codes/E0435.fixed [new file with mode: 0644]
src/test/ui/error-codes/E0435.rs
src/test/ui/error-codes/E0435.stderr
src/test/ui/impl-trait/bindings.stderr
src/test/ui/issues/issue-27433.fixed [new file with mode: 0644]
src/test/ui/issues/issue-27433.rs
src/test/ui/issues/issue-27433.stderr
src/test/ui/issues/issue-3521-2.fixed [new file with mode: 0644]
src/test/ui/issues/issue-3521-2.rs
src/test/ui/issues/issue-3521-2.stderr
src/test/ui/issues/issue-3521.fixed [new file with mode: 0644]
src/test/ui/issues/issue-3521.rs
src/test/ui/issues/issue-3521.stderr
src/test/ui/issues/issue-3668-2.fixed [new file with mode: 0644]
src/test/ui/issues/issue-3668-2.rs
src/test/ui/issues/issue-3668-2.stderr
src/test/ui/issues/issue-3668.stderr
src/test/ui/issues/issue-42060.stderr
src/test/ui/issues/issue-44239.fixed [new file with mode: 0644]
src/test/ui/issues/issue-44239.rs
src/test/ui/issues/issue-44239.stderr
src/test/ui/non-constant-expr-for-arr-len.stderr
src/test/ui/repeat_count.stderr
src/test/ui/type/type-dependent-def-issue-49241.stderr

index 6a181dbab5af76b6a298caa604927c528bdcdd86..03b66a3f7b30690471c15828aa1ebd79e39333bf 100644 (file)
@@ -398,20 +398,30 @@ impl<'a> Resolver<'a> {
                 err.help("use the `|| { ... }` closure form instead");
                 err
             }
-            ResolutionError::AttemptToUseNonConstantValueInConstant(ident, sugg) => {
+            ResolutionError::AttemptToUseNonConstantValueInConstant(ident, sugg, current) => {
                 let mut err = struct_span_err!(
                     self.session,
                     span,
                     E0435,
                     "attempt to use a non-constant value in a constant"
                 );
-                err.span_suggestion(
-                    ident.span,
-                    &sugg,
-                    "".to_string(),
-                    Applicability::MaybeIncorrect,
-                );
-                err.span_label(span, "non-constant value");
+                // let foo =...
+                //     ^^^ given this Span
+                // ------- get this Span to have an applicable suggestion
+                let sp =
+                    self.session.source_map().span_extend_to_prev_str(ident.span, current, true);
+                if sp.lo().0 == 0 {
+                    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
             }
             ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => {
index a6d0240b6fdcf7a190fa4bc5b60d3af144b8ff59..2c68e0418589b6839dbbe513f83bd9f81a3018c9 100644 (file)
@@ -210,7 +210,11 @@ enum ResolutionError<'a> {
     /// Error E0434: can't capture dynamic environment in a fn item.
     CannotCaptureDynamicEnvironmentInFnItem,
     /// 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.
     BindingShadowsSomethingUnacceptable(&'static str, Symbol, &'a NameBinding<'a>),
     /// Error E0128: type parameters with a default cannot use forward-declared identifiers.
@@ -2614,18 +2618,19 @@ fn validate_res_from_ribs(
                                             ConstantItemKind::Const => "const",
                                             ConstantItemKind::Static => "static",
                                         };
-                                        let sugg = format!(
-                                            "consider using `let` instead of `{}`",
-                                            kind_str
-                                        );
-                                        (span, AttemptToUseNonConstantValueInConstant(ident, sugg))
+                                        (
+                                            span,
+                                            AttemptToUseNonConstantValueInConstant(
+                                                ident, "let", kind_str,
+                                            ),
+                                        )
                                     } else {
-                                        let sugg = "consider using `const` instead of `let`";
                                         (
                                             rib_ident.span,
                                             AttemptToUseNonConstantValueInConstant(
                                                 original_rib_ident_def,
-                                                sugg.to_string(),
+                                                "const",
+                                                "let",
                                             ),
                                         )
                                     };
index fefc0cb48ddd8efea9b86f5346d4b2555ebcd75c..6635d44496c0302177eed3dae96412c12164353f 100644 (file)
@@ -671,7 +671,9 @@ pub fn span_extend_to_prev_str(&self, sp: Span, pat: &str, accept_newlines: bool
             let pat = pat.to_owned() + ws;
             if let Ok(prev_source) = self.span_to_prev_source(sp) {
                 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));
                 }
             }
diff --git a/src/test/ui/error-codes/E0435.fixed b/src/test/ui/error-codes/E0435.fixed
new file mode 100644 (file)
index 0000000..fdf896d
--- /dev/null
@@ -0,0 +1,6 @@
+// run-rustfix
+fn main () {
+    #[allow(non_upper_case_globals)]
+    const foo: usize = 42;
+    let _: [u8; foo]; //~ ERROR E0435
+}
index 620dd30a23bc94c356b05a49ceca6dcadb81151c..d9354efb8fdc4594e4b57509f024178bbbe6ead1 100644 (file)
@@ -1,4 +1,6 @@
+// run-rustfix
 fn main () {
-    let foo = 42u32;
+    #[allow(non_upper_case_globals)]
+    let foo: usize = 42;
     let _: [u8; foo]; //~ ERROR E0435
 }
index 21827d1fd874372ac93c3a0af9c663979202e7a6..fc08fade91cee5c9f0ce7a5ce08ad9e0721531f4 100644 (file)
@@ -1,8 +1,8 @@
 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;
-   |         --- help: consider using `const` instead of `let`
+LL |     let foo: usize = 42;
+   |     ------- help: consider using `const` instead of `let`: `const foo`
 LL |     let _: [u8; foo];
    |                 ^^^ non-constant value
 
index ad5f13d0672306c07b63d41a0607aa87c44fc20e..4da49f4dc7db167fbee93f1f5fa6f7faa6f30709 100644 (file)
@@ -2,33 +2,33 @@ error[E0435]: attempt to use a non-constant value in a constant
   --> $DIR/bindings.rs:5:29
    |
 LL |     const foo: impl Clone = x;
-   |           ---               ^ non-constant value
-   |           |
-   |           help: consider using `let` instead of `const`
+   |     ---------               ^ non-constant value
+   |     |
+   |     help: consider using `let` instead of `const`: `let foo`
 
 error[E0435]: attempt to use a non-constant value in a constant
   --> $DIR/bindings.rs:11:33
    |
 LL |         const foo: impl Clone = x;
-   |               ---               ^ non-constant value
-   |               |
-   |               help: consider using `let` instead of `const`
+   |         ---------               ^ non-constant value
+   |         |
+   |         help: consider using `let` instead of `const`: `let foo`
 
 error[E0435]: attempt to use a non-constant value in a constant
   --> $DIR/bindings.rs:18:33
    |
 LL |         const foo: impl Clone = x;
-   |               ---               ^ non-constant value
-   |               |
-   |               help: consider using `let` instead of `const`
+   |         ---------               ^ non-constant value
+   |         |
+   |         help: consider using `let` instead of `const`: `let foo`
 
 error[E0435]: attempt to use a non-constant value in a constant
   --> $DIR/bindings.rs:25:33
    |
 LL |         const foo: impl Clone = x;
-   |               ---               ^ non-constant value
-   |               |
-   |               help: consider using `let` instead of `const`
+   |         ---------               ^ non-constant value
+   |         |
+   |         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
   --> $DIR/bindings.rs:1:12
diff --git a/src/test/ui/issues/issue-27433.fixed b/src/test/ui/issues/issue-27433.fixed
new file mode 100644 (file)
index 0000000..ce31f6b
--- /dev/null
@@ -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
+}
index 156ae68efe2c50235036c981b89870ca431a95a4..01411a51c1372c5d5b4d374ce54450bce6b46f17 100644 (file)
@@ -1,5 +1,7 @@
+// run-rustfix
 fn main() {
     let foo = 42u32;
+    #[allow(unused_variables, non_snake_case)]
     const FOO : u32 = foo;
                    //~^ ERROR attempt to use a non-constant value in a constant
 }
index 201b7e8549cb17ba06a4253d24fd89e9098f8ae5..da751a64957363be2bf1b69a7b16060008273dfd 100644 (file)
@@ -1,10 +1,10 @@
 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;
-   |           ---         ^^^ non-constant value
-   |           |
-   |           help: consider using `let` instead of `const`
+   |     ---------         ^^^ non-constant value
+   |     |
+   |     help: consider using `let` instead of `const`: `let FOO`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/issues/issue-3521-2.fixed b/src/test/ui/issues/issue-3521-2.fixed
new file mode 100644 (file)
index 0000000..140c24b
--- /dev/null
@@ -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);
+}
index 871394f9eaeb96147d2b1144a3cad6988c91d9da..f66efec45e549ab2d81279f106793d8f8917db9e 100644 (file)
@@ -1,3 +1,4 @@
+// run-rustfix
 fn main() {
     let foo = 100;
 
index ba29d1becb85afa1772ed8db8151b511461a0d85..84c7a9efa35bbf61546b7f75d9c26db61f255646 100644 (file)
@@ -1,10 +1,10 @@
 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;
-   |            -          ^^^ non-constant value
-   |            |
-   |            help: consider using `let` instead of `static`
+   |     --------          ^^^ non-constant value
+   |     |
+   |     help: consider using `let` instead of `static`: `let y`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/issues/issue-3521.fixed b/src/test/ui/issues/issue-3521.fixed
new file mode 100644 (file)
index 0000000..f76106d
--- /dev/null
@@ -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);
+}
index 9e72dd29a408ecd490044c2d20d1b6e5f01e71a9..c425a22df91737afb327b28247611b3093c96def 100644 (file)
@@ -1,5 +1,7 @@
+// run-rustfix
 fn main() {
-    let foo = 100;
+    #[allow(non_upper_case_globals)]
+    let foo: isize = 100;
 
     #[derive(Debug)]
     enum Stuff {
index 8473526006c5c80fb3d365526d31aa9f4ce6e687..aa42772f12d8a4c0f5b528ff5c1bb112294afaa6 100644 (file)
@@ -1,8 +1,8 @@
 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;
-   |         --- help: consider using `const` instead of `let`
+LL |     let foo: isize = 100;
+   |     ------- help: consider using `const` instead of `let`: `const foo`
 ...
 LL |         Bar = foo
    |               ^^^ non-constant value
diff --git a/src/test/ui/issues/issue-3668-2.fixed b/src/test/ui/issues/issue-3668-2.fixed
new file mode 100644 (file)
index 0000000..a95781c
--- /dev/null
@@ -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() {}
index 525f6f5684e70f52a31dde057384e7f67d1bb700..8aa0897ecb4dc791cf9990716c99b8f63035143a 100644 (file)
@@ -1,3 +1,5 @@
+// run-rustfix
+#![allow(unused_variables, dead_code)]
 fn f(x:isize) {
     static child: isize = x + 1;
     //~^ ERROR attempt to use a non-constant value in a constant
index 7cee497b0bcedffc8dacfdafb0095cc1539c470d..ba96510415435125df33dc53af8a02329baa4a9d 100644 (file)
@@ -1,10 +1,10 @@
 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;
-   |            -----          ^ non-constant value
-   |            |
-   |            help: consider using `let` instead of `static`
+   |     ------------          ^ non-constant value
+   |     |
+   |     help: consider using `let` instead of `static`: `let child`
 
 error: aborting due to previous error
 
index e45472929ab31ee1a0fc45155a70399adafb16ef..edc49979c10a08abc75429468f23ef8d754213de 100644 (file)
@@ -2,9 +2,9 @@ error[E0435]: attempt to use a non-constant value in a constant
   --> $DIR/issue-3668.rs:8:34
    |
 LL |        static childVal: Box<P> = self.child.get();
-   |               --------           ^^^^ non-constant value
-   |               |
-   |               help: consider using `let` instead of `static`
+   |        ---------------           ^^^^ non-constant value
+   |        |
+   |        help: consider using `let` instead of `static`: `let childVal`
 
 error: aborting due to previous error
 
index dc089b856bb234531408b1e2316ec64186df506b..effcbe4d7f3e868121aaf41d2f2f0a262e267727 100644 (file)
@@ -2,7 +2,7 @@ error[E0435]: attempt to use a non-constant value in a constant
   --> $DIR/issue-42060.rs:3:23
    |
 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;
    |                       ^^^^^ 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
    |
 LL |     let q = 1;
-   |         - help: consider using `const` instead of `let`
+   |     ----- help: consider using `const` instead of `let`: `const q`
 LL |     <typeof(q)>::N
    |             ^ non-constant value
 
diff --git a/src/test/ui/issues/issue-44239.fixed b/src/test/ui/issues/issue-44239.fixed
new file mode 100644 (file)
index 0000000..e6c29ce
--- /dev/null
@@ -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
+    }
+}
index 99a865f75a498eb2b3d369bb4bf21a704e4b1c23..482ed194c7a1c117a19ad39219fbdc5691a24eb1 100644 (file)
@@ -1,5 +1,7 @@
+// run-rustfix
+#![allow(dead_code, non_upper_case_globals)]
 fn main() {
-    let n = 0;
+    let n: usize = 0;
 
     struct Foo;
     impl Foo {
index bbd3d116c9634530868aab405e404879c54e0141..2a245c92c4868ff0be861be7b1b5758eecc5d017 100644 (file)
@@ -1,8 +1,8 @@
 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;
-   |         - help: consider using `const` instead of `let`
+LL |     let n: usize = 0;
+   |     ----- help: consider using `const` instead of `let`: `const n`
 ...
 LL |         const N: usize = n;
    |                          ^ non-constant value
index 01da6bcf49aaa815e5f5c49efa0c989ace49997e..d684b8eaabdfe91652a2824fbdd3c3ede6c2f3ce 100644 (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
    |
 LL |     fn bar(n: usize) {
-   |            - help: consider using `const` instead of `let`
+   |            - this would need to be a `const`
 LL |         let _x = [0; n];
-   |                      ^ non-constant value
+   |                      ^
 
 error: aborting due to previous error
 
index aa1b2e60d51f865408e06cc27a59a3ed6cd4b3f4..e90754e9118d25e9e5728441630e087e22c1e066 100644 (file)
@@ -2,7 +2,7 @@ error[E0435]: attempt to use a non-constant value in a constant
   --> $DIR/repeat_count.rs:5:17
    |
 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];
    |                 ^ non-constant value
 
index df791435e88b95d8017f5f8cc5ed23c4868e6a21..64c7687f7a882d600af96e075bc78c7f9aff7afd 100644 (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
    |
 LL |     const l: usize = v.count();
-   |           -          ^ non-constant value
-   |           |
-   |           help: consider using `let` instead of `const`
+   |     -------          ^ non-constant value
+   |     |
+   |     help: consider using `let` instead of `const`: `let l`
 
 error: aborting due to previous error