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

Add filtering option to rustc_on_unimplemented and reword Iterator E0277 errors #54946

Merged
merged 8 commits into from
Oct 17, 2018

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Oct 10, 2018

 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (rust-lang#23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (rust-lang#36391)
 - Suggest using range literal when iterating on integers (rust-lang#34353)
 - Do not suggest `.iter()` by default (rust-lang#50773, rust-lang#46806)
@rust-highfive
Copy link
Collaborator

r? @varkor

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

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 10, 2018
@estebank
Copy link
Contributor Author

CC @csmoe I think this covers all that #52178 did

@rust-highfive

This comment has been minimized.

Copy link
Member

@varkor varkor left a comment

Choose a reason for hiding this comment

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

These diagnostic changes are a big improvement!

It'd be nice if we could get rid of the false positives (e.g. the "if you want to iterate between 0 until a value end" message), but it's hard to tell how often they'd come up in practice, so if that's awkward to support, I'd probably be okay with leaving it how it is: I think it probably helps more than it confuses.

src/libcore/iter/iterator.rs Outdated Show resolved Hide resolved
src/librustc/traits/error_reporting.rs Outdated Show resolved Hide resolved
|
= help: the trait `std::iter::Iterator` is not implemented for `i32`
= note: if you want to iterate between `0` until a value `end`, use the range syntax: `0..end`
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem perfect: it's specific to loops, but I imagine people writing <{integer} as Iterator> is so uncommon that this isn't really a problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was my thinking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would also be fixed by marking all the spans part of a for loop as being part of a desugaring context, which we can filter on in rustc_on_unimplemented and would improve the situation for many of this diagnostics that only apply when using a binding and not in type signatures.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that would be good!

--> $DIR/array-of-ranges.rs:7:14
|
LL | for _ in array_of_range {}
| ^^^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets
Copy link
Member

Choose a reason for hiding this comment

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

This isn't ideal. Any way we could only make this happen for the literal syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We would have to change rustc_on_unimplemented. I've tried to "poison" the span for the iterator with a "desugaring context", but was unable to get it working. It should be possible to add it, but given that the likelihood of this case in particular happening is so slim (you have to 1. create an array of ranges, already uncommon and 2. try to iterate over the array), I felt confident leaving it as is (at least for now).

Copy link
Member

Choose a reason for hiding this comment

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

That's true; it could always be a follow-up.

src/libcore/iter/iterator.rs Outdated Show resolved Hide resolved
src/libcore/iter/iterator.rs Show resolved Hide resolved
- reword messages
- apply custom comments to all types of ranges
- fix indentation
),
on(
_Self="[std::ops::RangeTo<Idx>; 1]",
label="if you meant to iterate until a value, remove the square brackets",
Copy link
Member

Choose a reason for hiding this comment

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

You can't iterate over a RangeTo, so I'm not sure this one is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're absolutely right, I just never had thought about it enough to look it up. I will still add a note to it in order to let users know that.

),
on(
_Self="[std::ops::RangeToInclusive<Idx>; 1]",
label="if you meant to iterate until a value, remove the square brackets",
Copy link
Member

Choose a reason for hiding this comment

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

Same with RangeToInclusive.

@varkor
Copy link
Member

varkor commented Oct 11, 2018

r=me after the last couple of comments, if Travis passes.

@estebank
Copy link
Contributor Author

@bors r=varkor

@bors
Copy link
Contributor

bors commented Oct 11, 2018

📌 Commit 5beeb53 has been approved by varkor

@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 Oct 11, 2018
@bors
Copy link
Contributor

bors commented Oct 13, 2018

⌛ Testing commit 5beeb53 with merge 73008786a90f5da800834e1540ca6521d8b7c7ba...

@bors
Copy link
Contributor

bors commented Oct 13, 2018

💔 Test failed - status-travis

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 13, 2018
@rust-highfive

This comment has been minimized.

@kennytm kennytm 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-review Status: Awaiting review from the assignee but also interested parties. labels Oct 13, 2018
@estebank
Copy link
Contributor Author

@bors try

bors added a commit that referenced this pull request Oct 15, 2018
Add filtering option to `rustc_on_unimplemented` and reword `Iterator` E0277 errors

 - Add more targetting filters for arrays to `rustc_on_unimplemented` (Fix #53766)
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (Fix #23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (Fix #36391)
 - Suggest using range literal when iterating on integers (Fix #34353)
 - Do not suggest `.iter()` by default (Fix #50773, fix #46806)
 - Add regression test (Fix #22872)
@bors
Copy link
Contributor

bors commented Oct 15, 2018

☀️ Test successful - status-travis
State: approved= try=True

@estebank
Copy link
Contributor Author

@bors r=varkor

@bors
Copy link
Contributor

bors commented Oct 15, 2018

📌 Commit f5cc6b5 has been approved by varkor

@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 Oct 15, 2018
@bors
Copy link
Contributor

bors commented Oct 16, 2018

⌛ Testing commit f5cc6b5 with merge d502144fd36634a938d73bf1893580d420194443...

@bors

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 16, 2018
@kennytm kennytm 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-review Status: Awaiting review from the assignee but also interested parties. labels Oct 16, 2018
Some(format!("[{}]", self.tcx.type_of(def.did).to_string())),
));
if let Some(len) = len.val.try_to_scalar().and_then(|scalar| {
scalar.to_i64().ok()
Copy link
Contributor Author

@estebank estebank Oct 16, 2018

Choose a reason for hiding this comment

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

Hi @oli-obk!

Could you take a look at how I'm using Scalar::to_i64 here? In travis I'm getting the following test error, and looking at the interface of Scalar I don't see a safe way of figuring wether the value is 1 without tripping the assert on some platforms. I need to check wether len.val is 1 (at the very least) or the actual value (more flexibility, but not needed now).

[00:46:27] thread 'main' panicked at 'assertion failed: `(left == right)`
[00:46:27]   left: `8`,
[00:46:27]  right: `4`', librustc/mir/interpret/value.rs:242:17
[00:46:27] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[00:46:27] 
[00:46:27] error: internal compiler error: unexpected panic

Copy link
Contributor

Choose a reason for hiding this comment

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

You can use scalar.to_usize(self.tcx).ok() which actually takes the target platform's usize size and should thus work on all platforms

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I didn't realize that TyCtxt implemented HasDataLayout.

@estebank

This comment has been minimized.

@estebank
Copy link
Contributor Author

@bors r=varkor

@bors
Copy link
Contributor

bors commented Oct 16, 2018

📌 Commit def0f54 has been approved by varkor

@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 Oct 16, 2018
@bors
Copy link
Contributor

bors commented Oct 17, 2018

⌛ Testing commit def0f54 with merge 1dceadd...

bors added a commit that referenced this pull request Oct 17, 2018
Add filtering option to `rustc_on_unimplemented` and reword `Iterator` E0277 errors

 - Add more targetting filters for arrays to `rustc_on_unimplemented` (Fix #53766)
 - Detect one element array of `Range` type, which is potentially a typo:
   `for _ in [0..10] {}` where iterating between `0` and `10` was intended.
   (Fix #23141)
 - Suggest `.bytes()` and `.chars()` for `String`.
 - Suggest borrowing or `.iter()` on arrays (Fix #36391)
 - Suggest using range literal when iterating on integers (Fix #34353)
 - Do not suggest `.iter()` by default (Fix #50773, fix #46806)
 - Add regression test (Fix #22872)
@bors
Copy link
Contributor

bors commented Oct 17, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: varkor
Pushing 1dceadd to master...

@bors bors merged commit def0f54 into rust-lang:master Oct 17, 2018
@clarfonthey
Copy link
Contributor

Late but... I think this just replaced several clippy lints with just one diagnostic change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment