Skip to content

Commit a642563

Browse files
committed
major test improvements
1 parent 3206960 commit a642563

10 files changed

+349
-24
lines changed

library/core/tests/mem.rs

+109-14
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,115 @@ fn offset_of() {
388388
assert!(offset_of!((u8, u16), 1) <= size_of::<(u8, u16)>() - 2);
389389
}
390390

391+
#[test]
392+
#[cfg(not(bootstrap))]
393+
fn offset_of_union() {
394+
#[repr(C)]
395+
union Foo {
396+
x: u8,
397+
y: u16,
398+
z: Bar,
399+
}
400+
401+
#[repr(C)]
402+
#[derive(Copy, Clone)]
403+
struct Bar(u8, u8);
404+
405+
assert_eq!(offset_of!(Foo, x), 0);
406+
assert_eq!(offset_of!(Foo, y), 0);
407+
assert_eq!(offset_of!(Foo, z.0), 0);
408+
assert_eq!(offset_of!(Foo, z.1), 1);
409+
}
410+
411+
#[test]
412+
#[cfg(not(bootstrap))]
413+
fn offset_of_dst() {
414+
#[repr(C)]
415+
struct Alpha {
416+
x: u8,
417+
y: u16,
418+
z: [u8],
419+
}
420+
421+
trait Trait {}
422+
423+
#[repr(C)]
424+
struct Beta {
425+
x: u8,
426+
y: u16,
427+
z: dyn Trait,
428+
}
429+
430+
extern "C" {
431+
type Extern;
432+
}
433+
434+
#[repr(C)]
435+
struct Gamma {
436+
x: u8,
437+
y: u16,
438+
z: Extern,
439+
}
440+
441+
assert_eq!(offset_of!(Alpha, x), 0);
442+
assert_eq!(offset_of!(Alpha, y), 2);
443+
444+
assert_eq!(offset_of!(Beta, x), 0);
445+
assert_eq!(offset_of!(Beta, y), 2);
446+
447+
assert_eq!(offset_of!(Gamma, x), 0);
448+
assert_eq!(offset_of!(Gamma, y), 2);
449+
}
450+
451+
#[test]
452+
#[cfg(not(bootstrap))]
453+
fn offset_of_packed() {
454+
#[repr(C, packed)]
455+
struct Foo {
456+
x: u8,
457+
y: u16,
458+
}
459+
460+
assert_eq!(offset_of!(Foo, x), 0);
461+
assert_eq!(offset_of!(Foo, y), 1);
462+
}
463+
464+
#[test]
465+
#[cfg(not(bootstrap))]
466+
fn offset_of_projection() {
467+
#[repr(C)]
468+
struct Foo {
469+
x: u8,
470+
y: u16,
471+
}
472+
473+
trait Projector {
474+
type Type;
475+
}
476+
477+
impl Projector for () {
478+
type Type = Foo;
479+
}
480+
481+
assert_eq!(offset_of!(<() as Projector>::Type, x), 0);
482+
assert_eq!(offset_of!(<() as Projector>::Type, y), 2);
483+
}
484+
485+
#[test]
486+
#[cfg(not(bootstrap))]
487+
fn offset_of_alias() {
488+
#[repr(C)]
489+
struct Foo {
490+
x: u8,
491+
y: u16,
492+
}
493+
494+
type Bar = Foo;
495+
496+
assert_eq!(offset_of!(Bar, x), 0);
497+
assert_eq!(offset_of!(Bar, y), 2);
498+
}
499+
391500
#[test]
392501
#[cfg(not(bootstrap))]
393502
fn const_offset_of() {
@@ -425,20 +534,6 @@ fn offset_of_without_const_promotion() {
425534
inner::<()>();
426535
}
427536

428-
#[test]
429-
#[cfg(not(bootstrap))]
430-
fn offset_of_dst() {
431-
#[repr(C)]
432-
struct Foo {
433-
x: u8,
434-
y: u16,
435-
slice: [u8],
436-
}
437-
438-
assert_eq!(offset_of!(Foo, x), 0);
439-
assert_eq!(offset_of!(Foo, y), 2);
440-
}
441-
442537
#[test]
443538
#[cfg(not(bootstrap))]
444539
fn offset_of_addr() {

tests/ui/lint/dead-code/offset-of-correct-param-env.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
// But importantly, the normalization ends up with T, which, as we've declared in our param
3434
// env is MyFieldDead. When we're in the param env of the `a` field, the where bound above
3535
// is not in scope, so we don't know what T is - it's generic.
36-
// We cannot access a field on T. Boom!
36+
// If we use the wrong param env, the lint will ICE.
3737
std::mem::offset_of!(A<GenericIsEqual<T>>, a.not_dead)
3838
}
3939

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![crate_type = "lib"]
2+
#![feature(staged_api)]
3+
#![stable(feature = "stable_test_feature", since = "1.0")]
4+
5+
#[unstable(feature = "unstable_test_feature", issue = "none")]
6+
pub struct Unstable {
7+
#[unstable(feature = "unstable_test_feature", issue = "none")]
8+
pub unstable: u8,
9+
}
10+
11+
#[stable(feature = "stable_test_feature", since = "1.0")]
12+
pub struct Stable {
13+
#[stable(feature = "stable_test_feature", since = "1.0")]
14+
pub stable: u8,
15+
}
16+
17+
#[stable(feature = "stable_test_feature", since = "1.0")]
18+
pub struct StableWithUnstableField {
19+
#[unstable(feature = "unstable_test_feature", issue = "none")]
20+
pub unstable: u8,
21+
}
22+
23+
#[stable(feature = "stable_test_feature", since = "1.0")]
24+
pub struct StableWithUnstableFieldType {
25+
#[stable(feature = "stable_test_feature", since = "1.0")]
26+
pub stable: Unstable,
27+
}
28+
29+
#[unstable(feature = "unstable_test_feature", issue = "none")]
30+
pub struct UnstableWithStableFieldType {
31+
#[unstable(feature = "unstable_test_feature", issue = "none")]
32+
pub unstable: Stable,
33+
}
+24-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
1-
#![feature(offset_of)]
1+
#![feature(offset_of, extern_types)]
22

33
use std::mem::offset_of;
44

5-
#[repr(C)]
6-
struct Foo {
5+
struct Alpha {
76
x: u8,
87
y: u16,
9-
slice: [u8],
8+
z: [u8],
9+
}
10+
11+
trait Trait {}
12+
13+
struct Beta {
14+
x: u8,
15+
y: u16,
16+
z: dyn Trait,
17+
}
18+
19+
extern {
20+
type Extern;
21+
}
22+
23+
struct Gamma {
24+
x: u8,
25+
y: u16,
26+
z: Extern,
1027
}
1128

1229
fn main() {
13-
offset_of!(Foo, slice); //~ ERROR the size for values of type
30+
offset_of!(Alpha, z); //~ ERROR the size for values of type
31+
offset_of!(Beta, z); //~ ERROR the size for values of type
32+
offset_of!(Gamma, z); //~ ERROR the size for values of type
1433
}
+20-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2-
--> $DIR/offset-of-dst-field.rs:13:5
2+
--> $DIR/offset-of-dst-field.rs:30:5
33
|
4-
LL | offset_of!(Foo, slice);
5-
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
4+
LL | offset_of!(Alpha, z);
5+
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[u8]`
88

9-
error: aborting due to previous error
9+
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
10+
--> $DIR/offset-of-dst-field.rs:31:5
11+
|
12+
LL | offset_of!(Beta, z);
13+
| ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
14+
|
15+
= help: the trait `Sized` is not implemented for `(dyn Trait + 'static)`
16+
17+
error[E0277]: the size for values of type `Extern` cannot be known at compilation time
18+
--> $DIR/offset-of-dst-field.rs:32:5
19+
|
20+
LL | offset_of!(Gamma, z);
21+
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
22+
|
23+
= help: the trait `Sized` is not implemented for `Extern`
24+
25+
error: aborting due to 3 previous errors
1026

1127
For more information about this error, try `rustc --explain E0277`.

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(offset_of)]
2+
3+
use std::mem::offset_of;
4+
5+
enum Alpha {
6+
One(u8),
7+
Two(u8),
8+
}
9+
10+
fn main() {
11+
offset_of!(Alpha::One, 0); //~ ERROR expected type, found variant `Alpha::One`
12+
offset_of!(Alpha, Two.0); //~ ERROR no field `Two` on type `Alpha`
13+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0573]: expected type, found variant `Alpha::One`
2+
--> $DIR/offset-of-enum.rs:11:16
3+
|
4+
LL | offset_of!(Alpha::One, 0);
5+
| ^^^^^^^^^^
6+
| |
7+
| not a type
8+
| help: try using the variant's enum: `Alpha`
9+
10+
error[E0609]: no field `Two` on type `Alpha`
11+
--> $DIR/offset-of-enum.rs:12:23
12+
|
13+
LL | offset_of!(Alpha, Two.0);
14+
| ^^^
15+
16+
error: aborting due to 2 previous errors
17+
18+
Some errors have detailed explanations: E0573, E0609.
19+
For more information about an error, try `rustc --explain E0573`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// check-pass
2+
// aux-build:offset-of-staged-api.rs
3+
4+
#![feature(offset_of, unstable_test_feature)]
5+
6+
use std::mem::offset_of;
7+
8+
extern crate offset_of_staged_api;
9+
10+
use offset_of_staged_api::*;
11+
12+
fn main() {
13+
offset_of!(Unstable, unstable);
14+
offset_of!(Stable, stable);
15+
offset_of!(StableWithUnstableField, unstable);
16+
offset_of!(StableWithUnstableFieldType, stable);
17+
offset_of!(StableWithUnstableFieldType, stable.unstable);
18+
offset_of!(UnstableWithStableFieldType, unstable);
19+
offset_of!(UnstableWithStableFieldType, unstable.stable);
20+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// aux-build:offset-of-staged-api.rs
2+
3+
#![feature(offset_of)]
4+
5+
use std::mem::offset_of;
6+
7+
extern crate offset_of_staged_api;
8+
9+
use offset_of_staged_api::*;
10+
11+
fn main() {
12+
offset_of!(
13+
//~^ ERROR use of unstable library feature
14+
Unstable, //~ ERROR use of unstable library feature
15+
unstable
16+
);
17+
offset_of!(Stable, stable);
18+
offset_of!(StableWithUnstableField, unstable); //~ ERROR use of unstable library feature
19+
offset_of!(StableWithUnstableFieldType, stable);
20+
offset_of!(StableWithUnstableFieldType, stable.unstable); //~ ERROR use of unstable library feature
21+
offset_of!(
22+
//~^ ERROR use of unstable library feature
23+
UnstableWithStableFieldType, //~ ERROR use of unstable library feature
24+
unstable
25+
);
26+
offset_of!(
27+
//~^ ERROR use of unstable library feature
28+
UnstableWithStableFieldType, //~ ERROR use of unstable library feature
29+
unstable.stable
30+
);
31+
}

0 commit comments

Comments
 (0)