Skip to content

Commit b95852b

Browse files
committed
test improvements
1 parent 61f23e0 commit b95852b

File tree

7 files changed

+147
-24
lines changed

7 files changed

+147
-24
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2048,7 +2048,11 @@ impl<'tcx> Debug for Rvalue<'tcx> {
20482048
}
20492049
UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
20502050
Discriminant(ref place) => write!(fmt, "discriminant({:?})", place),
2051-
NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
2051+
NullaryOp(ref op, ref t) => match op {
2052+
NullOp::SizeOf => write!(fmt, "SizeOf({:?})", t),
2053+
NullOp::AlignOf => write!(fmt, "AlignOf({:?})", t),
2054+
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({:?}, {:?})", t, fields),
2055+
},
20522056
ThreadLocalRef(did) => ty::tls::with(|tcx| {
20532057
let muta = tcx.static_mutability(did).unwrap().prefix_str();
20542058
write!(fmt, "&/*tls*/ {}{}", muta, tcx.def_path_str(did))

library/core/tests/mem.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,24 @@ fn offset_of_dst() {
438438
assert_eq!(offset_of!(Foo, x), 0);
439439
assert_eq!(offset_of!(Foo, y), 2);
440440
}
441+
442+
#[test]
443+
#[cfg(not(bootstrap))]
444+
fn offset_of_addr() {
445+
#[repr(C)]
446+
struct Foo {
447+
x: u8,
448+
y: u16,
449+
z: Bar,
450+
}
451+
452+
#[repr(C)]
453+
struct Bar(u8, u8);
454+
455+
let base = Foo { x: 0, y: 0, z: Bar(0, 0) };
456+
457+
assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, x), ptr::addr_of!(base.x).addr());
458+
assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, y), ptr::addr_of!(base.y).addr());
459+
assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, z.0), ptr::addr_of!(base.z.0).addr());
460+
assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, z.1), ptr::addr_of!(base.z.1).addr());
461+
}

tests/mir-opt/const_prop/offset_of.main.ConstProp.diff renamed to tests/mir-opt/const_prop/offset_of.concrete.ConstProp.diff

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
- // MIR for `main` before ConstProp
2-
+ // MIR for `main` after ConstProp
1+
- // MIR for `concrete` before ConstProp
2+
+ // MIR for `concrete` after ConstProp
33

4-
fn main() -> () {
5-
let mut _0: (); // return place in scope 0 at $DIR/offset_of.rs:+0:11: +0:11
4+
fn concrete() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/offset_of.rs:+0:15: +0:15
66
let _1: usize; // in scope 0 at $DIR/offset_of.rs:+1:9: +1:10
77
scope 1 {
88
debug x => _1; // in scope 1 at $DIR/offset_of.rs:+1:9: +1:10
@@ -22,17 +22,17 @@
2222

2323
bb0: {
2424
StorageLive(_1); // scope 0 at $DIR/offset_of.rs:+1:9: +1:10
25-
- _1 = OffsetOf([0])(Foo); // scope 0 at $DIR/offset_of.rs:+1:13: +1:31
26-
+ _1 = const 0_usize; // scope 0 at $DIR/offset_of.rs:+1:13: +1:31
25+
- _1 = OffsetOf(Alpha, [0]); // scope 0 at $DIR/offset_of.rs:+1:13: +1:33
26+
+ _1 = const 4_usize; // scope 0 at $DIR/offset_of.rs:+1:13: +1:33
2727
StorageLive(_2); // scope 1 at $DIR/offset_of.rs:+2:9: +2:10
28-
- _2 = OffsetOf([1])(Foo); // scope 1 at $DIR/offset_of.rs:+2:13: +2:31
29-
+ _2 = const 2_usize; // scope 1 at $DIR/offset_of.rs:+2:13: +2:31
28+
- _2 = OffsetOf(Alpha, [1]); // scope 1 at $DIR/offset_of.rs:+2:13: +2:33
29+
+ _2 = const 0_usize; // scope 1 at $DIR/offset_of.rs:+2:13: +2:33
3030
StorageLive(_3); // scope 2 at $DIR/offset_of.rs:+3:9: +3:11
31-
- _3 = OffsetOf([2, 0])(Foo); // scope 2 at $DIR/offset_of.rs:+3:14: +3:34
32-
+ _3 = const 4_usize; // scope 2 at $DIR/offset_of.rs:+3:14: +3:34
31+
- _3 = OffsetOf(Alpha, [2, 0]); // scope 2 at $DIR/offset_of.rs:+3:14: +3:36
32+
+ _3 = const 2_usize; // scope 2 at $DIR/offset_of.rs:+3:14: +3:36
3333
StorageLive(_4); // scope 3 at $DIR/offset_of.rs:+4:9: +4:11
34-
- _4 = OffsetOf([2, 1])(Foo); // scope 3 at $DIR/offset_of.rs:+4:14: +4:34
35-
+ _4 = const 5_usize; // scope 3 at $DIR/offset_of.rs:+4:14: +4:34
34+
- _4 = OffsetOf(Alpha, [2, 1]); // scope 3 at $DIR/offset_of.rs:+4:14: +4:36
35+
+ _4 = const 3_usize; // scope 3 at $DIR/offset_of.rs:+4:14: +4:36
3636
StorageDead(_4); // scope 3 at $DIR/offset_of.rs:+5:1: +5:2
3737
StorageDead(_3); // scope 2 at $DIR/offset_of.rs:+5:1: +5:2
3838
StorageDead(_2); // scope 1 at $DIR/offset_of.rs:+5:1: +5:2
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
- // MIR for `generic` before ConstProp
2+
+ // MIR for `generic` after ConstProp
3+
4+
fn generic() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/offset_of.rs:+0:17: +0:17
6+
let _1: usize; // in scope 0 at $DIR/offset_of.rs:+1:9: +1:11
7+
scope 1 {
8+
debug gx => _1; // in scope 1 at $DIR/offset_of.rs:+1:9: +1:11
9+
let _2: usize; // in scope 1 at $DIR/offset_of.rs:+2:9: +2:11
10+
scope 2 {
11+
debug gy => _2; // in scope 2 at $DIR/offset_of.rs:+2:9: +2:11
12+
let _3: usize; // in scope 2 at $DIR/offset_of.rs:+3:9: +3:11
13+
scope 3 {
14+
debug dx => _3; // in scope 3 at $DIR/offset_of.rs:+3:9: +3:11
15+
let _4: usize; // in scope 3 at $DIR/offset_of.rs:+4:9: +4:11
16+
scope 4 {
17+
debug dy => _4; // in scope 4 at $DIR/offset_of.rs:+4:9: +4:11
18+
}
19+
}
20+
}
21+
}
22+
23+
bb0: {
24+
StorageLive(_1); // scope 0 at $DIR/offset_of.rs:+1:9: +1:11
25+
_1 = OffsetOf(Gamma<T>, [0]); // scope 0 at $DIR/offset_of.rs:+1:14: +1:37
26+
StorageLive(_2); // scope 1 at $DIR/offset_of.rs:+2:9: +2:11
27+
_2 = OffsetOf(Gamma<T>, [1]); // scope 1 at $DIR/offset_of.rs:+2:14: +2:37
28+
StorageLive(_3); // scope 2 at $DIR/offset_of.rs:+3:9: +3:11
29+
_3 = OffsetOf(Delta<T>, [1]); // scope 2 at $DIR/offset_of.rs:+3:14: +3:37
30+
StorageLive(_4); // scope 3 at $DIR/offset_of.rs:+4:9: +4:11
31+
_4 = OffsetOf(Delta<T>, [2]); // scope 3 at $DIR/offset_of.rs:+4:14: +4:37
32+
StorageDead(_4); // scope 3 at $DIR/offset_of.rs:+5:1: +5:2
33+
StorageDead(_3); // scope 2 at $DIR/offset_of.rs:+5:1: +5:2
34+
StorageDead(_2); // scope 1 at $DIR/offset_of.rs:+5:1: +5:2
35+
StorageDead(_1); // scope 0 at $DIR/offset_of.rs:+5:1: +5:2
36+
return; // scope 0 at $DIR/offset_of.rs:+5:2: +5:2
37+
}
38+
}
39+

tests/mir-opt/const_prop/offset_of.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
11
// unit-test
22
// compile-flags: -O
33

4-
// EMIT_MIR offset_of.main.ConstProp.diff
5-
64
#![feature(offset_of)]
75

6+
use std::marker::PhantomData;
87
use std::mem::offset_of;
98

10-
#[repr(C)]
11-
struct Foo {
9+
struct Alpha {
10+
x: u8,
11+
y: u16,
12+
z: Beta,
13+
}
14+
15+
struct Beta(u8, u8);
16+
17+
struct Gamma<T> {
1218
x: u8,
1319
y: u16,
14-
z: Bar,
20+
_t: T,
1521
}
1622

1723
#[repr(C)]
18-
struct Bar(u8, u8);
24+
struct Delta<T> {
25+
_phantom: PhantomData<T>,
26+
x: u8,
27+
y: u16,
28+
}
29+
30+
// EMIT_MIR offset_of.concrete.ConstProp.diff
31+
fn concrete() {
32+
let x = offset_of!(Alpha, x);
33+
let y = offset_of!(Alpha, y);
34+
let z0 = offset_of!(Alpha, z.0);
35+
let z1 = offset_of!(Alpha, z.1);
36+
}
37+
38+
// EMIT_MIR offset_of.generic.ConstProp.diff
39+
fn generic<T>() {
40+
let gx = offset_of!(Gamma<T>, x);
41+
let gy = offset_of!(Gamma<T>, y);
42+
let dx = offset_of!(Delta<T>, x);
43+
let dy = offset_of!(Delta<T>, y);
44+
}
1945

2046
fn main() {
21-
let x = offset_of!(Foo, x);
22-
let y = offset_of!(Foo, y);
23-
let z0 = offset_of!(Foo, z.0);
24-
let z1 = offset_of!(Foo, z.1);
47+
concrete();
48+
generic::<()>();
2549
}

tests/ui/liveness/liveness-offset-of.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,26 @@ struct Gamma {
1919
b: (),
2020
}
2121

22+
struct Delta {
23+
a: (),
24+
b: (), //~ ERROR field `b` is never read
25+
}
26+
27+
trait Trait {
28+
type Assoc;
29+
}
30+
impl Trait for () {
31+
type Assoc = Delta;
32+
}
33+
34+
struct Project<T: Trait> {
35+
a: u8, //~ ERROR field `a` is never read
36+
b: <T as Trait>::Assoc,
37+
}
38+
2239
fn main() {
2340
offset_of!(Alpha, a);
2441
offset_of!(Alpha, c.b);
2542
offset_of!((Gamma,), 0.b);
43+
offset_of!(Project::<()>, b.a);
2644
}

tests/ui/liveness/liveness-offset-of.stderr

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,22 @@ LL | struct Gamma {
2929
LL | a: (),
3030
| ^
3131

32-
error: aborting due to 3 previous errors
32+
error: field `b` is never read
33+
--> $DIR/liveness-offset-of.rs:24:5
34+
|
35+
LL | struct Delta {
36+
| ----- field in this struct
37+
LL | a: (),
38+
LL | b: (),
39+
| ^
40+
41+
error: field `a` is never read
42+
--> $DIR/liveness-offset-of.rs:35:5
43+
|
44+
LL | struct Project<T: Trait> {
45+
| ------- field in this struct
46+
LL | a: u8,
47+
| ^
48+
49+
error: aborting due to 5 previous errors
3350

0 commit comments

Comments
 (0)