-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Perform type inference in range pattern #88090
Conversation
r? @jackh726 (rust-highfive has picked a reviewer for you, use r? to override) |
This is technically a behavior change (even though I agree it's more of a bug fix), so I'm just going to cc @rust-lang/compiler (If anyone else knows this code better and wants to take the review, feel free) |
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.
The changes look reasonable after a cursory review. @rust-lang/lang should chime in and approve the behavioral change (unless this was intentional). With that I can take a closer look at the code to approve or as for more changes.
} | ||
|
||
impl Zero for String { | ||
const ZERO: Self = String::new(); |
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.
When associated consts are involved in this error, we should likely point at them.
| | this is of type `_` but it should be `char` or numeric | ||
| this is of type `_` but it should be `char` or numeric |
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.
Somewhere we are not actually using resolve_ty_if_possible
. We should be mentioning that these are of type String
.
if let Some((ref mut fail, _, _)) = rhs { | ||
*fail = true; | ||
} | ||
self.emit_err_pat_range(span, lhs, rhs); |
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.
I think this is why. We pass in lhs
and rhs
. Inside of emit_err_pat_range
we don't resolve_vars_if_possible
on lhs.1
or rhs.1
. I think that doing that should make the labels talk about String
instead of _
.
I'm going to go ahead and nominate this for the lang team: Do we want to allow inference in range patterns (as in https://github.com/rust-lang/rust/blob/353c03955dfe0831e4b58f9e05e99447c06adff2/src/test/ui/pattern/issue-88074-pat-range-type-inference.rs)? The |
To summarize the effects of this PR, I believe it implements the following logic:
whereas before the logic was
This change in ordering makes all the difference for the inferencer, of course, since it is able to propagate types from one side to the other. Does this sound correct? |
That looks correct to me. @nbdd0121 could you actually add that explanation as a comment? That makes it abundantly clear what's going on. |
A more accurate description:
whereas before the logic was
Note 1: the early check if LHS or RHS types are known is needed because range pattern will strip away references. So if the early check is omitted and we just start unifying types, then match "A" {
"A".."B" => (),
_ => (),
} will have LHS and RHS be |
BTW one more thing: with this PR, match Zero::ZERO {
Zero::ZERO ..= Zero::ZERO => {},
_ => {},
} will still complain that |
Interesting...can you make sure there is a test that covers this case?
What happens with non-range patterns? |
This is from a existing test case ^^
Non-range patterns do not need to inspect the type, so match Zero::ZERO {
Zero::ZERO => {},
_ => {},
} will just behave like let _ = Zero::ZERO; and gives a "type annotations needed" error. |
I would say if the change to emit "type annotations needed" is small, that's preferable, so behavior lines up. If not, then I think it can be left as a followup FIXME |
v1 -> v2:
|
@rfcbot fcp merge We discussed this in the @rust-lang/lang meeting today. General consensus was that this change was quite reasonable; in order to change the assumption that L/R hand sides of |
Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged team members:
No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@rfcbot fcp cancel |
@rfcbot fcp merge We discussed this in the @rust-lang/lang meeting today. General consensus was that this change was quite reasonable; in order to change the assumption that L/R hand sides of |
Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
@bors r+ |
📌 Commit 52a0403 has been approved by |
Perform type inference in range pattern Fix rust-lang#88074
Perform type inference in range pattern Fix rust-lang#88074
Perform type inference in range pattern Fix rust-lang#88074
…ingjubilee Rollup of 15 pull requests Successful merges: - rust-lang#87993 (Stabilize try_reserve) - rust-lang#88090 (Perform type inference in range pattern) - rust-lang#88780 (Added abs_diff for integer types.) - rust-lang#89270 (path.push() should work as expected on windows verbatim paths) - rust-lang#89413 (Correctly handle supertraits for min_specialization) - rust-lang#89456 (Update to the final LLVM 13.0.0 release) - rust-lang#89466 (Fix bug with query modifier parsing) - rust-lang#89473 (Fix extra `non_snake_case` warning for shorthand field bindings) - rust-lang#89474 (rustdoc: Improve doctest pass's name and module's name) - rust-lang#89478 (Fixed numerus of error message) - rust-lang#89480 (Add test for issue 89118.) - rust-lang#89487 (Try to recover from a `=>` -> `=` or `->` typo in a match arm) - rust-lang#89494 (Deny `where` clauses on `auto` traits) - rust-lang#89511 (:arrow_up: rust-analyzer) - rust-lang#89536 (update Miri) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Fix #88074