-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Postfix match fixes #123394
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
Merged
Merged
Postfix match fixes #123394
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,16 +144,8 @@ impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> { | |
}); | ||
return; | ||
} | ||
ExprKind::Match { scrutinee, scrutinee_hir_id, box ref arms } => { | ||
let source = match ex.span.desugaring_kind() { | ||
Some(DesugaringKind::ForLoop) => hir::MatchSource::ForLoopDesugar, | ||
Some(DesugaringKind::QuestionMark) => { | ||
hir::MatchSource::TryDesugar(scrutinee_hir_id) | ||
} | ||
Some(DesugaringKind::Await) => hir::MatchSource::AwaitDesugar, | ||
_ => hir::MatchSource::Normal, | ||
}; | ||
self.check_match(scrutinee, arms, source, ex.span); | ||
ExprKind::Match { scrutinee, scrutinee_hir_id: _, box ref arms, match_source } => { | ||
self.check_match(scrutinee, arms, match_source, ex.span); | ||
} | ||
ExprKind::Let { box ref pat, expr } => { | ||
self.check_let(pat, Some(expr), ex.span); | ||
|
@@ -505,8 +497,41 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { | |
None, | ||
); | ||
} else { | ||
// span after scrutinee, or after `.match`. That is, the braces, arms, | ||
// and any whitespace preceding the braces. | ||
let braces_span = match source { | ||
hir::MatchSource::Normal => scrut | ||
.span | ||
.find_ancestor_in_same_ctxt(expr_span) | ||
.map(|scrut_span| scrut_span.shrink_to_hi().with_hi(expr_span.hi())), | ||
hir::MatchSource::Postfix => { | ||
// This is horrendous, and we should deal with it by just | ||
// stashing the span of the braces somewhere (like in the match source). | ||
scrut.span.find_ancestor_in_same_ctxt(expr_span).and_then(|scrut_span| { | ||
let sm = self.tcx.sess.source_map(); | ||
let brace_span = sm.span_extend_to_next_char(scrut_span, '{', true); | ||
Comment on lines
+508
to
+512
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, we need to audit and remove every |
||
if sm.span_to_snippet(sm.next_point(brace_span)).as_deref() == Ok("{") { | ||
let sp = brace_span.shrink_to_hi().with_hi(expr_span.hi()); | ||
// We also need to extend backwards for whitespace | ||
sm.span_extend_prev_while(sp, |c| c.is_whitespace()).ok() | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
hir::MatchSource::ForLoopDesugar | ||
| hir::MatchSource::TryDesugar(_) | ||
| hir::MatchSource::AwaitDesugar | ||
| hir::MatchSource::FormatArgs => None, | ||
}; | ||
self.error = Err(report_non_exhaustive_match( | ||
&cx, self.thir, scrut.ty, scrut.span, witnesses, arms, expr_span, | ||
&cx, | ||
self.thir, | ||
scrut.ty, | ||
scrut.span, | ||
witnesses, | ||
arms, | ||
braces_span, | ||
)); | ||
} | ||
} | ||
|
@@ -929,7 +954,7 @@ fn report_non_exhaustive_match<'p, 'tcx>( | |
sp: Span, | ||
witnesses: Vec<WitnessPat<'p, 'tcx>>, | ||
arms: &[ArmId], | ||
expr_span: Span, | ||
braces_span: Option<Span>, | ||
) -> ErrorGuaranteed { | ||
let is_empty_match = arms.is_empty(); | ||
let non_empty_enum = match scrut_ty.kind() { | ||
|
@@ -941,8 +966,8 @@ fn report_non_exhaustive_match<'p, 'tcx>( | |
if is_empty_match && !non_empty_enum { | ||
return cx.tcx.dcx().emit_err(NonExhaustivePatternsTypeNotEmpty { | ||
cx, | ||
expr_span, | ||
span: sp, | ||
scrut_span: sp, | ||
braces_span, | ||
ty: scrut_ty, | ||
}); | ||
} | ||
|
@@ -1028,15 +1053,15 @@ fn report_non_exhaustive_match<'p, 'tcx>( | |
let mut suggestion = None; | ||
let sm = cx.tcx.sess.source_map(); | ||
match arms { | ||
[] if sp.eq_ctxt(expr_span) => { | ||
[] if let Some(braces_span) = braces_span => { | ||
// Get the span for the empty match body `{}`. | ||
let (indentation, more) = if let Some(snippet) = sm.indentation_before(sp) { | ||
(format!("\n{snippet}"), " ") | ||
} else { | ||
(" ".to_string(), "") | ||
}; | ||
suggestion = Some(( | ||
sp.shrink_to_hi().with_hi(expr_span.hi()), | ||
braces_span, | ||
format!(" {{{indentation}{more}{suggested_arm},{indentation}}}",), | ||
)); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![feature(postfix_match)] | ||
|
||
fn main() { | ||
1 as i32.match {}; | ||
//~^ ERROR cast cannot be followed by a postfix match | ||
//~| ERROR non-exhaustive patterns | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: cast cannot be followed by a postfix match | ||
--> $DIR/match-after-as.rs:4:5 | ||
| | ||
LL | 1 as i32.match {}; | ||
| ^^^^^^^^ | ||
| | ||
help: try surrounding the expression in parentheses | ||
| | ||
LL | (1 as i32).match {}; | ||
| + + | ||
|
||
error[E0004]: non-exhaustive patterns: type `i32` is non-empty | ||
--> $DIR/match-after-as.rs:4:5 | ||
| | ||
LL | 1 as i32.match {}; | ||
| ^^^^^^^^ | ||
| | ||
= note: the matched value is of type `i32` | ||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown | ||
| | ||
LL ~ 1 as i32.match { | ||
LL + _ => todo!(), | ||
LL ~ }; | ||
| | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0004`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.