rust/tests/ui/repr/repr-transparent.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
2.4 KiB
Rust
Raw Normal View History

2018-01-03 10:43:30 -06:00
// This file tests repr(transparent)-related errors reported during typeck. Other errors
// that are reported earlier and therefore preempt these are tested in:
// - repr-transparent-other-reprs.rs
// - repr-transparent-other-items.rs
2020-01-10 17:53:54 -06:00
#![feature(transparent_unions)]
2018-01-03 10:43:30 -06:00
use std::marker::PhantomData;
#[repr(transparent)]
struct NoFields;
2018-01-03 10:43:30 -06:00
#[repr(transparent)]
struct ContainsOnlyZst(());
2018-01-03 10:43:30 -06:00
#[repr(transparent)]
struct ContainsOnlyZstArray([bool; 0]);
2018-01-03 10:43:30 -06:00
#[repr(transparent)]
struct ContainsMultipleZst(PhantomData<*const i32>, NoFields);
#[repr(transparent)]
struct ContainsZstAndNonZst((), [i32; 2]);
#[repr(transparent)]
struct MultipleNonZst(u8, u8); //~ ERROR needs at most one field with non-trivial size or alignment
2018-01-03 10:43:30 -06:00
trait Mirror { type It: ?Sized; }
impl<T: ?Sized> Mirror for T { type It = Self; }
#[repr(transparent)]
pub struct StructWithProjection(f32, <f32 as Mirror>::It);
//~^ ERROR needs at most one field with non-trivial size or alignment
2018-01-03 10:43:30 -06:00
#[repr(transparent)]
struct NontrivialAlignZst(u32, [u16; 0]); //~ ERROR needs at most one field with non-trivial size or alignment
2018-01-03 10:43:30 -06:00
#[repr(align(32))]
struct ZstAlign32<T>(PhantomData<T>);
#[repr(transparent)]
struct GenericAlign<T>(ZstAlign32<T>, u32); //~ ERROR needs at most one field with non-trivial size or alignment
#[repr(transparent)]
struct WrapsZstWithAlignment([i32; 0]);
#[repr(transparent)] //~ ERROR unsupported representation for zero-variant enum
enum Void {} //~ ERROR transparent enum needs exactly one variant, but has 0
#[repr(transparent)]
enum FieldlessEnum {
Foo,
}
#[repr(transparent)]
enum UnitFieldEnum {
Foo(()),
}
#[repr(transparent)]
enum TooManyFieldsEnum {
Foo(u32, String),
}
//~^^^ ERROR transparent enum needs at most one field with non-trivial size or alignment, but has 2
#[repr(transparent)]
enum MultipleVariants { //~ ERROR transparent enum needs exactly one variant, but has 2
Foo(String),
Bar,
}
2020-01-12 09:05:18 -06:00
#[repr(transparent)]
enum NontrivialAlignZstEnum { //~ ERROR needs at most one field with non-trivial size or alignment
Foo(u32, [u16; 0]),
2020-01-12 09:05:18 -06:00
}
#[repr(transparent)]
enum GenericAlignEnum<T> { //~ ERROR needs at most one field with non-trivial size or alignment
Foo { bar: ZstAlign32<T>, baz: u32 }
2020-01-12 09:05:18 -06:00
}
#[repr(transparent)]
union UnitUnion {
u: (),
}
#[repr(transparent)]
union TooManyFields { //~ ERROR transparent union needs at most one field with non-trivial size or alignment, but has 2
u: u32,
s: i32
}
fn main() {}