@@ -185,7 +185,6 @@ use rustc_ast::ptr::P;
185
185
use rustc_ast:: { self as ast, BinOpKind , EnumDef , Expr , Generics , PatKind } ;
186
186
use rustc_ast:: { GenericArg , GenericParamKind , VariantData } ;
187
187
use rustc_attr as attr;
188
- use rustc_data_structures:: map_in_place:: MapInPlace ;
189
188
use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
190
189
use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
191
190
use rustc_span:: Span ;
@@ -1207,7 +1206,7 @@ impl<'a> MethodDef<'a> {
1207
1206
trait_ : & TraitDef < ' b > ,
1208
1207
enum_def : & ' b EnumDef ,
1209
1208
type_ident : Ident ,
1210
- mut self_args : Vec < P < Expr > > ,
1209
+ self_args : Vec < P < Expr > > ,
1211
1210
nonself_args : & [ P < Expr > ] ,
1212
1211
) -> P < Expr > {
1213
1212
let span = trait_. span ;
@@ -1247,6 +1246,13 @@ impl<'a> MethodDef<'a> {
1247
1246
1248
1247
let first_fieldless = variants. iter ( ) . find ( |v| v. data . fields ( ) . is_empty ( ) ) ;
1249
1248
1249
+ // Support mutability only for `&mut self` for now.
1250
+ let self_mutbl = match & self . explicit_self {
1251
+ Some ( Some ( PtrTy :: Borrowed ( _, mutbl) ) ) => * mutbl,
1252
+ Some ( Some ( PtrTy :: Raw ( mutbl) ) ) => * mutbl,
1253
+ _ => ast:: Mutability :: Not ,
1254
+ } ;
1255
+
1250
1256
// These arms are of the form:
1251
1257
// (Variant1, Variant1, ...) => Body1
1252
1258
// (Variant2, Variant2, ...) => Body2
@@ -1257,12 +1263,6 @@ impl<'a> MethodDef<'a> {
1257
1263
. enumerate ( )
1258
1264
. filter ( |& ( _, v) | !( self . unify_fieldless_variants && v. data . fields ( ) . is_empty ( ) ) )
1259
1265
. map ( |( index, variant) | {
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 ,
1265
- } ;
1266
1266
let mk_self_pat =
1267
1267
|cx : & mut ExtCtxt < ' _ > , self_arg_name : & str , mutbl : ast:: Mutability | {
1268
1268
let ( p, idents) = trait_. create_enum_variant_pattern (
@@ -1454,7 +1454,17 @@ impl<'a> MethodDef<'a> {
1454
1454
// them when they are fed as r-values into a tuple
1455
1455
// expression; here add a layer of borrowing, turning
1456
1456
// `(*self, *__arg_0, ...)` into `(&*self, &*__arg_0, ...)`.
1457
- self_args. map_in_place ( |self_arg| cx. expr_addr_of ( span, self_arg) ) ;
1457
+ let self_args: Vec < _ > = self_args
1458
+ . into_iter ( )
1459
+ . enumerate ( )
1460
+ . map ( |( i, self_arg) | {
1461
+ if i == 0 && self_mutbl == ast:: Mutability :: Mut {
1462
+ cx. expr_addr_of_mut ( span, self_arg)
1463
+ } else {
1464
+ cx. expr_addr_of ( span, self_arg)
1465
+ }
1466
+ } )
1467
+ . collect ( ) ;
1458
1468
let match_arg = cx. expr ( span, ast:: ExprKind :: Tup ( self_args) ) ;
1459
1469
1460
1470
// Lastly we create an expression which branches on all discriminants being equal
@@ -1530,7 +1540,17 @@ impl<'a> MethodDef<'a> {
1530
1540
// them when they are fed as r-values into a tuple
1531
1541
// expression; here add a layer of borrowing, turning
1532
1542
// `(*self, *__arg_0, ...)` into `(&*self, &*__arg_0, ...)`.
1533
- self_args. map_in_place ( |self_arg| cx. expr_addr_of ( span, self_arg) ) ;
1543
+ let self_args: Vec < _ > = self_args
1544
+ . into_iter ( )
1545
+ . enumerate ( )
1546
+ . map ( |( i, self_arg) | {
1547
+ if i == 0 && self_mutbl == ast:: Mutability :: Mut {
1548
+ cx. expr_addr_of_mut ( span, self_arg)
1549
+ } else {
1550
+ cx. expr_addr_of ( span, self_arg)
1551
+ }
1552
+ } )
1553
+ . collect ( ) ;
1534
1554
let match_arg = cx. expr ( span, ast:: ExprKind :: Tup ( self_args) ) ;
1535
1555
cx. expr_match ( span, match_arg, match_arms)
1536
1556
}
0 commit comments