summaryrefslogtreecommitdiffstats
path: root/tests/ui/repr/16-bit-repr-c-enum.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/repr/16-bit-repr-c-enum.rs')
-rw-r--r--tests/ui/repr/16-bit-repr-c-enum.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/ui/repr/16-bit-repr-c-enum.rs b/tests/ui/repr/16-bit-repr-c-enum.rs
new file mode 100644
index 000000000..2acfde4be
--- /dev/null
+++ b/tests/ui/repr/16-bit-repr-c-enum.rs
@@ -0,0 +1,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];