Implement unused pattern bindings, continued#6518
Implement unused pattern bindings, continued#6518jonmeow merged 52 commits intocarbon-language:trunkfrom
Conversation
Implements the following diagnostics in check: * [UnusedBinding] warning, a binding is unused. * [UnusedPatternNoBindings] warning, "unused" marker has no effect * [UnusedMarkerInDeclaration] error, "unused" is for definitions * [UnusedButUsed] error, marker is contradicting use * [UnusedButUsedHere] note, shows first use Updates all test cases which contain unused markers, and replaces TODOs with actual warnings (or evidence of absence of warnings). Unfortunately, the prevalence of unused bindings in test cases makes the change rather large.
jonmeow
left a comment
There was a problem hiding this comment.
Thanks for the contribution! I've done a first pass.
In particular regarding my comments on CARBON_KIND_SWITCH and things like AnyBindingPattern, note that comes up multiple times in this PR. We use those a lot to reduce the weight of these sorts of repeat inst kind checks, hopefully they're helpful here!
burakemir
left a comment
There was a problem hiding this comment.
Thanks for the suggestions! PTAL.
jonmeow
left a comment
There was a problem hiding this comment.
Stepping back a moment, I was thinking again about the responses on #6448. I went back to @zygoloid because of this comment, trying to figure out when he meant that changing a name to _ should diagnose. After discussion, here are a couple specific things:
fn F(x: i32) {
return;
// When a non-`unused` binding is used in unreachable code,
// that should not diagnose because changing it to `_` would be a name lookup diagnostic here.
x = 0;
}
fn F(unused x: i32) {
return;
// When an explicitly `unused` name is used in unreachable code,
// both warning and not warning are okay choices.
x = 0;
}
I was looking through tests, trying to figure out if cases like this are covered (it looks like tests may not be covering unreachable dataflow?). In particular I'm noting this, because I think it may have interesting implications on dataflow; is the current setup giving desired answer for references to x: i32 (no unused)?
For return flow, zygoloid reminded me we have IsCurrentPositionReachable. We use this to take advantage of some flow data that check accumulates (rather than as a separate dataflow step, as proposed here). Had you looked at using IsCurrentPositionReachable for the unused-but-used case?
|
Also, quick small note in addition to the above, I know it's a big question but I'm also going to be out soon for a while; I'll probably be handing off review tomorrow since even though people are out a lot for the holidays, I'm probably coming back later than others. :) |
jonmeow
left a comment
There was a problem hiding this comment.
Thanks! I think this is close to being ready to submit. Can you please update it against trunk, when you think it's ready for a merge? That way we can handle actually doing the merge.
burakemir
left a comment
There was a problem hiding this comment.
Thanks for the review, Will rebase now.
|
Added a merge commit. This should be good to land. Note that I don't have rights to merge. |
|
It looks like you still have conflicts, with older PRs (e.g. toolchain/check/BUILD last updated a week ago). Would you be interested in fixing the outstanding conflicts, or do you want me to finish the merge work? To be sure, you should probably be doing something like |
…ngs_impl_continued
|
Done the proper merge commit from trunk now. |
36ce922 to
00d166d
Compare
There was a problem hiding this comment.
It looks like a PR got in. I was getting to merge that (you might note me having pushed to this PR, I undid it), but was double-checking tests were passing (bazel test //...) before that. It looks like you have a couple failing tests already, so I backed out my merge -- do you want to fix tests and merge again?
| // CHECK:STDERR: generic_default_fn.carbon:[[@LINE+4]]:6: warning: binding `T` unused [UnusedBinding] | ||
| // CHECK:STDERR: fn I(T:! type).F[ref self: Self]() -> Self* { return &self; } | ||
| // CHECK:STDERR: ^ | ||
| // CHECK:STDERR: |
There was a problem hiding this comment.
This test is failing. Maybe this could be _:! type?
There was a problem hiding this comment.
We cannot use _ here, since proposal #3763 explicitly disallows this and considers it a difference in redeclaration.
However, if we were to try "fn I(unused T:! type).F ..." we would find that the toolchain's generic-handling code does not handle unused correctly in either construction or comparison of eval blocks (or both). This is explicitly left as TODO, and I would prefer to not expand the scope of this PR further.
There was a problem hiding this comment.
To be very clear: the test is not failing for me, with the warning being present in the expected output. I propose to just leave that warning in.
| // CHECK:STDERR: nested_require_incomplete_interface.carbon:[[@LINE+4]]:6: warning: binding `T` unused [UnusedBinding] | ||
| // CHECK:STDERR: fn F(T:! X where .X1 = {}) {} | ||
| // CHECK:STDERR: ^ | ||
| // CHECK:STDERR: |
There was a problem hiding this comment.
Can you add an unused marker here?
…ngs_impl_continued Add "unused" to new tests.
|
Thanks for taking a look. It is not the case that I had a couple failing tests (wouldn't presubmit have caught this?), it is rather that everytime a test case gets added anywhere, it likely contains unused variables which are now diagnosed. This is one reason why I had to catch up the 150 commits with a strategy of rebase-in-stages before doing the merge. |
|
Thanks for all your work here, I'll finish and resolve any remaining merge issues.
Unfortunately conflicts prevent GitHub actions from running; the local pre-commit tooling doesn't run tests because we value it being fast. |
Implementation of unused pattern bindings #2022, continued.
Whereas previous PR #6460 took care of parsing, and PR #6479 prepared the stage by using _ in some test cases, this PR has the the actual implementation, using a simple dataflow analysis.