@@ -144,16 +144,8 @@ impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
144
144
} ) ;
145
145
return ;
146
146
}
147
- ExprKind :: Match { scrutinee, scrutinee_hir_id, box ref arms } => {
148
- let source = match ex. span . desugaring_kind ( ) {
149
- Some ( DesugaringKind :: ForLoop ) => hir:: MatchSource :: ForLoopDesugar ,
150
- Some ( DesugaringKind :: QuestionMark ) => {
151
- hir:: MatchSource :: TryDesugar ( scrutinee_hir_id)
152
- }
153
- Some ( DesugaringKind :: Await ) => hir:: MatchSource :: AwaitDesugar ,
154
- _ => hir:: MatchSource :: Normal ,
155
- } ;
156
- self . check_match ( scrutinee, arms, source, ex. span ) ;
147
+ ExprKind :: Match { scrutinee, scrutinee_hir_id : _, box ref arms, match_source } => {
148
+ self . check_match ( scrutinee, arms, match_source, ex. span ) ;
157
149
}
158
150
ExprKind :: Let { box ref pat, expr } => {
159
151
self . check_let ( pat, Some ( expr) , ex. span ) ;
@@ -505,8 +497,41 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
505
497
None ,
506
498
) ;
507
499
} else {
500
+ // span after scrutinee, or after `.match`. That is, the braces, arms,
501
+ // and any whitespace preceding the braces.
502
+ let braces_span = match source {
503
+ hir:: MatchSource :: Normal => scrut
504
+ . span
505
+ . find_ancestor_in_same_ctxt ( expr_span)
506
+ . map ( |scrut_span| scrut_span. shrink_to_hi ( ) . with_hi ( expr_span. hi ( ) ) ) ,
507
+ hir:: MatchSource :: Postfix => {
508
+ // This is horrendous, and we should deal with it by just
509
+ // stashing the span of the braces somewhere (like in the match source).
510
+ scrut. span . find_ancestor_in_same_ctxt ( expr_span) . and_then ( |scrut_span| {
511
+ let sm = self . tcx . sess . source_map ( ) ;
512
+ let brace_span = sm. span_extend_to_next_char ( scrut_span, '{' , true ) ;
513
+ if sm. span_to_snippet ( sm. next_point ( brace_span) ) . as_deref ( ) == Ok ( "{" ) {
514
+ let sp = brace_span. shrink_to_hi ( ) . with_hi ( expr_span. hi ( ) ) ;
515
+ // We also need to extend backwards for whitespace
516
+ sm. span_extend_prev_while ( sp, |c| c. is_whitespace ( ) ) . ok ( )
517
+ } else {
518
+ None
519
+ }
520
+ } )
521
+ }
522
+ hir:: MatchSource :: ForLoopDesugar
523
+ | hir:: MatchSource :: TryDesugar ( _)
524
+ | hir:: MatchSource :: AwaitDesugar
525
+ | hir:: MatchSource :: FormatArgs => None ,
526
+ } ;
508
527
self . error = Err ( report_non_exhaustive_match (
509
- & cx, self . thir , scrut. ty , scrut. span , witnesses, arms, expr_span,
528
+ & cx,
529
+ self . thir ,
530
+ scrut. ty ,
531
+ scrut. span ,
532
+ witnesses,
533
+ arms,
534
+ braces_span,
510
535
) ) ;
511
536
}
512
537
}
@@ -929,7 +954,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
929
954
sp : Span ,
930
955
witnesses : Vec < WitnessPat < ' p , ' tcx > > ,
931
956
arms : & [ ArmId ] ,
932
- expr_span : Span ,
957
+ braces_span : Option < Span > ,
933
958
) -> ErrorGuaranteed {
934
959
let is_empty_match = arms. is_empty ( ) ;
935
960
let non_empty_enum = match scrut_ty. kind ( ) {
@@ -941,8 +966,8 @@ fn report_non_exhaustive_match<'p, 'tcx>(
941
966
if is_empty_match && !non_empty_enum {
942
967
return cx. tcx . dcx ( ) . emit_err ( NonExhaustivePatternsTypeNotEmpty {
943
968
cx,
944
- expr_span ,
945
- span : sp ,
969
+ scrut_span : sp ,
970
+ braces_span ,
946
971
ty : scrut_ty,
947
972
} ) ;
948
973
}
@@ -1028,15 +1053,15 @@ fn report_non_exhaustive_match<'p, 'tcx>(
1028
1053
let mut suggestion = None ;
1029
1054
let sm = cx. tcx . sess . source_map ( ) ;
1030
1055
match arms {
1031
- [ ] if sp . eq_ctxt ( expr_span ) => {
1056
+ [ ] if let Some ( braces_span ) = braces_span => {
1032
1057
// Get the span for the empty match body `{}`.
1033
1058
let ( indentation, more) = if let Some ( snippet) = sm. indentation_before ( sp) {
1034
1059
( format ! ( "\n {snippet}" ) , " " )
1035
1060
} else {
1036
1061
( " " . to_string ( ) , "" )
1037
1062
} ;
1038
1063
suggestion = Some ( (
1039
- sp . shrink_to_hi ( ) . with_hi ( expr_span . hi ( ) ) ,
1064
+ braces_span ,
1040
1065
format ! ( " {{{indentation}{more}{suggested_arm},{indentation}}}" , ) ,
1041
1066
) ) ;
1042
1067
}
0 commit comments