Skip to content

Commit c5266e4

Browse files
Finish struct, progress tuple
1 parent 7fdc4f5 commit c5266e4

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,12 @@ fn cs_clone_from(
300300
&format!("not exactly 2 arguments in `clone_from` in `derive({})`", name),
301301
);
302302
}
303+
let self_ = cx.expr_addr_of_mut(field.span, field.self_.clone());
304+
let other = cx.expr_addr_of(field.span, field.other[0].clone());
303305
cx.stmt_semi(cx.expr_call_global(
304306
field.span,
305307
clone_from_path.clone(),
306-
vec![field.self_.clone(), field.other[0].clone()],
308+
vec![self_, other],
307309
))
308310
})
309311
.collect();

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,13 @@ impl<'a> MethodDef<'a> {
10391039
let span = trait_.span;
10401040
let mut patterns = Vec::new();
10411041
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+
10421049
// We could use `type_ident` instead of `Self`, but in the case of a type parameter
10431050
// shadowing the struct name, that causes a second, unnecessary E0578 error. #97343
10441051
let struct_path = cx.path(span, vec![Ident::new(kw::SelfUpper, type_ident.span)]);
@@ -1047,7 +1054,7 @@ impl<'a> MethodDef<'a> {
10471054
struct_path,
10481055
struct_def,
10491056
&format!("__self_{}", i),
1050-
ast::Mutability::Not,
1057+
mutbl,
10511058
use_temporaries,
10521059
);
10531060
patterns.push(pat);
@@ -1250,28 +1257,35 @@ impl<'a> MethodDef<'a> {
12501257
.enumerate()
12511258
.filter(|&(_, v)| !(self.unify_fieldless_variants && v.data.fields().is_empty()))
12521259
.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,
12621265
};
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+
};
12631277

12641278
// A single arm has form (&VariantK, &VariantK, ...) => BodyK
12651279
// (see "Final wrinkle" note below for why.)
12661280
let mut subpats = Vec::with_capacity(self_arg_names.len());
12671281
let mut self_pats_idents = Vec::with_capacity(self_arg_names.len() - 1);
12681282
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);
12701284
subpats.push(p);
12711285
idents
12721286
};
12731287
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);
12751289
subpats.push(p);
12761290
self_pats_idents.push(idents);
12771291
}

compiler/rustc_expand/src/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ impl<'a> ExtCtxt<'a> {
243243
self.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Not, e))
244244
}
245245

246+
pub fn expr_addr_of_mut(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
247+
self.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Mut, e))
248+
}
249+
246250
pub fn expr_call(
247251
&self,
248252
span: Span,

0 commit comments

Comments
 (0)