Skip to content
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

Merged
merged 1 commit into from Nov 8, 2023

Conversation

clubby789
Copy link
Contributor

Fixes #113912

@rustbot
Copy link
Collaborator

rustbot commented Jul 21, 2023

r? @eholk

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 21, 2023
/// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👮

@clubby789
Copy link
Contributor Author

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

Copy link
Contributor

@estebank estebank left a 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.

@clubby789
Copy link
Contributor Author

image
While working on this, it looks like we get weird highlighting when the span we're suggesting gets moved to a newline. Putting the const after works okay but results in weirder looking code
image

@estebank
Copy link
Contributor

estebank commented Jul 24, 2023

The highlighting is likely a bug in the suggestion calculation. What you could do instead is use span_extend_to_line (and check that there's no code before it (like if we had the span for bar() in foo(); bar();) and insert the whole line to side step the issue (but ideally we should fix the underlying problem, fixing the coloring, at some point).

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 \t in this part of the snippet machinery.

@clubby789
Copy link
Contributor Author

Ah, looks like it's a bug with indentation. Fixing the indentation makes the highlighting work properly

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);
Copy link
Contributor Author

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

@rust-log-analyzer

This comment has been minimized.

@@ -1,7 +1,11 @@
static _MAYBE_STRINGS: [Option<String>; 5] = [None; 5];
Copy link
Contributor

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.)

Copy link
Contributor Author

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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
to be used in the repeat position");
to be used in the repeat position"
);

Copy link
Contributor

@estebank estebank left a 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.

@estebank
Copy link
Contributor

r? @estebank

@bors r+

@bors
Copy link
Contributor

bors commented Jul 25, 2023

📌 Commit 86b1122 has been approved by estebank

It is now in the queue for this repository.

@rustbot rustbot assigned estebank and unassigned eholk Jul 25, 2023
@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 25, 2023
@clubby789
Copy link
Contributor Author

I'd like to address the nitpicks, having a rustfix example seems like a good idea
@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 25, 2023
@Dylan-DPC
Copy link
Member

@clubby789 any updates on this?

@estebank
Copy link
Contributor

estebank commented Nov 7, 2023

@Dylan-DPC lets see if we can land as is and @clubby789 can open a follow up PR at their leisure.

@bors r+

@bors
Copy link
Contributor

bors commented Nov 7, 2023

📌 Commit 86b1122 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 7, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 8, 2023
…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
@bors bors merged commit b724d9c into rust-lang:master Nov 8, 2023
11 checks passed
@rustbot rustbot added this to the 1.75.0 milestone Nov 8, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 8, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve help for const-able values used in array initialization
8 participants