summaryrefslogtreecommitdiffstats
path: root/src/test/mir-opt/sroa.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:32 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:32 +0000
commit4547b622d8d29df964fa2914213088b148c498fc (patch)
tree9fc6b25f3c3add6b745be9a2400a6e96140046e9 /src/test/mir-opt/sroa.rs
parentReleasing progress-linux version 1.66.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-4547b622d8d29df964fa2914213088b148c498fc.tar.xz
rustc-4547b622d8d29df964fa2914213088b148c498fc.zip
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/mir-opt/sroa.rs')
-rw-r--r--src/test/mir-opt/sroa.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/test/mir-opt/sroa.rs b/src/test/mir-opt/sroa.rs
new file mode 100644
index 000000000..ff8deb40d
--- /dev/null
+++ b/src/test/mir-opt/sroa.rs
@@ -0,0 +1,88 @@
+// unit-test: ScalarReplacementOfAggregates
+// compile-flags: -Cpanic=abort
+// no-prefer-dynamic
+
+struct Tag(usize);
+
+#[repr(C)]
+struct S(Tag, Tag, Tag);
+
+impl Drop for Tag {
+ #[inline(never)]
+ fn drop(&mut self) {}
+}
+
+// EMIT_MIR sroa.dropping.ScalarReplacementOfAggregates.diff
+pub fn dropping() {
+ S(Tag(0), Tag(1), Tag(2)).1;
+}
+
+// EMIT_MIR sroa.enums.ScalarReplacementOfAggregates.diff
+pub fn enums(a: usize) -> usize {
+ if let Some(a) = Some(a) { a } else { 0 }
+}
+
+// EMIT_MIR sroa.structs.ScalarReplacementOfAggregates.diff
+pub fn structs(a: f32) -> f32 {
+ struct U {
+ _foo: usize,
+ a: f32,
+ }
+
+ U { _foo: 0, a }.a
+}
+
+// EMIT_MIR sroa.unions.ScalarReplacementOfAggregates.diff
+pub fn unions(a: f32) -> u32 {
+ union Repr {
+ f: f32,
+ u: u32,
+ }
+ unsafe { Repr { f: a }.u }
+}
+
+struct Foo {
+ a: u8,
+ b: (),
+ c: &'static str,
+ d: Option<isize>,
+}
+
+fn g() -> u32 {
+ 3
+}
+
+// EMIT_MIR sroa.flat.ScalarReplacementOfAggregates.diff
+pub fn flat() {
+ let Foo { a, b, c, d } = Foo { a: 5, b: (), c: "a", d: Some(-4) };
+ let _ = a;
+ let _ = b;
+ let _ = c;
+ let _ = d;
+}
+
+#[repr(C)]
+struct Escaping {
+ a: u32,
+ b: u32,
+ c: u32,
+}
+
+fn f(a: *const u32) {
+ println!("{}", unsafe { *a.add(2) });
+}
+
+// EMIT_MIR sroa.escaping.ScalarReplacementOfAggregates.diff
+pub fn escaping() {
+ // Verify this struct is not flattened.
+ f(&Escaping { a: 1, b: 2, c: g() }.a);
+}
+
+fn main() {
+ dropping();
+ enums(5);
+ structs(5.);
+ unions(5.);
+ flat();
+ escaping();
+}