Skip to content
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

Postfix match fixes #123394

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_errors::{DiagArgValue, IntoDiagArg};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::{BindingAnnotation, ByRef, RangeEnd};
use rustc_hir::{BindingAnnotation, ByRef, MatchSource, RangeEnd};
use rustc_index::newtype_index;
use rustc_index::IndexVec;
use rustc_middle::middle::region;
Expand Down Expand Up @@ -358,6 +358,7 @@ pub enum ExprKind<'tcx> {
scrutinee: ExprId,
scrutinee_hir_id: hir::HirId,
arms: Box<[ArmId]>,
match_source: MatchSource,
},
/// A block.
Block {
Expand Down
23 changes: 8 additions & 15 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,16 @@ pub enum UnusedUnsafeEnclosing {

pub(crate) struct NonExhaustivePatternsTypeNotEmpty<'p, 'tcx, 'm> {
pub cx: &'m RustcPatCtxt<'p, 'tcx>,
pub expr_span: Span,
pub span: Span,
pub scrut_span: Span,
pub braces_span: Option<Span>,
pub ty: Ty<'tcx>,
}

impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> {
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'_, G> {
let mut diag =
Diag::new(dcx, level, fluent::mir_build_non_exhaustive_patterns_type_not_empty);
diag.span(self.span);
diag.span(self.scrut_span);
diag.code(E0004);
let peeled_ty = self.ty.peel_refs();
diag.arg("ty", self.ty);
Expand Down Expand Up @@ -502,26 +502,19 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for NonExhaustivePatternsTypeNo
}
}

let mut suggestion = None;
let sm = self.cx.tcx.sess.source_map();
if self.span.eq_ctxt(self.expr_span) {
if let Some(braces_span) = self.braces_span {
// Get the span for the empty match body `{}`.
let (indentation, more) = if let Some(snippet) = sm.indentation_before(self.span) {
let (indentation, more) = if let Some(snippet) = sm.indentation_before(self.scrut_span)
{
(format!("\n{snippet}"), " ")
} else {
(" ".to_string(), "")
};
suggestion = Some((
self.span.shrink_to_hi().with_hi(self.expr_span.hi()),
format!(" {{{indentation}{more}_ => todo!(),{indentation}}}",),
));
}

if let Some((span, sugg)) = suggestion {
diag.span_suggestion_verbose(
span,
braces_span,
fluent::mir_build_suggestion,
sugg,
format!(" {{{indentation}{more}_ => todo!(),{indentation}}}"),
Applicability::HasPlaceholders,
);
} else {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,10 +716,11 @@ impl<'tcx> Cx<'tcx> {
then: self.mirror_expr(then),
else_opt: else_opt.map(|el| self.mirror_expr(el)),
},
hir::ExprKind::Match(discr, arms, _) => ExprKind::Match {
hir::ExprKind::Match(discr, arms, match_source) => ExprKind::Match {
scrutinee: self.mirror_expr(discr),
scrutinee_hir_id: discr.hir_id,
arms: arms.iter().map(|a| self.convert_arm(a)).collect(),
match_source,
},
hir::ExprKind::Loop(body, ..) => {
let block_ty = self.typeck_results().node_type(body.hir_id);
Expand Down
57 changes: 41 additions & 16 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
estebank marked this conversation as resolved.
Show resolved Hide resolved
.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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we need to audit and remove every span_extend_to_next_char or leave a comment explaining what stopped us from doing so.

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,
));
}
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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,
});
}
Expand Down Expand Up @@ -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}}}",),
));
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ impl<'a> Parser<'a> {
ExprKind::MethodCall(_) => "a method call",
ExprKind::Call(_, _) => "a function call",
ExprKind::Await(_, _) => "`.await`",
ExprKind::Match(_, _, MatchKind::Postfix) => "a postfix match",
ExprKind::Err(_) => return Ok(with_postfix),
_ => unreachable!("parse_dot_or_call_expr_with_ shouldn't produce this"),
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/match/postfix-match/match-after-as.rs
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
}
28 changes: 28 additions & 0 deletions tests/ui/match/postfix-match/match-after-as.stderr
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`.
Loading