rust/tests/mir-opt/pre-codegen/clone_as_copy.rs
DianQK c16c22cc9c
Simplify the canonical clone method to copy
The optimized clone method ends up as the following MIR:

```
_2 = copy ((*_1).0: i32);
_3 = copy ((*_1).1: u64);
_4 = copy ((*_1).2: [i8; 3]);
_0 = Foo { a: move _2, b: move _3, c: move _4 };
```

We can transform this to:

```
_0 = copy (*_1);
```
2024-09-14 13:30:35 +08:00

44 lines
966 B
Rust

//@ compile-flags: -Cdebuginfo=full
// Check if we have transformed the nested clone to the copy in the complete pipeline.
#[derive(Clone)]
struct AllCopy {
a: i32,
b: u64,
c: [i8; 3],
}
#[derive(Clone)]
struct NestCopy {
a: i32,
b: AllCopy,
c: [i8; 3],
}
#[derive(Clone)]
enum Enum1 {
A(AllCopy),
B(NestCopy),
}
// EMIT_MIR clone_as_copy.clone_as_copy.PreCodegen.after.mir
fn clone_as_copy(v: &NestCopy) -> NestCopy {
// CHECK-LABEL: fn clone_as_copy(
// CHECK-NOT: = AllCopy { {{.*}} };
// CHECK-NOT: = NestCopy { {{.*}} };
// CHECK: _0 = copy (*_1);
// CHECK: return;
v.clone()
}
// FIXME: We can merge into exactly one assignment statement.
// EMIT_MIR clone_as_copy.enum_clone_as_copy.PreCodegen.after.mir
fn enum_clone_as_copy(v: &Enum1) -> Enum1 {
// CHECK-LABEL: fn enum_clone_as_copy(
// CHECK-NOT: = Enum1::
// CHECK: _0 = copy (*_1);
// CHECK: _0 = copy (*_1);
v.clone()
}