From b95852b93c8a7b70f9238b0bbd787de68c9241bd Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Tue, 11 Apr 2023 16:38:00 -0700 Subject: [PATCH] test improvements --- compiler/rustc_middle/src/mir/mod.rs | 6 ++- library/core/tests/mem.rs | 21 +++++++++ ...diff => offset_of.concrete.ConstProp.diff} | 24 +++++----- .../offset_of.generic.ConstProp.diff | 39 ++++++++++++++++ tests/mir-opt/const_prop/offset_of.rs | 44 ++++++++++++++----- tests/ui/liveness/liveness-offset-of.rs | 18 ++++++++ tests/ui/liveness/liveness-offset-of.stderr | 19 +++++++- 7 files changed, 147 insertions(+), 24 deletions(-) rename tests/mir-opt/const_prop/{offset_of.main.ConstProp.diff => offset_of.concrete.ConstProp.diff} (67%) create mode 100644 tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index f985aae9a22..e72771b61f8 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2048,7 +2048,11 @@ impl<'tcx> Debug for Rvalue<'tcx> { } UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a), Discriminant(ref place) => write!(fmt, "discriminant({:?})", place), - NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t), + NullaryOp(ref op, ref t) => match op { + NullOp::SizeOf => write!(fmt, "SizeOf({:?})", t), + NullOp::AlignOf => write!(fmt, "AlignOf({:?})", t), + NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({:?}, {:?})", t, fields), + }, ThreadLocalRef(did) => ty::tls::with(|tcx| { let muta = tcx.static_mutability(did).unwrap().prefix_str(); write!(fmt, "&/*tls*/ {}{}", muta, tcx.def_path_str(did)) diff --git a/library/core/tests/mem.rs b/library/core/tests/mem.rs index c08fc6fd2fb..2351406ddd2 100644 --- a/library/core/tests/mem.rs +++ b/library/core/tests/mem.rs @@ -438,3 +438,24 @@ fn offset_of_dst() { assert_eq!(offset_of!(Foo, x), 0); assert_eq!(offset_of!(Foo, y), 2); } + +#[test] +#[cfg(not(bootstrap))] +fn offset_of_addr() { + #[repr(C)] + struct Foo { + x: u8, + y: u16, + z: Bar, + } + + #[repr(C)] + struct Bar(u8, u8); + + let base = Foo { x: 0, y: 0, z: Bar(0, 0) }; + + assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, x), ptr::addr_of!(base.x).addr()); + assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, y), ptr::addr_of!(base.y).addr()); + assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, z.0), ptr::addr_of!(base.z.0).addr()); + assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, z.1), ptr::addr_of!(base.z.1).addr()); +} diff --git a/tests/mir-opt/const_prop/offset_of.main.ConstProp.diff b/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff similarity index 67% rename from tests/mir-opt/const_prop/offset_of.main.ConstProp.diff rename to tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff index 0437ec81f1e..4e2f1b39d2b 100644 --- a/tests/mir-opt/const_prop/offset_of.main.ConstProp.diff +++ b/tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff @@ -1,8 +1,8 @@ -- // MIR for `main` before ConstProp -+ // MIR for `main` after ConstProp +- // MIR for `concrete` before ConstProp ++ // MIR for `concrete` after ConstProp - fn main() -> () { - let mut _0: (); // return place in scope 0 at $DIR/offset_of.rs:+0:11: +0:11 + fn concrete() -> () { + let mut _0: (); // return place in scope 0 at $DIR/offset_of.rs:+0:15: +0:15 let _1: usize; // in scope 0 at $DIR/offset_of.rs:+1:9: +1:10 scope 1 { debug x => _1; // in scope 1 at $DIR/offset_of.rs:+1:9: +1:10 @@ -22,17 +22,17 @@ bb0: { StorageLive(_1); // scope 0 at $DIR/offset_of.rs:+1:9: +1:10 -- _1 = OffsetOf([0])(Foo); // scope 0 at $DIR/offset_of.rs:+1:13: +1:31 -+ _1 = const 0_usize; // scope 0 at $DIR/offset_of.rs:+1:13: +1:31 +- _1 = OffsetOf(Alpha, [0]); // scope 0 at $DIR/offset_of.rs:+1:13: +1:33 ++ _1 = const 4_usize; // scope 0 at $DIR/offset_of.rs:+1:13: +1:33 StorageLive(_2); // scope 1 at $DIR/offset_of.rs:+2:9: +2:10 -- _2 = OffsetOf([1])(Foo); // scope 1 at $DIR/offset_of.rs:+2:13: +2:31 -+ _2 = const 2_usize; // scope 1 at $DIR/offset_of.rs:+2:13: +2:31 +- _2 = OffsetOf(Alpha, [1]); // scope 1 at $DIR/offset_of.rs:+2:13: +2:33 ++ _2 = const 0_usize; // scope 1 at $DIR/offset_of.rs:+2:13: +2:33 StorageLive(_3); // scope 2 at $DIR/offset_of.rs:+3:9: +3:11 -- _3 = OffsetOf([2, 0])(Foo); // scope 2 at $DIR/offset_of.rs:+3:14: +3:34 -+ _3 = const 4_usize; // scope 2 at $DIR/offset_of.rs:+3:14: +3:34 +- _3 = OffsetOf(Alpha, [2, 0]); // scope 2 at $DIR/offset_of.rs:+3:14: +3:36 ++ _3 = const 2_usize; // scope 2 at $DIR/offset_of.rs:+3:14: +3:36 StorageLive(_4); // scope 3 at $DIR/offset_of.rs:+4:9: +4:11 -- _4 = OffsetOf([2, 1])(Foo); // scope 3 at $DIR/offset_of.rs:+4:14: +4:34 -+ _4 = const 5_usize; // scope 3 at $DIR/offset_of.rs:+4:14: +4:34 +- _4 = OffsetOf(Alpha, [2, 1]); // scope 3 at $DIR/offset_of.rs:+4:14: +4:36 ++ _4 = const 3_usize; // scope 3 at $DIR/offset_of.rs:+4:14: +4:36 StorageDead(_4); // scope 3 at $DIR/offset_of.rs:+5:1: +5:2 StorageDead(_3); // scope 2 at $DIR/offset_of.rs:+5:1: +5:2 StorageDead(_2); // scope 1 at $DIR/offset_of.rs:+5:1: +5:2 diff --git a/tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff b/tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff new file mode 100644 index 00000000000..5c6cb47089e --- /dev/null +++ b/tests/mir-opt/const_prop/offset_of.generic.ConstProp.diff @@ -0,0 +1,39 @@ +- // MIR for `generic` before ConstProp ++ // MIR for `generic` after ConstProp + + fn generic() -> () { + let mut _0: (); // return place in scope 0 at $DIR/offset_of.rs:+0:17: +0:17 + let _1: usize; // in scope 0 at $DIR/offset_of.rs:+1:9: +1:11 + scope 1 { + debug gx => _1; // in scope 1 at $DIR/offset_of.rs:+1:9: +1:11 + let _2: usize; // in scope 1 at $DIR/offset_of.rs:+2:9: +2:11 + scope 2 { + debug gy => _2; // in scope 2 at $DIR/offset_of.rs:+2:9: +2:11 + let _3: usize; // in scope 2 at $DIR/offset_of.rs:+3:9: +3:11 + scope 3 { + debug dx => _3; // in scope 3 at $DIR/offset_of.rs:+3:9: +3:11 + let _4: usize; // in scope 3 at $DIR/offset_of.rs:+4:9: +4:11 + scope 4 { + debug dy => _4; // in scope 4 at $DIR/offset_of.rs:+4:9: +4:11 + } + } + } + } + + bb0: { + StorageLive(_1); // scope 0 at $DIR/offset_of.rs:+1:9: +1:11 + _1 = OffsetOf(Gamma, [0]); // scope 0 at $DIR/offset_of.rs:+1:14: +1:37 + StorageLive(_2); // scope 1 at $DIR/offset_of.rs:+2:9: +2:11 + _2 = OffsetOf(Gamma, [1]); // scope 1 at $DIR/offset_of.rs:+2:14: +2:37 + StorageLive(_3); // scope 2 at $DIR/offset_of.rs:+3:9: +3:11 + _3 = OffsetOf(Delta, [1]); // scope 2 at $DIR/offset_of.rs:+3:14: +3:37 + StorageLive(_4); // scope 3 at $DIR/offset_of.rs:+4:9: +4:11 + _4 = OffsetOf(Delta, [2]); // scope 3 at $DIR/offset_of.rs:+4:14: +4:37 + StorageDead(_4); // scope 3 at $DIR/offset_of.rs:+5:1: +5:2 + StorageDead(_3); // scope 2 at $DIR/offset_of.rs:+5:1: +5:2 + StorageDead(_2); // scope 1 at $DIR/offset_of.rs:+5:1: +5:2 + StorageDead(_1); // scope 0 at $DIR/offset_of.rs:+5:1: +5:2 + return; // scope 0 at $DIR/offset_of.rs:+5:2: +5:2 + } + } + diff --git a/tests/mir-opt/const_prop/offset_of.rs b/tests/mir-opt/const_prop/offset_of.rs index 54e19890c36..eabdf848079 100644 --- a/tests/mir-opt/const_prop/offset_of.rs +++ b/tests/mir-opt/const_prop/offset_of.rs @@ -1,25 +1,49 @@ // unit-test // compile-flags: -O -// EMIT_MIR offset_of.main.ConstProp.diff - #![feature(offset_of)] +use std::marker::PhantomData; use std::mem::offset_of; -#[repr(C)] -struct Foo { +struct Alpha { x: u8, y: u16, - z: Bar, + z: Beta, +} + +struct Beta(u8, u8); + +struct Gamma { + x: u8, + y: u16, + _t: T, } #[repr(C)] -struct Bar(u8, u8); +struct Delta { + _phantom: PhantomData, + x: u8, + y: u16, +} + +// EMIT_MIR offset_of.concrete.ConstProp.diff +fn concrete() { + let x = offset_of!(Alpha, x); + let y = offset_of!(Alpha, y); + let z0 = offset_of!(Alpha, z.0); + let z1 = offset_of!(Alpha, z.1); +} + +// EMIT_MIR offset_of.generic.ConstProp.diff +fn generic() { + let gx = offset_of!(Gamma, x); + let gy = offset_of!(Gamma, y); + let dx = offset_of!(Delta, x); + let dy = offset_of!(Delta, y); +} fn main() { - let x = offset_of!(Foo, x); - let y = offset_of!(Foo, y); - let z0 = offset_of!(Foo, z.0); - let z1 = offset_of!(Foo, z.1); + concrete(); + generic::<()>(); } diff --git a/tests/ui/liveness/liveness-offset-of.rs b/tests/ui/liveness/liveness-offset-of.rs index 461f546f3ee..da91de3862f 100644 --- a/tests/ui/liveness/liveness-offset-of.rs +++ b/tests/ui/liveness/liveness-offset-of.rs @@ -19,8 +19,26 @@ struct Gamma { b: (), } +struct Delta { + a: (), + b: (), //~ ERROR field `b` is never read +} + +trait Trait { + type Assoc; +} +impl Trait for () { + type Assoc = Delta; +} + +struct Project { + a: u8, //~ ERROR field `a` is never read + b: ::Assoc, +} + fn main() { offset_of!(Alpha, a); offset_of!(Alpha, c.b); offset_of!((Gamma,), 0.b); + offset_of!(Project::<()>, b.a); } diff --git a/tests/ui/liveness/liveness-offset-of.stderr b/tests/ui/liveness/liveness-offset-of.stderr index 32d13c8ed2c..afc4c590eeb 100644 --- a/tests/ui/liveness/liveness-offset-of.stderr +++ b/tests/ui/liveness/liveness-offset-of.stderr @@ -29,5 +29,22 @@ LL | struct Gamma { LL | a: (), | ^ -error: aborting due to 3 previous errors +error: field `b` is never read + --> $DIR/liveness-offset-of.rs:24:5 + | +LL | struct Delta { + | ----- field in this struct +LL | a: (), +LL | b: (), + | ^ + +error: field `a` is never read + --> $DIR/liveness-offset-of.rs:35:5 + | +LL | struct Project { + | ------- field in this struct +LL | a: u8, + | ^ + +error: aborting due to 5 previous errors