-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Further improve diagnostics for expressions in pattern position #123877
Conversation
This comment has been minimized.
This comment has been minimized.
Currently vacationing for two more days, then I'm gonna review this PR. |
Squashed the commits, I still have the |
☔ The latest upstream changes (presumably #126678) made this pull request unmergeable. Please resolve the merge conflicts. |
This comment has been minimized.
This comment has been minimized.
1361d1a
to
27b59b3
Compare
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've finished reviewing 3 out of 4 commits. Looking good so far. Gonna look at the 4th one in a sec. Sorry for taking so long ^^'
else { | ||
// We got a trailing method/operator, but that wasn't an expression. | ||
return None; | ||
}; |
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.
Nice let-else!
error: expected a pattern, found an expression | ||
--> $DIR/recover-pat-exprs.rs:58:9 | ||
| | ||
LL | x.sqrt() @ .. => (), | ||
| ^^^^^^^^ arbitrary expressions are not allowed in patterns |
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.
Btw, if you'd like to suppress this one, we could pass a param RecoverExprInPat::{Yes, No}
to parse_pat…
error: expected a pattern, found an expression | ||
--> $DIR/recover-pat-exprs.rs:43:10 | ||
| | ||
LL | (1 + 2) * 3 => (), | ||
| ^^^^^ arbitrary expressions are not allowed in patterns | ||
|
||
error: expected a pattern, found an expression | ||
--> $DIR/recover-pat-exprs.rs:43:9 | ||
| | ||
LL | (1 + 2) * 3 => (), | ||
| ^^^^^^^^^^^ arbitrary expressions are not allowed in patterns |
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.
Note to self: Ah, right. If we were to reparse the tokens as an expr when failing to parse a pat (starting right before the first (
), then we would only emit a single error for (1 + 2) * 3
in pat position but we don't follow this approach since that would require creating a snapshot upfront potentially in the happy path very likely incurring a perf cost (it's been a while, trying to remember all the context).
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.
Very nicely structured test file 👏
☔ The latest upstream changes (presumably #127777) made this pull request unmergeable. Please resolve the merge conflicts. |
This comment has been minimized.
This comment has been minimized.
e6cfede
to
47413ca
Compare
I was on vacation and I wanted to move some comments (i.e. 2133219), I can split now if that's desirable. I’ve reverted the “error reducing” for fn main() {
let Some((1 + 2) * 3) = None::<i32>;
}
It's somewhat verbose, but that can be improved in a follow-up. I wonder if the match arm guard suggestions should be moved to another PR? Tracking issue #129967 will move the guard from the match arm to the pattern, so it might be best to wait for @rustbot ready |
/// Called by [`Parser::parse_stmt_without_recovery`], used to add statement-aware subdiagnostics to the errors stashed | ||
/// by [`Parser::maybe_recover_trailing_expr`]. | ||
pub(super) fn maybe_augment_stashed_expr_in_pats_with_suggestions(&mut self, stmt: &Stmt) { | ||
if self.dcx().has_errors().is_none() { |
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 you use has_stashed_diagnostic
for a more precise test?
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.
It requires the exact span which I don't have, and there's no
fn has_stashed_diagnostics(self: &DiagCtxtInner) -> bool {
!self.stashed_diagnostics.is_empty()
}
So it's kinda the best I can do without modifying the stashing API.
☔ The latest upstream changes (presumably #130091) made this pull request unmergeable. Please resolve the merge conflicts. |
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.
Tracking issue #129967 will move the guard from the match arm to the pattern, so it might be best to wait for guard_patterns's implementation's PR to be merged before adding guard suggestions.
I see what you're getting at but erm, I'd rather merge this first. This PR is a lot older and will immediately affect diagnostics. We don't know how long it will take for the initial guard-pattern PR to get to a ready state and merged. Also, it shouldn't be that hard to update this code, I think.
I'll bors-approve after a rebase. Thanks a lot for your patience! I took way too long for this
47413ca
to
db09345
Compare
@rustbot ready |
@bors r+ |
☀️ Test successful - checks-actions |
Finished benchmarking commit (df7f778): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results (primary 0.9%, secondary 2.2%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (secondary -2.3%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 766.835s -> 767.849s (0.13%) |
Follow-up of #118625, see #121697.
Before:
After:
r? fmease
@rustbot label +A-diagnostics +A-parser +A-patterns +C-enhancement