summaryrefslogtreecommitdiffstats
path: root/tests/ui/repr/16-bit-repr-c-enum.rs
blob: 2acfde4be46d9b0a579fe1f14a527dd121f06720 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// build-pass
// revisions: avr msp430
//
// [avr] needs-llvm-components: avr
// [avr] compile-flags: --target=avr-unknown-gnu-atmega328 --crate-type=rlib
// [msp430] needs-llvm-components: msp430
// [msp430] compile-flags: --target=msp430-none-elf --crate-type=rlib
#![feature(no_core, lang_items, intrinsics, staged_api)]
#![no_core]
#![crate_type = "lib"]
#![stable(feature = "", since = "")]
#![allow(dead_code)]

// Test that the repr(C) attribute doesn't break compilation
// Previous bad assumption was that 32-bit enum default width is fine on msp430, avr
// But the width of the C int on these platforms is 16 bits, and C enums <= C int range
// so we want no more than that, usually. This resulted in errors like
// "layout decided on a larger discriminant type (I32) than typeck (I16)"
#[repr(C)]
enum Foo {
    Bar,
}

extern "rust-intrinsic" {
    #[stable(feature = "", since = "")]
    #[rustc_const_stable(feature = "", since = "")]
    #[rustc_safe_intrinsic]
    fn size_of<T>() -> usize;
}

#[lang="sized"]
trait Sized {}
#[lang="copy"]
trait Copy {}

const EXPECTED: usize = 2;
const ACTUAL: usize = size_of::<Foo>();
// Validate that the size is indeed 16 bits, to match this C static_assert:
/**
```c
#include <assert.h>
enum foo {
    BAR
};
int main(void)
{
    /* passes on msp430-elf-gcc */
    static_assert(sizeof(enum foo) == 2);
}
```
*/
const _: [(); EXPECTED] = [(); ACTUAL];