@@ -388,6 +388,18 @@ impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
388388 }
389389 }
390390
391+ fn is_non_exhaustive_variant < ' p > ( & self , pattern : & ' p Pattern < ' tcx > ) -> bool
392+ where ' a : ' p
393+ {
394+ match * pattern. kind {
395+ PatternKind :: Variant { adt_def, variant_index, .. } => {
396+ let ref variant = adt_def. variants [ variant_index] ;
397+ variant. is_field_list_non_exhaustive ( )
398+ }
399+ _ => false ,
400+ }
401+ }
402+
391403 fn is_non_exhaustive_enum ( & self , ty : Ty < ' tcx > ) -> bool {
392404 match ty. sty {
393405 ty:: Adt ( adt_def, ..) => adt_def. is_variant_list_non_exhaustive ( ) ,
@@ -1097,10 +1109,17 @@ pub fn is_useful<'p, 'a: 'p, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
10971109 debug ! ( "is_useful_expand_first_col: pcx={:#?}, expanding {:#?}" , pcx, v[ 0 ] ) ;
10981110
10991111 if let Some ( constructors) = pat_constructors ( cx, v[ 0 ] , pcx) {
1100- debug ! ( "is_useful - expanding constructors: {:#?}" , constructors) ;
1101- split_grouped_constructors ( cx. tcx , constructors, matrix, pcx. ty ) . into_iter ( ) . map ( |c|
1102- is_useful_specialized ( cx, matrix, v, c, pcx. ty , witness)
1103- ) . find ( |result| result. is_useful ( ) ) . unwrap_or ( NotUseful )
1112+ let is_declared_nonexhaustive = cx. is_non_exhaustive_variant ( v[ 0 ] ) && !cx. is_local ( pcx. ty ) ;
1113+ debug ! ( "is_useful - expanding constructors: {:#?}, is_declared_nonexhaustive: {:?}" ,
1114+ constructors, is_declared_nonexhaustive) ;
1115+
1116+ if is_declared_nonexhaustive {
1117+ Useful
1118+ } else {
1119+ split_grouped_constructors ( cx. tcx , constructors, matrix, pcx. ty ) . into_iter ( ) . map ( |c|
1120+ is_useful_specialized ( cx, matrix, v, c, pcx. ty , witness)
1121+ ) . find ( |result| result. is_useful ( ) ) . unwrap_or ( NotUseful )
1122+ }
11041123 } else {
11051124 debug ! ( "is_useful - expanding wildcard" ) ;
11061125
0 commit comments