-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve diagnostic for const ctors in array repeat expressions #113925
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -27,6 +27,7 @@ use rustc_infer::infer::error_reporting::TypeErrCtxt; | |||||||
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; | ||||||||
use rustc_infer::infer::{DefineOpaqueTypes, InferOk, LateBoundRegionConversionTime}; | ||||||||
use rustc_middle::hir::map; | ||||||||
use rustc_middle::traits::IsConstable; | ||||||||
use rustc_middle::ty::error::TypeError::{self, Sorts}; | ||||||||
use rustc_middle::ty::{ | ||||||||
self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, | ||||||||
|
@@ -2832,20 +2833,30 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { | |||||||
)); | ||||||||
} | ||||||||
} | ||||||||
ObligationCauseCode::RepeatElementCopy { is_const_fn } => { | ||||||||
ObligationCauseCode::RepeatElementCopy { is_constable, elt_type, elt_span, elt_stmt_span } => { | ||||||||
err.note( | ||||||||
"the `Copy` trait is required because this value will be copied for each element of the array", | ||||||||
); | ||||||||
|
||||||||
if is_const_fn { | ||||||||
err.help( | ||||||||
"consider creating a new `const` item and initializing it with the result \ | ||||||||
of the function call to be used in the repeat position, like \ | ||||||||
`const VAL: Type = const_fn();` and `let x = [VAL; 42];`", | ||||||||
); | ||||||||
let value_kind = match is_constable { | ||||||||
IsConstable::Fn => Some("the result of the function call"), | ||||||||
IsConstable::Ctor => Some("the result of the constructor"), | ||||||||
_ => None | ||||||||
}; | ||||||||
let sm = tcx.sess.source_map(); | ||||||||
if let Some(value_kind) = value_kind && | ||||||||
let Ok(snip) = sm.span_to_snippet(elt_span) | ||||||||
{ | ||||||||
let help_msg = format!( | ||||||||
"consider creating a new `const` item and initializing it with {value_kind} \ | ||||||||
to be used in the repeat position"); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
let indentation = sm.indentation_before(elt_stmt_span).unwrap_or_default(); | ||||||||
err.multipart_suggestion(help_msg, vec![ | ||||||||
(elt_stmt_span.shrink_to_lo(), format!("const ARRAY_REPEAT_VALUE: {elt_type} = {snip};\n{indentation}")), | ||||||||
(elt_span, "ARRAY_REPEAT_VALUE".to_string()) | ||||||||
], Applicability::MachineApplicable); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made this MachineApplicable because |
||||||||
} | ||||||||
|
||||||||
if self.tcx.sess.is_nightly_build() && is_const_fn { | ||||||||
if self.tcx.sess.is_nightly_build() && matches!(is_constable, IsConstable::Fn|IsConstable::Ctor) { | ||||||||
err.help( | ||||||||
"create an inline `const` block, see RFC #2920 \ | ||||||||
<https://github.com/rust-lang/rfcs/pull/2920> for more information", | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
static _MAYBE_STRINGS: [Option<String>; 5] = [None; 5]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we annotate this file with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah unfortunately no, because the const uses the same name which conflicts. It might be a good idea to maybe make the identifier more unique (do we have a way of 'identifier-izing' an arbritrary string?). Something like |
||
//~^ ERROR the trait bound `String: Copy` is not satisfied | ||
|
||
fn main() { | ||
// should hint to create an inline `const` block | ||
// or to create a new `const` item | ||
let strings: [String; 5] = [String::new(); 5]; | ||
let _strings: [String; 5] = [String::new(); 5]; | ||
//~^ ERROR the trait bound `String: Copy` is not satisfied | ||
let _maybe_strings: [Option<String>; 5] = [None; 5]; | ||
//~^ ERROR the trait bound `String: Copy` is not satisfied | ||
println!("{:?}", strings); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,47 @@ | ||
error[E0277]: the trait bound `String: Copy` is not satisfied | ||
--> $DIR/const-fn-in-vec.rs:4:33 | ||
--> $DIR/const-fn-in-vec.rs:1:47 | ||
| | ||
LL | let strings: [String; 5] = [String::new(); 5]; | ||
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String` | ||
LL | static _MAYBE_STRINGS: [Option<String>; 5] = [None; 5]; | ||
| ^^^^ the trait `Copy` is not implemented for `String` | ||
| | ||
= note: required for `Option<String>` to implement `Copy` | ||
= note: the `Copy` trait is required because this value will be copied for each element of the array | ||
= help: consider creating a new `const` item and initializing it with the result of the function call to be used in the repeat position, like `const VAL: Type = const_fn();` and `let x = [VAL; 42];` | ||
= help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information | ||
help: consider creating a new `const` item and initializing it with the result of the constructor to be used in the repeat position | ||
| | ||
LL + const ARRAY_REPEAT_VALUE: Option<String> = None; | ||
LL ~ static _MAYBE_STRINGS: [Option<String>; 5] = [ARRAY_REPEAT_VALUE; 5]; | ||
| | ||
|
||
error[E0277]: the trait bound `String: Copy` is not satisfied | ||
--> $DIR/const-fn-in-vec.rs:7:34 | ||
| | ||
LL | let _strings: [String; 5] = [String::new(); 5]; | ||
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String` | ||
| | ||
= note: the `Copy` trait is required because this value will be copied for each element of the array | ||
= help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information | ||
help: consider creating a new `const` item and initializing it with the result of the function call to be used in the repeat position | ||
| | ||
LL ~ const ARRAY_REPEAT_VALUE: String = String::new(); | ||
LL ~ let _strings: [String; 5] = [ARRAY_REPEAT_VALUE; 5]; | ||
| | ||
|
||
error[E0277]: the trait bound `String: Copy` is not satisfied | ||
--> $DIR/const-fn-in-vec.rs:9:48 | ||
| | ||
LL | let _maybe_strings: [Option<String>; 5] = [None; 5]; | ||
| ^^^^ the trait `Copy` is not implemented for `String` | ||
| | ||
= note: required for `Option<String>` to implement `Copy` | ||
= note: the `Copy` trait is required because this value will be copied for each element of the array | ||
= help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information | ||
help: consider creating a new `const` item and initializing it with the result of the constructor to be used in the repeat position | ||
| | ||
LL ~ const ARRAY_REPEAT_VALUE: Option<String> = None; | ||
LL ~ let _maybe_strings: [Option<String>; 5] = [ARRAY_REPEAT_VALUE; 5]; | ||
| | ||
|
||
error: aborting due to previous error | ||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👮