From df7be435d3a1ad05ba8b71b2966b4ec9b2a1229e Mon Sep 17 00:00:00 2001 From: scalexm Date: Mon, 7 Aug 2017 17:14:20 +0200 Subject: [PATCH] Optimize clone shim for `Copy` types --- src/librustc_mir/shim.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/librustc_mir/shim.rs b/src/librustc_mir/shim.rs index 818e713cf48..a9fa596a567 100644 --- a/src/librustc_mir/shim.rs +++ b/src/librustc_mir/shim.rs @@ -347,7 +347,20 @@ fn build_clone_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>, loc }; + let is_copy = !self_ty.moves_by_default(tcx, tcx.param_env(def_id), span); + match self_ty.sty { + _ if is_copy => { + // `return *self;` + let statement = Statement { + source_info: source_info, + kind: StatementKind::Assign( + Lvalue::Local(RETURN_POINTER), + Rvalue::Use(Operand::Consume(rcvr)) + ) + }; + block(&mut blocks, statement, TerminatorKind::Return); + } ty::TyArray(ty, len) => { let mut returns = Vec::new(); for i in 0..len { @@ -402,15 +415,7 @@ fn build_clone_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>, block(&mut blocks, statement, TerminatorKind::Return); } _ => { - // `return *self;` - let statement = Statement { - source_info: source_info, - kind: StatementKind::Assign( - Lvalue::Local(RETURN_POINTER), - Rvalue::Use(Operand::Consume(rcvr)) - ) - }; - block(&mut blocks, statement, TerminatorKind::Return); + bug!("builtin shim for `{:?}` which is not `Copy` and is not an aggregate", self_ty); } };