Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support building clone shims for arrays with generic size #79272

Merged
merged 1 commit into from
Nov 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions compiler/rustc_mir/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,7 @@ fn build_clone_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -

match self_ty.kind() {
_ if is_copy => builder.copy_shim(),
ty::Array(ty, len) => {
let len = len.eval_usize(tcx, param_env);
builder.array_shim(dest, src, ty, len)
}
ty::Array(ty, len) => builder.array_shim(dest, src, ty, len),
ty::Closure(_, substs) => {
builder.tuple_like_shim(dest, src, substs.as_closure().upvar_tys())
}
Expand Down Expand Up @@ -485,7 +482,13 @@ impl CloneShimBuilder<'tcx> {
}
}

fn array_shim(&mut self, dest: Place<'tcx>, src: Place<'tcx>, ty: Ty<'tcx>, len: u64) {
fn array_shim(
&mut self,
dest: Place<'tcx>,
src: Place<'tcx>,
ty: Ty<'tcx>,
len: &'tcx ty::Const<'tcx>,
) {
let tcx = self.tcx;
let span = self.span;

Expand All @@ -503,7 +506,11 @@ impl CloneShimBuilder<'tcx> {
))),
self.make_statement(StatementKind::Assign(box (
end,
Rvalue::Use(Operand::Constant(self.make_usize(len))),
Rvalue::Use(Operand::Constant(box Constant {
span: self.span,
user_ty: None,
literal: len,
})),
))),
];
self.block(inits, TerminatorKind::Goto { target: BasicBlock::new(1) }, false);
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/mir/mir-inlining/array-clone-with-generic-size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Checks that we can build a clone shim for array with generic size.
// Regression test for issue #79269.
//
// build-pass
// compile-flags: -Zmir-opt-level=2 -Zvalidate-mir
#![feature(min_const_generics)]

#[derive(Clone)]
struct Array<T, const N: usize>([T; N]);

fn main() {
let _ = Array([0u32, 1u32, 2u32]).clone();
}