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
Conversation
r? @eholk (rustbot has picked a reviewer for you, use r? to override) |
7127f5c
to
22ce932
Compare
/// Whether a value can be extracted into a const. | ||
/// Used for diagnostics around array repeat expressions. | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)] | ||
pub enum IsConstable { |
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.
👮
compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
22ce932
to
8dccd6a
Compare
Updated to be a structured suggestion, but I'm not sure about the placement of it (the line before the span of the expression being repeated), since the line before could be part of some other expression. It probably doesn't matter oto much since this is a HasPlaceholders suggestion |
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.
Oh wow! Thank you for attempting to turn this into a structured suggestion in this same PR, was expecting it to be addressed as follow up work :)
I think we can make this very robust, by changing RepeatElementCopy
to carry more info.
compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
The highlighting is likely a bug in the suggestion calculation. What you could do instead is use In any case, try to get the ascii output itself to look ok and rustfix suggestions to apply neatly, and we can deal with the coloring separately. Edit: could it be that there's a tab or some other whitespace hidden in there? I could imagine inconsistent handling of |
Ah, looks like it's a bug with indentation. Fixing the indentation makes the highlighting work properly |
8dccd6a
to
7857b27
Compare
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Made this MachineApplicable because ARRAY_REPEAT_VALUE
is unlikely to conflict unless there's multiple instances of this in the same scope, and I'm reasonably sure the HIR walking should give us a span it's safe to put a new item on. This can be changed to MaybeIncorrect if there's any weird counterexamples
This comment has been minimized.
This comment has been minimized.
7857b27
to
86b1122
Compare
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
If we annotate this file with // run-rustfix
, does the test continue to pass? If so, let's add that to protect against involuntary regressions in the suggestions. (You might have to allow
some lints, potentially.)
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.
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 Enum::Variant
to ENUM_VARIANT
might help
{ | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
to be used in the repeat position"); | |
to be used in the repeat position" | |
); |
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.
r=me after addressing the nitpicks.
I'd like to address the nitpicks, having a rustfix example seems like a good idea |
@clubby789 any updates on this? |
@Dylan-DPC lets see if we can land as is and @clubby789 can open a follow up PR at their leisure. @bors r+ |
…iaskrgr Rollup of 4 pull requests Successful merges: - rust-lang#113925 (Improve diagnostic for const ctors in array repeat expressions) - rust-lang#116399 (Small changes w/ `query::Erase<_>`) - rust-lang#117625 (Fix some clippy perf lints) - rust-lang#117655 (Method suggestion code tweaks) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#113925 - clubby789:const-ctor-repeat, r=estebank Improve diagnostic for const ctors in array repeat expressions Fixes rust-lang#113912
Fixes #113912