File: 16-bit-repr-c-enum.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (53 lines) | stat: -rw-r--r-- 1,509 bytes parent folder | download | duplicates (3)
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
53
//@ 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, rustc_attrs)]
#![no_core]
#![crate_type = "lib"]
#![stable(feature = "intrinsics_for_test", since = "3.3.3")]
#![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,
}

#[stable(feature = "intrinsics_for_test", since = "3.3.3")]
#[rustc_const_stable(feature = "intrinsics_for_test", since = "3.3.3")]
#[rustc_intrinsic]
#[rustc_intrinsic_must_be_overridden]
const fn size_of<T>() -> usize {
    loop {}
}

#[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];