@@ -1039,6 +1039,13 @@ impl<'a> MethodDef<'a> {
1039
1039
let span = trait_. span ;
1040
1040
let mut patterns = Vec :: new ( ) ;
1041
1041
for i in 0 ..self_args. len ( ) {
1042
+ // Currently supports mutability only for `&mut self`
1043
+ let mutbl = match ( i, & self . explicit_self ) {
1044
+ ( 0 , Some ( Some ( PtrTy :: Borrowed ( _, mutbl) ) ) ) => * mutbl,
1045
+ ( 0 , Some ( Some ( PtrTy :: Raw ( mutbl) ) ) ) => * mutbl,
1046
+ _ => ast:: Mutability :: Not ,
1047
+ } ;
1048
+
1042
1049
// We could use `type_ident` instead of `Self`, but in the case of a type parameter
1043
1050
// shadowing the struct name, that causes a second, unnecessary E0578 error. #97343
1044
1051
let struct_path = cx. path ( span, vec ! [ Ident :: new( kw:: SelfUpper , type_ident. span) ] ) ;
@@ -1047,7 +1054,7 @@ impl<'a> MethodDef<'a> {
1047
1054
struct_path,
1048
1055
struct_def,
1049
1056
& format ! ( "__self_{}" , i) ,
1050
- ast :: Mutability :: Not ,
1057
+ mutbl ,
1051
1058
use_temporaries,
1052
1059
) ;
1053
1060
patterns. push ( pat) ;
@@ -1250,28 +1257,35 @@ impl<'a> MethodDef<'a> {
1250
1257
. enumerate ( )
1251
1258
. filter ( |& ( _, v) | !( self . unify_fieldless_variants && v. data . fields ( ) . is_empty ( ) ) )
1252
1259
. map ( |( index, variant) | {
1253
- let mk_self_pat = |cx : & mut ExtCtxt < ' _ > , self_arg_name : & str | {
1254
- let ( p, idents) = trait_. create_enum_variant_pattern (
1255
- cx,
1256
- type_ident,
1257
- variant,
1258
- self_arg_name,
1259
- ast:: Mutability :: Not ,
1260
- ) ;
1261
- ( cx. pat ( span, PatKind :: Ref ( p, ast:: Mutability :: Not ) ) , idents)
1260
+ // Support mutability only for `&mut self` for now.
1261
+ let self_mutbl = match & self . explicit_self {
1262
+ Some ( Some ( PtrTy :: Borrowed ( _, mutbl) ) ) => * mutbl,
1263
+ Some ( Some ( PtrTy :: Raw ( mutbl) ) ) => * mutbl,
1264
+ _ => ast:: Mutability :: Not ,
1262
1265
} ;
1266
+ let mk_self_pat =
1267
+ |cx : & mut ExtCtxt < ' _ > , self_arg_name : & str , mutbl : ast:: Mutability | {
1268
+ let ( p, idents) = trait_. create_enum_variant_pattern (
1269
+ cx,
1270
+ type_ident,
1271
+ variant,
1272
+ self_arg_name,
1273
+ mutbl,
1274
+ ) ;
1275
+ ( cx. pat ( span, PatKind :: Ref ( p, mutbl) ) , idents)
1276
+ } ;
1263
1277
1264
1278
// A single arm has form (&VariantK, &VariantK, ...) => BodyK
1265
1279
// (see "Final wrinkle" note below for why.)
1266
1280
let mut subpats = Vec :: with_capacity ( self_arg_names. len ( ) ) ;
1267
1281
let mut self_pats_idents = Vec :: with_capacity ( self_arg_names. len ( ) - 1 ) ;
1268
1282
let first_self_pat_idents = {
1269
- let ( p, idents) = mk_self_pat ( cx, & self_arg_names[ 0 ] ) ;
1283
+ let ( p, idents) = mk_self_pat ( cx, & self_arg_names[ 0 ] , self_mutbl ) ;
1270
1284
subpats. push ( p) ;
1271
1285
idents
1272
1286
} ;
1273
1287
for self_arg_name in & self_arg_names[ 1 ..] {
1274
- let ( p, idents) = mk_self_pat ( cx, & self_arg_name) ;
1288
+ let ( p, idents) = mk_self_pat ( cx, & self_arg_name, ast :: Mutability :: Not ) ;
1275
1289
subpats. push ( p) ;
1276
1290
self_pats_idents. push ( idents) ;
1277
1291
}
0 commit comments