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

Suggest array indexing when tuple indexing on an array #54292

Merged
merged 3 commits into from
Sep 21, 2018

Conversation

memoryruins
Copy link
Contributor

Closes #53712

r? @varkor
cc @estebank

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 17, 2018
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.

I'm really happy to see this PR!

I left a couple of comments inline.

let base = self.tcx.hir.node_to_pretty_string(base.id);
let msg = format!("attempting to use tuple indexing on an array; try");
let suggestion = format!("{}[{}]", base, field);
err.span_suggestion(field.span, &msg, suggestion);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use span_suggestion_with_applicability and Applicability::MaybeIncorrect.

@@ -3344,6 +3344,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
};
}
ty::Array(ty, _) if ty.is_numeric() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if you can get the numeric value of field and check wether it is smaller than ty::Array(_, len). That would give us a great degree of confidence that the suggestion is correct, we could even switch between Applicability levels given that... 🤔

@@ -3344,6 +3344,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
};
}
ty::Array(ty, _) if ty.is_numeric() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason to limit this suggestion to only [{integer}; _] and [{float}; _]? I'm not against it, as being conservative in suggestions is not bad, I just want to hear what was your reasoning.

@@ -3344,6 +3344,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
};
}
ty::Array(ty, _) if ty.is_numeric() => {
let base = self.tcx.hir.node_to_pretty_string(base.id);
let msg = format!("attempting to use tuple indexing on an array; try");
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's change the wording to something along the lines of instead of using tuple indexing use array indexing.

@memoryruins
Copy link
Contributor Author

memoryruins commented Sep 17, 2018

@estebank Limiting to only numeric types was definitely overly conservative, and I should’ve been checking if the field was numeric instead :)

Good thinking on the array length and applicability levels. I’ll try it out!

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.

Other than one last fix needed, LGTM.

Applicability::MaybeIncorrect
};
err.span_suggestion_with_applicability(
field.span, help, suggestion, applicability
Copy link
Contributor

Choose a reason for hiding this comment

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

You probably want to use expr.span here instead of field.span.

@@ -2,7 +2,7 @@ error[E0609]: no field `0` on type `[{integer}; 5]`
--> $DIR/issue-53712.rs:5:9
|
LL | arr.0;
| ^ help: attempting to use tuple indexing on an array; try: `arr[0]`
| ^ help: instead of using tuple indexing, use array indexing: `arr[0]`
Copy link
Contributor

Choose a reason for hiding this comment

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

I just realized that the suggestion span is slightly incorrect. The suggestion span should cover the entire expression, otherwise when applying the suggestion with rustfix, for example, you'd end up with the following code

    arr.arr[0];

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, right you are!

@estebank
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Sep 18, 2018

📌 Commit 73fdc81 has been approved by estebank

@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 Sep 18, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Sep 20, 2018
Suggest array indexing when tuple indexing on an array

Closes rust-lang#53712

r? @varkor
cc @estebank
bors added a commit that referenced this pull request Sep 20, 2018
Rollup of 15 pull requests

Successful merges:

 - #52813 (Duration div mul extras)
 - #53470 (Warn about metadata loader errors)
 - #54233 (Remove LLVM 3.9 workaround.)
 - #54257 (Switch wasm math symbols to their original names)
 - #54258 (Enable fatal warnings for the wasm32 linker)
 - #54266 (Update LLVM to fix "bool" arguments on PPC32)
 - #54290 (Switch linker for aarch64-pc-windows-msvc from LLD to MSVC)
 - #54292 (Suggest array indexing when tuple indexing on an array)
 - #54295 (A few cleanups and minor improvements to rustc/traits)
 - #54298 (miri: correctly compute expected alignment for field)
 - #54333 (Update The Book to latest)
 - #54337 (Remove unneeded clone() from tests in librustdoc)
 - #54346 (rustc: future-proof error reporting for polymorphic constants in types.)
 - #54362 (Pass --batch to gdb)
 - #54367 (Add regression test for thread local static mut borrows)
@bors bors merged commit 73fdc81 into rust-lang:master Sep 21, 2018
@memoryruins memoryruins deleted the issue-53712 branch September 22, 2018 19:05
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants