-
Notifications
You must be signed in to change notification settings - Fork 13k
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
resolve: Use same rules for disambiguating fresh bindings in match
and let
#45050
Conversation
//~^ NOTE cannot be named the same as a constant | ||
let d = 4; //~ ERROR let bindings cannot shadow constants | ||
//~^ NOTE cannot be named the same as a constant | ||
let a = 4; //~ ERROR refutable pattern in local binding: `_` not covered |
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.
Can we provide some better hint as to why this error is occurring? I could see this being rather perplexing.
BracedStruct => {} // OK, `BracedStruct` is a fresh binding | ||
} | ||
match UnitVariant { | ||
UnitVariant => {} // OK, `UnitVariant` is a unit variant pattern |
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.
Why isn't this a non-exhaustive error? Is UnitVariant
a new match binding shadowing E::UnitVariant
?
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.
Exhaustiveness is checked in a separate late pass that isn't reached here because type checking completes with errors.
I think this is a good PR but -- like @cramertj -- I too am mildly concerned about diagnostics. That said, the naming conventions here probably make this a largely moot point (at the time when we adopted this hard error, we had not yet adopted the upper-case naming pattern for variants, if memory serves). Errors like |
Yeah, I'll add a note/help for #45050 (comment) once the change is decided to be desirable in general. |
@petrochenkov ok, I'm in favor other than the diagnostics issue. Do you think we ought to do an FCP? |
I suppose we should. |
@rfcbot fcp merge This is a change to our handling of constants that appear in irrefutable patterns. In particular, at present, it is an error to use a variant name in an irrefutable pattern, which means that people who try to do newtype'd variants on struct TypeError;
fn foo() -> Result<(), TypeError> { .. }
fn bar() {
let TypeError = foo().unwrap_err(); // artificial but wev
} This is obviously inconsistent with refutable arms. The origin of this rule goes way back to the point where we first decided to use the name resolution context to decide how to interpret a bare identifier like Now that variants are traditionally named with an upper-case, this rule is less important, and the confusion tends to arise in the opposite way: people try to use unit structs in an irrefutable pattern, and get an error. Furthermore, as our diagnostics have improved, we've now started to tilt towards handling these sorts of confusing cases with improved diagnostics instead of hard errors. @petrochenkov has promised to put such a message in (the current PR does not include any special error message hangling). I think we should remove this special-case rule and make all patterns behave the same. |
Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, 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 reviewed |
Ping @aturon @eddyb @nrc @pnkfelix @withoutboats for the ticky boxes! |
Ping @withoutboats again for the ticky boxes! |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
Updated with better diagnostics for #45050 (comment) |
--> $DIR/const-pattern-irrefutable.rs:24:9 | ||
| | ||
24 | let d = 4; //~ ERROR refutable pattern in local binding: `_` not covered | ||
| ^ interpreted as a constant pattern, not new variable |
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.
👍
@bors r+ Going to r+. I don't anticipate any commentary from FCP, but we can always back out -- on the off chance bors gets around to landing this anytime in the next few days. ;) |
📌 Commit 765076f has been approved by |
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit 765076f has been approved by |
resolve: Use same rules for disambiguating fresh bindings in `match` and `let` Resolve `Unit` as a unit struct pattern in ```rust struct Unit; let Unit = x; ``` consistently with ```rust match x { Unit => {} } ``` It was previously an error. (The change also applies to unit variants and constants.) Fixes https://users.rust-lang.org/t/e0530-cannot-shadow-unit-structs-what-in-the-earthly-what/13054 (This particular change doesn't depend on a fix for the issue mentioned in https://users.rust-lang.org/t/e0530-cannot-shadow-unit-structs-what-in-the-earthly-what/13054/4) cc @rust-lang/lang r? @nikomatsakis
☀️ Test successful - status-appveyor, status-travis |
Resolve
Unit
as a unit struct pattern inconsistently with
It was previously an error.
(The change also applies to unit variants and constants.)
Fixes https://users.rust-lang.org/t/e0530-cannot-shadow-unit-structs-what-in-the-earthly-what/13054
(This particular change doesn't depend on a fix for the issue mentioned in https://users.rust-lang.org/t/e0530-cannot-shadow-unit-structs-what-in-the-earthly-what/13054/4)
cc @rust-lang/lang
r? @nikomatsakis