@@ -1032,22 +1032,29 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
1032
1032
ret
1033
1033
}
1034
1034
1035
- pub fn cat_downcast < N : ast_node > ( & self ,
1036
- node : & N ,
1037
- base_cmt : cmt < ' tcx > ,
1038
- downcast_ty : Ty < ' tcx > ,
1039
- variant_did : DefId )
1040
- -> cmt < ' tcx > {
1041
- let ret = Rc :: new ( cmt_ {
1042
- id : node. id ( ) ,
1043
- span : node. span ( ) ,
1044
- mutbl : base_cmt. mutbl . inherit ( ) ,
1045
- cat : Categorization :: Downcast ( base_cmt, variant_did) ,
1046
- ty : downcast_ty,
1047
- note : NoteNone
1048
- } ) ;
1049
- debug ! ( "cat_downcast ret={:?}" , ret) ;
1050
- ret
1035
+ pub fn cat_downcast_if_needed < N : ast_node > ( & self ,
1036
+ node : & N ,
1037
+ base_cmt : cmt < ' tcx > ,
1038
+ variant_did : DefId )
1039
+ -> cmt < ' tcx > {
1040
+ // univariant enums do not need downcasts
1041
+ let base_did = self . tcx . parent_def_id ( variant_did) . unwrap ( ) ;
1042
+ if !self . tcx . adt_def ( base_did) . is_univariant ( ) {
1043
+ let base_ty = base_cmt. ty ;
1044
+ let ret = Rc :: new ( cmt_ {
1045
+ id : node. id ( ) ,
1046
+ span : node. span ( ) ,
1047
+ mutbl : base_cmt. mutbl . inherit ( ) ,
1048
+ cat : Categorization :: Downcast ( base_cmt, variant_did) ,
1049
+ ty : base_ty,
1050
+ note : NoteNone
1051
+ } ) ;
1052
+ debug ! ( "cat_downcast ret={:?}" , ret) ;
1053
+ ret
1054
+ } else {
1055
+ debug ! ( "cat_downcast univariant={:?}" , base_cmt) ;
1056
+ base_cmt
1057
+ }
1051
1058
}
1052
1059
1053
1060
pub fn cat_pattern < F > ( & self , cmt : cmt < ' tcx > , pat : & hir:: Pat , mut op : F ) -> McResult < ( ) >
@@ -1109,45 +1116,23 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
1109
1116
1110
1117
op ( cmt. clone ( ) , pat) ;
1111
1118
1112
- // Note: This goes up here (rather than within the PatKind::TupleStruct arm
1113
- // alone) because PatKind::Struct can also refer to variants.
1114
- let cmt = match pat. node {
1115
- PatKind :: Path ( hir:: QPath :: Resolved ( _, ref path) ) |
1116
- PatKind :: TupleStruct ( hir:: QPath :: Resolved ( _, ref path) , ..) |
1117
- PatKind :: Struct ( hir:: QPath :: Resolved ( _, ref path) , ..) => {
1118
- match path. def {
1119
- Def :: Err => {
1120
- debug ! ( "access to unresolvable pattern {:?}" , pat) ;
1121
- return Err ( ( ) )
1122
- }
1123
- Def :: Variant ( variant_did) |
1124
- Def :: VariantCtor ( variant_did, ..) => {
1125
- // univariant enums do not need downcasts
1126
- let enum_did = self . tcx . parent_def_id ( variant_did) . unwrap ( ) ;
1127
- if !self . tcx . adt_def ( enum_did) . is_univariant ( ) {
1128
- self . cat_downcast ( pat, cmt. clone ( ) , cmt. ty , variant_did)
1129
- } else {
1130
- cmt
1131
- }
1132
- }
1133
- _ => cmt
1134
- }
1135
- }
1136
- _ => cmt
1137
- } ;
1138
-
1139
1119
match pat. node {
1140
1120
PatKind :: TupleStruct ( ref qpath, ref subpats, ddpos) => {
1141
1121
let def = self . tables . qpath_def ( qpath, pat. id ) ;
1142
- let expected_len = match def {
1122
+ let ( cmt, expected_len) = match def {
1123
+ Def :: Err => {
1124
+ debug ! ( "access to unresolvable pattern {:?}" , pat) ;
1125
+ return Err ( ( ) )
1126
+ }
1143
1127
Def :: VariantCtor ( def_id, CtorKind :: Fn ) => {
1144
1128
let enum_def = self . tcx . parent_def_id ( def_id) . unwrap ( ) ;
1145
- self . tcx . adt_def ( enum_def) . variant_with_id ( def_id) . fields . len ( )
1129
+ ( self . cat_downcast_if_needed ( pat, cmt, def_id) ,
1130
+ self . tcx . adt_def ( enum_def) . variant_with_id ( def_id) . fields . len ( ) )
1146
1131
}
1147
1132
Def :: StructCtor ( _, CtorKind :: Fn ) => {
1148
1133
match self . pat_ty ( & pat) ?. sty {
1149
1134
ty:: TyAdt ( adt_def, _) => {
1150
- adt_def. struct_variant ( ) . fields . len ( )
1135
+ ( cmt , adt_def. struct_variant ( ) . fields . len ( ) )
1151
1136
}
1152
1137
ref ty => {
1153
1138
span_bug ! ( pat. span, "tuple struct pattern unexpected type {:?}" , ty) ;
@@ -1168,8 +1153,21 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
1168
1153
}
1169
1154
}
1170
1155
1171
- PatKind :: Struct ( _ , ref field_pats, _) => {
1156
+ PatKind :: Struct ( ref qpath , ref field_pats, _) => {
1172
1157
// {f1: p1, ..., fN: pN}
1158
+ let def = self . tables . qpath_def ( qpath, pat. id ) ;
1159
+ let cmt = match def {
1160
+ Def :: Err => {
1161
+ debug ! ( "access to unresolvable pattern {:?}" , pat) ;
1162
+ return Err ( ( ) )
1163
+ } ,
1164
+ Def :: Variant ( variant_did) |
1165
+ Def :: VariantCtor ( variant_did, ..) => {
1166
+ self . cat_downcast_if_needed ( pat, cmt, variant_did)
1167
+ } ,
1168
+ _ => cmt
1169
+ } ;
1170
+
1173
1171
for fp in field_pats {
1174
1172
let field_ty = self . pat_ty ( & fp. node . pat ) ?; // see (*2)
1175
1173
let cmt_field = self . cat_field ( pat, cmt. clone ( ) , fp. node . name , field_ty) ;
0 commit comments