From b56dc8ee90582d9c1322632d570b996bcb73ecda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 5 Jul 2024 19:32:17 +0000 Subject: [PATCH 1/3] Use verbose style when suggesting changing `const` with `let` --- compiler/rustc_parse/src/parser/item.rs | 2 +- compiler/rustc_resolve/src/diagnostics.rs | 5 ++- compiler/rustc_resolve/src/errors.rs | 4 +- tests/ui/asm/parse-error.stderr | 40 ++++++++++++------- tests/ui/asm/type-check-1.stderr | 24 ++++++----- tests/ui/asm/x86_64/x86_64_parse_error.stderr | 24 ++++++----- .../legacy-const-generics-bad.stderr | 7 +++- tests/ui/consts/issue-3521.stderr | 8 ++-- tests/ui/consts/issue-91560.stderr | 16 +++++--- .../ui/consts/non-const-value-in-const.stderr | 17 +++++--- tests/ui/error-codes/E0435.stderr | 7 +++- tests/ui/issues/issue-27433.stderr | 9 +++-- tests/ui/issues/issue-3521-2.stderr | 9 +++-- .../issue-3668-2.stderr | 9 +++-- .../issue-3668.stderr | 9 +++-- tests/ui/issues/issue-44239.stderr | 8 ++-- tests/ui/parser/suggest-assoc-const.stderr | 7 +++- tests/ui/repeat-expr/repeat_count.stderr | 7 +++- .../type-dependent-def-issue-49241.stderr | 9 +++-- tests/ui/typeof/issue-42060.stderr | 14 +++++-- 20 files changed, 153 insertions(+), 82 deletions(-) diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index f31e634f55c8b..8e3887dfb2542 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -810,7 +810,7 @@ impl<'a> Parser<'a> { self.dcx().struct_span_err(non_item_span, "non-item in item list"); self.consume_block(Delimiter::Brace, ConsumeClosingDelim::Yes); if is_let { - err.span_suggestion( + err.span_suggestion_verbose( non_item_span, "consider using `const` instead of `let` for associated const", "const", diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index ffd495aa9857e..f879e548f2754 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -836,11 +836,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let ((with, with_label), without) = match sp { Some(sp) if !self.tcx.sess.source_map().is_multiline(sp) => { - let sp = sp.with_lo(BytePos(sp.lo().0 - (current.len() as u32))); + let sp = sp + .with_lo(BytePos(sp.lo().0 - (current.len() as u32))) + .until(ident.span); ( (Some(errs::AttemptToUseNonConstantValueInConstantWithSuggestion { span: sp, - ident, suggestion, current, }), Some(errs::AttemptToUseNonConstantValueInConstantLabelWithSuggestion {span})), diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index 0620f3d709eb2..a4bdd62acbbda 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -242,13 +242,13 @@ pub(crate) struct AttemptToUseNonConstantValueInConstant<'a> { #[derive(Subdiagnostic)] #[suggestion( resolve_attempt_to_use_non_constant_value_in_constant_with_suggestion, - code = "{suggestion} {ident}", + code = "{suggestion} ", + style = "verbose", applicability = "maybe-incorrect" )] pub(crate) struct AttemptToUseNonConstantValueInConstantWithSuggestion<'a> { #[primary_span] pub(crate) span: Span, - pub(crate) ident: Ident, pub(crate) suggestion: &'a str, pub(crate) current: &'a str, } diff --git a/tests/ui/asm/parse-error.stderr b/tests/ui/asm/parse-error.stderr index 80ee5191dbbe9..8030face1d253 100644 --- a/tests/ui/asm/parse-error.stderr +++ b/tests/ui/asm/parse-error.stderr @@ -371,47 +371,57 @@ LL | global_asm!("{}", label {}); error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:39:37 | -LL | let mut foo = 0; - | ----------- help: consider using `const` instead of `let`: `const foo` -... LL | asm!("{}", options(), const foo); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const foo = 0; + | ~~~~~ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:71:44 | -LL | let mut foo = 0; - | ----------- help: consider using `const` instead of `let`: `const foo` -... LL | asm!("{}", clobber_abi("C"), const foo); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const foo = 0; + | ~~~~~ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:74:55 | -LL | let mut foo = 0; - | ----------- help: consider using `const` instead of `let`: `const foo` -... LL | asm!("{}", options(), clobber_abi("C"), const foo); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const foo = 0; + | ~~~~~ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:76:31 | -LL | let mut foo = 0; - | ----------- help: consider using `const` instead of `let`: `const foo` -... LL | asm!("{a}", a = const foo, a = const bar); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const foo = 0; + | ~~~~~ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:76:46 | -LL | let mut bar = 0; - | ----------- help: consider using `const` instead of `let`: `const bar` -... LL | asm!("{a}", a = const foo, a = const bar); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const bar = 0; + | ~~~~~ error: aborting due to 64 previous errors diff --git a/tests/ui/asm/type-check-1.stderr b/tests/ui/asm/type-check-1.stderr index 07a609c52139e..deba9e0fc544b 100644 --- a/tests/ui/asm/type-check-1.stderr +++ b/tests/ui/asm/type-check-1.stderr @@ -1,29 +1,35 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/type-check-1.rs:41:26 | -LL | let x = 0; - | ----- help: consider using `const` instead of `let`: `const x` -... LL | asm!("{}", const x); | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const x = 0; + | ~~~~~ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/type-check-1.rs:44:36 | -LL | let x = 0; - | ----- help: consider using `const` instead of `let`: `const x` -... LL | asm!("{}", const const_foo(x)); | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const x = 0; + | ~~~~~ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/type-check-1.rs:47:36 | -LL | let x = 0; - | ----- help: consider using `const` instead of `let`: `const x` -... LL | asm!("{}", const const_bar(x)); | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const x = 0; + | ~~~~~ error: invalid `sym` operand --> $DIR/type-check-1.rs:49:24 diff --git a/tests/ui/asm/x86_64/x86_64_parse_error.stderr b/tests/ui/asm/x86_64/x86_64_parse_error.stderr index f2854ae512851..95b86d533fa87 100644 --- a/tests/ui/asm/x86_64/x86_64_parse_error.stderr +++ b/tests/ui/asm/x86_64/x86_64_parse_error.stderr @@ -15,29 +15,35 @@ LL | asm!("{1}", in("eax") foo, const bar); error[E0435]: attempt to use a non-constant value in a constant --> $DIR/x86_64_parse_error.rs:13:46 | -LL | let mut bar = 0; - | ----------- help: consider using `const` instead of `let`: `const bar` -... LL | asm!("{a}", in("eax") foo, a = const bar); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const bar = 0; + | ~~~~~ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/x86_64_parse_error.rs:15:46 | -LL | let mut bar = 0; - | ----------- help: consider using `const` instead of `let`: `const bar` -... LL | asm!("{a}", in("eax") foo, a = const bar); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const bar = 0; + | ~~~~~ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/x86_64_parse_error.rs:17:42 | -LL | let mut bar = 0; - | ----------- help: consider using `const` instead of `let`: `const bar` -... LL | asm!("{1}", in("eax") foo, const bar); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const bar = 0; + | ~~~~~ error: aborting due to 5 previous errors diff --git a/tests/ui/const-generics/legacy-const-generics-bad.stderr b/tests/ui/const-generics/legacy-const-generics-bad.stderr index 83c71e07253b3..6285d47807109 100644 --- a/tests/ui/const-generics/legacy-const-generics-bad.stderr +++ b/tests/ui/const-generics/legacy-const-generics-bad.stderr @@ -1,10 +1,13 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/legacy-const-generics-bad.rs:7:35 | -LL | let a = 1; - | ----- help: consider using `const` instead of `let`: `const a` LL | legacy_const_generics::foo(0, a, 2); | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const a = 1; + | ~~~~~ error: generic parameters may not be used in const operations --> $DIR/legacy-const-generics-bad.rs:12:35 diff --git a/tests/ui/consts/issue-3521.stderr b/tests/ui/consts/issue-3521.stderr index 70ce9b2d6a08a..c0e4cdc5a94ce 100644 --- a/tests/ui/consts/issue-3521.stderr +++ b/tests/ui/consts/issue-3521.stderr @@ -1,11 +1,13 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/issue-3521.rs:8:15 | -LL | let foo: isize = 100; - | ------- help: consider using `const` instead of `let`: `const foo` -... LL | Bar = foo | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const foo: isize = 100; + | ~~~~~ error: aborting due to 1 previous error diff --git a/tests/ui/consts/issue-91560.stderr b/tests/ui/consts/issue-91560.stderr index e1b5d4cacf8e3..37c8f50d49434 100644 --- a/tests/ui/consts/issue-91560.stderr +++ b/tests/ui/consts/issue-91560.stderr @@ -1,20 +1,24 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/issue-91560.rs:10:19 | -LL | let mut length: usize = 2; - | -------------- help: consider using `const` instead of `let`: `const length` -LL | LL | let arr = [0; length]; | ^^^^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const length: usize = 2; + | ~~~~~ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/issue-91560.rs:17:19 | -LL | let length: usize = 2; - | ------------ help: consider using `const` instead of `let`: `const length` -LL | LL | let arr = [0; length]; | ^^^^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const length: usize = 2; + | ~~~~~ error: aborting due to 2 previous errors diff --git a/tests/ui/consts/non-const-value-in-const.stderr b/tests/ui/consts/non-const-value-in-const.stderr index 0ce4b4b705334..ee9ac16fe42b8 100644 --- a/tests/ui/consts/non-const-value-in-const.stderr +++ b/tests/ui/consts/non-const-value-in-const.stderr @@ -2,18 +2,23 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/non-const-value-in-const.rs:3:20 | LL | const Y: i32 = x; - | ------- ^ non-constant value - | | - | help: consider using `let` instead of `const`: `let Y` + | ^ non-constant value + | +help: consider using `let` instead of `const` + | +LL | let Y: i32 = x; + | ~~~ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/non-const-value-in-const.rs:6:17 | -LL | let x = 5; - | ----- help: consider using `const` instead of `let`: `const x` -... LL | let _ = [0; x]; | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const x = 5; + | ~~~~~ error: aborting due to 2 previous errors diff --git a/tests/ui/error-codes/E0435.stderr b/tests/ui/error-codes/E0435.stderr index 68d6ddba2a107..1ebb997639447 100644 --- a/tests/ui/error-codes/E0435.stderr +++ b/tests/ui/error-codes/E0435.stderr @@ -1,10 +1,13 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/E0435.rs:5:17 | -LL | let foo: usize = 42; - | ------- help: consider using `const` instead of `let`: `const foo` LL | let _: [u8; foo]; | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const foo: usize = 42; + | ~~~~~ error: aborting due to 1 previous error diff --git a/tests/ui/issues/issue-27433.stderr b/tests/ui/issues/issue-27433.stderr index aba8e612858a2..f6d5fc2b768f5 100644 --- a/tests/ui/issues/issue-27433.stderr +++ b/tests/ui/issues/issue-27433.stderr @@ -2,9 +2,12 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/issue-27433.rs:5:23 | LL | const FOO : u32 = foo; - | --------- ^^^ non-constant value - | | - | help: consider using `let` instead of `const`: `let FOO` + | ^^^ non-constant value + | +help: consider using `let` instead of `const` + | +LL | let FOO : u32 = foo; + | ~~~ error: aborting due to 1 previous error diff --git a/tests/ui/issues/issue-3521-2.stderr b/tests/ui/issues/issue-3521-2.stderr index 0be0e93c19e64..a12241cb1dfd5 100644 --- a/tests/ui/issues/issue-3521-2.stderr +++ b/tests/ui/issues/issue-3521-2.stderr @@ -2,9 +2,12 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/issue-3521-2.rs:5:23 | LL | static y: isize = foo + 1; - | -------- ^^^ non-constant value - | | - | help: consider using `let` instead of `static`: `let y` + | ^^^ non-constant value + | +help: consider using `let` instead of `static` + | +LL | let y: isize = foo + 1; + | ~~~ error: aborting due to 1 previous error diff --git a/tests/ui/issues/issue-3668-non-constant-value-in-constant/issue-3668-2.stderr b/tests/ui/issues/issue-3668-non-constant-value-in-constant/issue-3668-2.stderr index 3676f388891eb..9661dbf2f62fa 100644 --- a/tests/ui/issues/issue-3668-non-constant-value-in-constant/issue-3668-2.stderr +++ b/tests/ui/issues/issue-3668-non-constant-value-in-constant/issue-3668-2.stderr @@ -2,9 +2,12 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/issue-3668-2.rs:4:27 | LL | static child: isize = x + 1; - | ------------ ^ non-constant value - | | - | help: consider using `let` instead of `static`: `let child` + | ^ non-constant value + | +help: consider using `let` instead of `static` + | +LL | let child: isize = x + 1; + | ~~~ error: aborting due to 1 previous error diff --git a/tests/ui/issues/issue-3668-non-constant-value-in-constant/issue-3668.stderr b/tests/ui/issues/issue-3668-non-constant-value-in-constant/issue-3668.stderr index d761b2d87db4e..7fad45f4b1a25 100644 --- a/tests/ui/issues/issue-3668-non-constant-value-in-constant/issue-3668.stderr +++ b/tests/ui/issues/issue-3668-non-constant-value-in-constant/issue-3668.stderr @@ -2,9 +2,12 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/issue-3668.rs:8:34 | LL | static childVal: Box

= self.child.get(); - | --------------- ^^^^ non-constant value - | | - | help: consider using `let` instead of `static`: `let childVal` + | ^^^^ non-constant value + | +help: consider using `let` instead of `static` + | +LL | let childVal: Box

= self.child.get(); + | ~~~ error: aborting due to 1 previous error diff --git a/tests/ui/issues/issue-44239.stderr b/tests/ui/issues/issue-44239.stderr index 633fb177b75df..1a047d4c63b69 100644 --- a/tests/ui/issues/issue-44239.stderr +++ b/tests/ui/issues/issue-44239.stderr @@ -1,11 +1,13 @@ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/issue-44239.rs:8:26 | -LL | let n: usize = 0; - | ----- help: consider using `const` instead of `let`: `const n` -... LL | const N: usize = n; | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const n: usize = 0; + | ~~~~~ error: aborting due to 1 previous error diff --git a/tests/ui/parser/suggest-assoc-const.stderr b/tests/ui/parser/suggest-assoc-const.stderr index 6e29fad98d466..70ebeded31374 100644 --- a/tests/ui/parser/suggest-assoc-const.stderr +++ b/tests/ui/parser/suggest-assoc-const.stderr @@ -2,7 +2,12 @@ error: non-item in item list --> $DIR/suggest-assoc-const.rs:5:5 | LL | let _X: i32; - | ^^^ help: consider using `const` instead of `let` for associated const: `const` + | ^^^ + | +help: consider using `const` instead of `let` for associated const + | +LL | const _X: i32; + | ~~~~~ error: aborting due to 1 previous error diff --git a/tests/ui/repeat-expr/repeat_count.stderr b/tests/ui/repeat-expr/repeat_count.stderr index 8a1ed8f3b9c7c..9b5afc557357a 100644 --- a/tests/ui/repeat-expr/repeat_count.stderr +++ b/tests/ui/repeat-expr/repeat_count.stderr @@ -1,10 +1,13 @@ 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`: `const n` LL | let a = [0; n]; | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const n = 1; + | ~~~~~ error[E0308]: mismatched types --> $DIR/repeat_count.rs:7:17 diff --git a/tests/ui/type/type-dependent-def-issue-49241.stderr b/tests/ui/type/type-dependent-def-issue-49241.stderr index 15d47cca3d202..cf372dc59681e 100644 --- a/tests/ui/type/type-dependent-def-issue-49241.stderr +++ b/tests/ui/type/type-dependent-def-issue-49241.stderr @@ -2,9 +2,12 @@ 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`: `let l` + | ^ non-constant value + | +help: consider using `let` instead of `const` + | +LL | let l: usize = v.count(); + | ~~~ error: aborting due to 1 previous error diff --git a/tests/ui/typeof/issue-42060.stderr b/tests/ui/typeof/issue-42060.stderr index effcbe4d7f3e8..7af65633f71d9 100644 --- a/tests/ui/typeof/issue-42060.stderr +++ b/tests/ui/typeof/issue-42060.stderr @@ -1,18 +1,24 @@ 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`: `const thing` LL | let other: typeof(thing) = thing; | ^^^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const thing = (); + | ~~~~~ 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`: `const q` LL | ::N | ^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const q = 1; + | ~~~~~ error[E0516]: `typeof` is a reserved keyword but unimplemented --> $DIR/issue-42060.rs:3:16 From cbe75486f7eb6ae9fb00602f5347a09a62a7a9de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 8 Jul 2024 19:43:20 +0000 Subject: [PATCH 2/3] Account for `let foo = expr`; to suggest `const foo: Ty = expr;` --- compiler/rustc_resolve/src/diagnostics.rs | 8 +++- compiler/rustc_resolve/src/errors.rs | 10 +++-- compiler/rustc_resolve/src/ident.rs | 44 ++++++++++++++----- compiler/rustc_resolve/src/lib.rs | 11 ++--- tests/ui/asm/parse-error.stderr | 20 ++++----- tests/ui/asm/type-check-1.stderr | 12 ++--- tests/ui/asm/x86_64/x86_64_parse_error.stderr | 12 ++--- .../legacy-const-generics-bad.stderr | 4 +- .../ui/consts/non-const-value-in-const.stderr | 4 +- tests/ui/issues/issue-27433.fixed | 2 +- tests/ui/issues/issue-27433.rs | 2 +- tests/ui/repeat-expr/repeat_count.stderr | 4 +- tests/ui/typeof/issue-42060.stderr | 8 ++-- 13 files changed, 85 insertions(+), 56 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index f879e548f2754..09221041031a2 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -819,7 +819,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { ResolutionError::CannotCaptureDynamicEnvironmentInFnItem => { self.dcx().create_err(errs::CannotCaptureDynamicEnvironmentInFnItem { span }) } - ResolutionError::AttemptToUseNonConstantValueInConstant(ident, suggestion, current) => { + ResolutionError::AttemptToUseNonConstantValueInConstant { + ident, + suggestion, + current, + type_span, + } => { // let foo =... // ^^^ given this Span // ------- get this Span to have an applicable suggestion @@ -844,6 +849,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { span: sp, suggestion, current, + type_span, }), Some(errs::AttemptToUseNonConstantValueInConstantLabelWithSuggestion {span})), None, ) diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index a4bdd62acbbda..79c76e7a976a7 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -240,16 +240,18 @@ pub(crate) struct AttemptToUseNonConstantValueInConstant<'a> { } #[derive(Subdiagnostic)] -#[suggestion( +#[multipart_suggestion( resolve_attempt_to_use_non_constant_value_in_constant_with_suggestion, - code = "{suggestion} ", style = "verbose", - applicability = "maybe-incorrect" + applicability = "has-placeholders" )] pub(crate) struct AttemptToUseNonConstantValueInConstantWithSuggestion<'a> { - #[primary_span] + // #[primary_span] + #[suggestion_part(code = "{suggestion} ")] pub(crate) span: Span, pub(crate) suggestion: &'a str, + #[suggestion_part(code = ": /* Type */")] + pub(crate) type_span: Option, pub(crate) current: &'a str, } diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index 7d531385e2120..f1934ff184bc3 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -1178,21 +1178,41 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { if let Some(span) = finalize { let (span, resolution_error) = match item { None if rib_ident.as_str() == "self" => (span, LowercaseSelf), - None => ( - rib_ident.span, - AttemptToUseNonConstantValueInConstant( - original_rib_ident_def, - "const", - "let", - ), - ), + None => { + // If we have a `let name = expr;`, we have the span for + // `name` and use that to see if it is followed by a type + // specifier. If not, then we know we need to suggest + // `const name: Ty = expr;`. This is a heuristic, it will + // break down in the presence of macros. + let sm = self.tcx.sess.source_map(); + let type_span = match sm.span_look_ahead( + original_rib_ident_def.span, + ":", + None, + ) { + None => { + Some(original_rib_ident_def.span.shrink_to_hi()) + } + Some(_) => None, + }; + ( + rib_ident.span, + AttemptToUseNonConstantValueInConstant { + ident: original_rib_ident_def, + suggestion: "const", + current: "let", + type_span, + }, + ) + } Some((ident, kind)) => ( span, - AttemptToUseNonConstantValueInConstant( + AttemptToUseNonConstantValueInConstant { ident, - "let", - kind.as_str(), - ), + suggestion: "let", + current: kind.as_str(), + type_span: None, + }, ), }; self.report_error(span, resolution_error); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 7cb5a3fb2fd15..3dcb83d65b029 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -236,11 +236,12 @@ 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, - /* suggestion */ &'static str, - /* current */ &'static str, - ), + AttemptToUseNonConstantValueInConstant { + ident: Ident, + suggestion: &'static str, + current: &'static str, + type_span: Option, + }, /// Error E0530: `X` bindings cannot shadow `Y`s. BindingShadowsSomethingUnacceptable { shadowing_binding: PatternSource, diff --git a/tests/ui/asm/parse-error.stderr b/tests/ui/asm/parse-error.stderr index 8030face1d253..1999cd09aa3b5 100644 --- a/tests/ui/asm/parse-error.stderr +++ b/tests/ui/asm/parse-error.stderr @@ -376,8 +376,8 @@ LL | asm!("{}", options(), const foo); | help: consider using `const` instead of `let` | -LL | const foo = 0; - | ~~~~~ +LL | const foo: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:71:44 @@ -387,8 +387,8 @@ LL | asm!("{}", clobber_abi("C"), const foo); | help: consider using `const` instead of `let` | -LL | const foo = 0; - | ~~~~~ +LL | const foo: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:74:55 @@ -398,8 +398,8 @@ LL | asm!("{}", options(), clobber_abi("C"), const foo); | help: consider using `const` instead of `let` | -LL | const foo = 0; - | ~~~~~ +LL | const foo: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:76:31 @@ -409,8 +409,8 @@ LL | asm!("{a}", a = const foo, a = const bar); | help: consider using `const` instead of `let` | -LL | const foo = 0; - | ~~~~~ +LL | const foo: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:76:46 @@ -420,8 +420,8 @@ LL | asm!("{a}", a = const foo, a = const bar); | help: consider using `const` instead of `let` | -LL | const bar = 0; - | ~~~~~ +LL | const bar: /* Type */ = 0; + | ~~~~~ ++++++++++++ error: aborting due to 64 previous errors diff --git a/tests/ui/asm/type-check-1.stderr b/tests/ui/asm/type-check-1.stderr index deba9e0fc544b..1852623211813 100644 --- a/tests/ui/asm/type-check-1.stderr +++ b/tests/ui/asm/type-check-1.stderr @@ -6,8 +6,8 @@ LL | asm!("{}", const x); | help: consider using `const` instead of `let` | -LL | const x = 0; - | ~~~~~ +LL | const x: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/type-check-1.rs:44:36 @@ -17,8 +17,8 @@ LL | asm!("{}", const const_foo(x)); | help: consider using `const` instead of `let` | -LL | const x = 0; - | ~~~~~ +LL | const x: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/type-check-1.rs:47:36 @@ -28,8 +28,8 @@ LL | asm!("{}", const const_bar(x)); | help: consider using `const` instead of `let` | -LL | const x = 0; - | ~~~~~ +LL | const x: /* Type */ = 0; + | ~~~~~ ++++++++++++ error: invalid `sym` operand --> $DIR/type-check-1.rs:49:24 diff --git a/tests/ui/asm/x86_64/x86_64_parse_error.stderr b/tests/ui/asm/x86_64/x86_64_parse_error.stderr index 95b86d533fa87..9751f7b09d0eb 100644 --- a/tests/ui/asm/x86_64/x86_64_parse_error.stderr +++ b/tests/ui/asm/x86_64/x86_64_parse_error.stderr @@ -20,8 +20,8 @@ LL | asm!("{a}", in("eax") foo, a = const bar); | help: consider using `const` instead of `let` | -LL | const bar = 0; - | ~~~~~ +LL | const bar: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/x86_64_parse_error.rs:15:46 @@ -31,8 +31,8 @@ LL | asm!("{a}", in("eax") foo, a = const bar); | help: consider using `const` instead of `let` | -LL | const bar = 0; - | ~~~~~ +LL | const bar: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/x86_64_parse_error.rs:17:42 @@ -42,8 +42,8 @@ LL | asm!("{1}", in("eax") foo, const bar); | help: consider using `const` instead of `let` | -LL | const bar = 0; - | ~~~~~ +LL | const bar: /* Type */ = 0; + | ~~~~~ ++++++++++++ error: aborting due to 5 previous errors diff --git a/tests/ui/const-generics/legacy-const-generics-bad.stderr b/tests/ui/const-generics/legacy-const-generics-bad.stderr index 6285d47807109..e9ea22e472c18 100644 --- a/tests/ui/const-generics/legacy-const-generics-bad.stderr +++ b/tests/ui/const-generics/legacy-const-generics-bad.stderr @@ -6,8 +6,8 @@ LL | legacy_const_generics::foo(0, a, 2); | help: consider using `const` instead of `let` | -LL | const a = 1; - | ~~~~~ +LL | const a: /* Type */ = 1; + | ~~~~~ ++++++++++++ error: generic parameters may not be used in const operations --> $DIR/legacy-const-generics-bad.rs:12:35 diff --git a/tests/ui/consts/non-const-value-in-const.stderr b/tests/ui/consts/non-const-value-in-const.stderr index ee9ac16fe42b8..654b573544c2e 100644 --- a/tests/ui/consts/non-const-value-in-const.stderr +++ b/tests/ui/consts/non-const-value-in-const.stderr @@ -17,8 +17,8 @@ LL | let _ = [0; x]; | help: consider using `const` instead of `let` | -LL | const x = 5; - | ~~~~~ +LL | const x: /* Type */ = 5; + | ~~~~~ ++++++++++++ error: aborting due to 2 previous errors diff --git a/tests/ui/issues/issue-27433.fixed b/tests/ui/issues/issue-27433.fixed index ff6704e393b5b..f847b698976a0 100644 --- a/tests/ui/issues/issue-27433.fixed +++ b/tests/ui/issues/issue-27433.fixed @@ -3,5 +3,5 @@ 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 + //~^ ERROR attempt to use a non-constant value in a constant } diff --git a/tests/ui/issues/issue-27433.rs b/tests/ui/issues/issue-27433.rs index 2a34b43f58d22..9bbc5bcbb459d 100644 --- a/tests/ui/issues/issue-27433.rs +++ b/tests/ui/issues/issue-27433.rs @@ -3,5 +3,5 @@ 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 + //~^ ERROR attempt to use a non-constant value in a constant } diff --git a/tests/ui/repeat-expr/repeat_count.stderr b/tests/ui/repeat-expr/repeat_count.stderr index 9b5afc557357a..350ac287507a3 100644 --- a/tests/ui/repeat-expr/repeat_count.stderr +++ b/tests/ui/repeat-expr/repeat_count.stderr @@ -6,8 +6,8 @@ LL | let a = [0; n]; | help: consider using `const` instead of `let` | -LL | const n = 1; - | ~~~~~ +LL | const n: /* Type */ = 1; + | ~~~~~ ++++++++++++ error[E0308]: mismatched types --> $DIR/repeat_count.rs:7:17 diff --git a/tests/ui/typeof/issue-42060.stderr b/tests/ui/typeof/issue-42060.stderr index 7af65633f71d9..86ba9432384b2 100644 --- a/tests/ui/typeof/issue-42060.stderr +++ b/tests/ui/typeof/issue-42060.stderr @@ -6,8 +6,8 @@ LL | let other: typeof(thing) = thing; | help: consider using `const` instead of `let` | -LL | const thing = (); - | ~~~~~ +LL | const thing: /* Type */ = (); + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/issue-42060.rs:9:13 @@ -17,8 +17,8 @@ LL | ::N | help: consider using `const` instead of `let` | -LL | const q = 1; - | ~~~~~ +LL | const q: /* Type */ = 1; + | ~~~~~ ++++++++++++ error[E0516]: `typeof` is a reserved keyword but unimplemented --> $DIR/issue-42060.rs:3:16 From 4df75140dde8776fb4e371a28474d899524d99e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 9 Jul 2024 17:27:08 +0000 Subject: [PATCH 3/3] Fix aarch64 test --- tests/ui/asm/aarch64/parse-error.stderr | 64 +++++++++++++++---------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/tests/ui/asm/aarch64/parse-error.stderr b/tests/ui/asm/aarch64/parse-error.stderr index 539c134472fda..d21a8ab1f85fd 100644 --- a/tests/ui/asm/aarch64/parse-error.stderr +++ b/tests/ui/asm/aarch64/parse-error.stderr @@ -313,74 +313,90 @@ LL | global_asm!("{1}", format!("{{{}}}", 0), const FOO, const BAR); error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:39:37 | -LL | let mut foo = 0; - | ----------- help: consider using `const` instead of `let`: `const foo` -... LL | asm!("{}", options(), const foo); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const foo: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:47:44 | -LL | let mut foo = 0; - | ----------- help: consider using `const` instead of `let`: `const foo` -... LL | asm!("{}", clobber_abi("C"), const foo); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const foo: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:50:55 | -LL | let mut foo = 0; - | ----------- help: consider using `const` instead of `let`: `const foo` -... LL | asm!("{}", options(), clobber_abi("C"), const foo); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const foo: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:52:31 | -LL | let mut foo = 0; - | ----------- help: consider using `const` instead of `let`: `const foo` -... LL | asm!("{a}", a = const foo, a = const bar); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const foo: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:52:46 | -LL | let mut bar = 0; - | ----------- help: consider using `const` instead of `let`: `const bar` -... LL | asm!("{a}", a = const foo, a = const bar); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const bar: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:59:45 | -LL | let mut bar = 0; - | ----------- help: consider using `const` instead of `let`: `const bar` -... LL | asm!("{a}", in("x0") foo, a = const bar); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const bar: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:61:45 | -LL | let mut bar = 0; - | ----------- help: consider using `const` instead of `let`: `const bar` -... LL | asm!("{a}", in("x0") foo, a = const bar); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const bar: /* Type */ = 0; + | ~~~~~ ++++++++++++ error[E0435]: attempt to use a non-constant value in a constant --> $DIR/parse-error.rs:63:41 | -LL | let mut bar = 0; - | ----------- help: consider using `const` instead of `let`: `const bar` -... LL | asm!("{1}", in("x0") foo, const bar); | ^^^ non-constant value + | +help: consider using `const` instead of `let` + | +LL | const bar: /* Type */ = 0; + | ~~~~~ ++++++++++++ error: aborting due to 57 previous errors