Skip to content

Commit

Permalink
major test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
beepster4096 committed Apr 21, 2023
1 parent 3206960 commit a642563
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 24 deletions.
123 changes: 109 additions & 14 deletions library/core/tests/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,115 @@ fn offset_of() {
assert!(offset_of!((u8, u16), 1) <= size_of::<(u8, u16)>() - 2);
}

#[test]
#[cfg(not(bootstrap))]
fn offset_of_union() {
#[repr(C)]
union Foo {
x: u8,
y: u16,
z: Bar,
}

#[repr(C)]
#[derive(Copy, Clone)]
struct Bar(u8, u8);

assert_eq!(offset_of!(Foo, x), 0);
assert_eq!(offset_of!(Foo, y), 0);
assert_eq!(offset_of!(Foo, z.0), 0);
assert_eq!(offset_of!(Foo, z.1), 1);
}

#[test]
#[cfg(not(bootstrap))]
fn offset_of_dst() {
#[repr(C)]
struct Alpha {
x: u8,
y: u16,
z: [u8],
}

trait Trait {}

#[repr(C)]
struct Beta {
x: u8,
y: u16,
z: dyn Trait,
}

extern "C" {
type Extern;
}

#[repr(C)]
struct Gamma {
x: u8,
y: u16,
z: Extern,
}

assert_eq!(offset_of!(Alpha, x), 0);
assert_eq!(offset_of!(Alpha, y), 2);

assert_eq!(offset_of!(Beta, x), 0);
assert_eq!(offset_of!(Beta, y), 2);

assert_eq!(offset_of!(Gamma, x), 0);
assert_eq!(offset_of!(Gamma, y), 2);
}

#[test]
#[cfg(not(bootstrap))]
fn offset_of_packed() {
#[repr(C, packed)]
struct Foo {
x: u8,
y: u16,
}

assert_eq!(offset_of!(Foo, x), 0);
assert_eq!(offset_of!(Foo, y), 1);
}

#[test]
#[cfg(not(bootstrap))]
fn offset_of_projection() {
#[repr(C)]
struct Foo {
x: u8,
y: u16,
}

trait Projector {
type Type;
}

impl Projector for () {
type Type = Foo;
}

assert_eq!(offset_of!(<() as Projector>::Type, x), 0);
assert_eq!(offset_of!(<() as Projector>::Type, y), 2);
}

#[test]
#[cfg(not(bootstrap))]
fn offset_of_alias() {
#[repr(C)]
struct Foo {
x: u8,
y: u16,
}

type Bar = Foo;

assert_eq!(offset_of!(Bar, x), 0);
assert_eq!(offset_of!(Bar, y), 2);
}

#[test]
#[cfg(not(bootstrap))]
fn const_offset_of() {
Expand Down Expand Up @@ -425,20 +534,6 @@ fn offset_of_without_const_promotion() {
inner::<()>();
}

#[test]
#[cfg(not(bootstrap))]
fn offset_of_dst() {
#[repr(C)]
struct Foo {
x: u8,
y: u16,
slice: [u8],
}

assert_eq!(offset_of!(Foo, x), 0);
assert_eq!(offset_of!(Foo, y), 2);
}

#[test]
#[cfg(not(bootstrap))]
fn offset_of_addr() {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/dead-code/offset-of-correct-param-env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
// But importantly, the normalization ends up with T, which, as we've declared in our param
// env is MyFieldDead. When we're in the param env of the `a` field, the where bound above
// is not in scope, so we don't know what T is - it's generic.
// We cannot access a field on T. Boom!
// If we use the wrong param env, the lint will ICE.
std::mem::offset_of!(A<GenericIsEqual<T>>, a.not_dead)
}

Expand Down
33 changes: 33 additions & 0 deletions tests/ui/offset-of/auxiliary/offset-of-staged-api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![crate_type = "lib"]
#![feature(staged_api)]
#![stable(feature = "stable_test_feature", since = "1.0")]

#[unstable(feature = "unstable_test_feature", issue = "none")]
pub struct Unstable {
#[unstable(feature = "unstable_test_feature", issue = "none")]
pub unstable: u8,
}

#[stable(feature = "stable_test_feature", since = "1.0")]
pub struct Stable {
#[stable(feature = "stable_test_feature", since = "1.0")]
pub stable: u8,
}

#[stable(feature = "stable_test_feature", since = "1.0")]
pub struct StableWithUnstableField {
#[unstable(feature = "unstable_test_feature", issue = "none")]
pub unstable: u8,
}

#[stable(feature = "stable_test_feature", since = "1.0")]
pub struct StableWithUnstableFieldType {
#[stable(feature = "stable_test_feature", since = "1.0")]
pub stable: Unstable,
}

#[unstable(feature = "unstable_test_feature", issue = "none")]
pub struct UnstableWithStableFieldType {
#[unstable(feature = "unstable_test_feature", issue = "none")]
pub unstable: Stable,
}
29 changes: 24 additions & 5 deletions tests/ui/offset-of/offset-of-dst-field.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
#![feature(offset_of)]
#![feature(offset_of, extern_types)]

use std::mem::offset_of;

#[repr(C)]
struct Foo {
struct Alpha {
x: u8,
y: u16,
slice: [u8],
z: [u8],
}

trait Trait {}

struct Beta {
x: u8,
y: u16,
z: dyn Trait,
}

extern {
type Extern;
}

struct Gamma {
x: u8,
y: u16,
z: Extern,
}

fn main() {
offset_of!(Foo, slice); //~ ERROR the size for values of type
offset_of!(Alpha, z); //~ ERROR the size for values of type
offset_of!(Beta, z); //~ ERROR the size for values of type
offset_of!(Gamma, z); //~ ERROR the size for values of type
}
24 changes: 20 additions & 4 deletions tests/ui/offset-of/offset-of-dst-field.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:13:5
--> $DIR/offset-of-dst-field.rs:30:5
|
LL | offset_of!(Foo, slice);
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
LL | offset_of!(Alpha, z);
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`

error: aborting due to previous error
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:31:5
|
LL | offset_of!(Beta, z);
| ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Trait + 'static)`

error[E0277]: the size for values of type `Extern` cannot be known at compilation time
--> $DIR/offset-of-dst-field.rs:32:5
|
LL | offset_of!(Gamma, z);
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `Extern`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0277`.
13 changes: 13 additions & 0 deletions tests/ui/offset-of/offset-of-enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(offset_of)]

use std::mem::offset_of;

enum Alpha {
One(u8),
Two(u8),
}

fn main() {
offset_of!(Alpha::One, 0); //~ ERROR expected type, found variant `Alpha::One`
offset_of!(Alpha, Two.0); //~ ERROR no field `Two` on type `Alpha`
}
19 changes: 19 additions & 0 deletions tests/ui/offset-of/offset-of-enum.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0573]: expected type, found variant `Alpha::One`
--> $DIR/offset-of-enum.rs:11:16
|
LL | offset_of!(Alpha::One, 0);
| ^^^^^^^^^^
| |
| not a type
| help: try using the variant's enum: `Alpha`

error[E0609]: no field `Two` on type `Alpha`
--> $DIR/offset-of-enum.rs:12:23
|
LL | offset_of!(Alpha, Two.0);
| ^^^

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0573, E0609.
For more information about an error, try `rustc --explain E0573`.
20 changes: 20 additions & 0 deletions tests/ui/offset-of/offset-of-unstable-with-feature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// check-pass
// aux-build:offset-of-staged-api.rs

#![feature(offset_of, unstable_test_feature)]

use std::mem::offset_of;

extern crate offset_of_staged_api;

use offset_of_staged_api::*;

fn main() {
offset_of!(Unstable, unstable);
offset_of!(Stable, stable);
offset_of!(StableWithUnstableField, unstable);
offset_of!(StableWithUnstableFieldType, stable);
offset_of!(StableWithUnstableFieldType, stable.unstable);
offset_of!(UnstableWithStableFieldType, unstable);
offset_of!(UnstableWithStableFieldType, unstable.stable);
}
31 changes: 31 additions & 0 deletions tests/ui/offset-of/offset-of-unstable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// aux-build:offset-of-staged-api.rs

#![feature(offset_of)]

use std::mem::offset_of;

extern crate offset_of_staged_api;

use offset_of_staged_api::*;

fn main() {
offset_of!(
//~^ ERROR use of unstable library feature
Unstable, //~ ERROR use of unstable library feature
unstable
);
offset_of!(Stable, stable);
offset_of!(StableWithUnstableField, unstable); //~ ERROR use of unstable library feature
offset_of!(StableWithUnstableFieldType, stable);
offset_of!(StableWithUnstableFieldType, stable.unstable); //~ ERROR use of unstable library feature
offset_of!(
//~^ ERROR use of unstable library feature
UnstableWithStableFieldType, //~ ERROR use of unstable library feature
unstable
);
offset_of!(
//~^ ERROR use of unstable library feature
UnstableWithStableFieldType, //~ ERROR use of unstable library feature
unstable.stable
);
}
Loading

0 comments on commit a642563

Please sign in to comment.