2024-02-16 14:02:50 -06:00
|
|
|
//@ build-pass
|
|
|
|
//@ revisions: avr msp430
|
Default repr(C) enums to c_int size
This is what ISO C strongly implies this is correct, and
many processor-specific ABIs imply or mandate this size, so
"everyone" (LLVM, gcc...) defaults to emitting enums this way.
However, this is by no means guaranteed by ISO C,
and the bare-metal Arm targets show it can be overridden,
which rustc supports via `c-enum-min-bits` in a target.json.
The override is a flag named `-fshort-enums` in clang and gcc,
but introducing a CLI flag is probably unnecessary for rustc.
This flag can be used by non-Arm microcontroller targets,
like AVR and MSP430, but it is not enabled for them by default.
Rust programmers who know the size of a target's enums
can use explicit reprs, which also lets them match C23 code.
This change is most relevant to 16-bit targets: AVR and MSP430.
Most of rustc's targets use 32-bit ints, but ILP64 does exist.
Regardless, rustc should now correctly handle enums for
both very small and very large targets.
Thanks to William for confirming MSP430 behavior,
and to Waffle for better style and no-core size_of asserts.
Co-authored-by: William D. Jones <thor0505@comcast.net>
Co-authored-by: Waffle Maybe <waffle.lapkin@gmail.com>
2023-02-02 02:14:36 -06:00
|
|
|
//
|
2024-02-16 14:02:50 -06:00
|
|
|
//@ [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
|
2023-09-25 14:40:40 -05:00
|
|
|
#![feature(no_core, lang_items, intrinsics, staged_api, rustc_attrs)]
|
Default repr(C) enums to c_int size
This is what ISO C strongly implies this is correct, and
many processor-specific ABIs imply or mandate this size, so
"everyone" (LLVM, gcc...) defaults to emitting enums this way.
However, this is by no means guaranteed by ISO C,
and the bare-metal Arm targets show it can be overridden,
which rustc supports via `c-enum-min-bits` in a target.json.
The override is a flag named `-fshort-enums` in clang and gcc,
but introducing a CLI flag is probably unnecessary for rustc.
This flag can be used by non-Arm microcontroller targets,
like AVR and MSP430, but it is not enabled for them by default.
Rust programmers who know the size of a target's enums
can use explicit reprs, which also lets them match C23 code.
This change is most relevant to 16-bit targets: AVR and MSP430.
Most of rustc's targets use 32-bit ints, but ILP64 does exist.
Regardless, rustc should now correctly handle enums for
both very small and very large targets.
Thanks to William for confirming MSP430 behavior,
and to Waffle for better style and no-core size_of asserts.
Co-authored-by: William D. Jones <thor0505@comcast.net>
Co-authored-by: Waffle Maybe <waffle.lapkin@gmail.com>
2023-02-02 02:14:36 -06:00
|
|
|
#![no_core]
|
|
|
|
#![crate_type = "lib"]
|
2023-10-16 15:11:26 -05:00
|
|
|
#![stable(feature = "intrinsics_for_test", since = "3.3.3")]
|
Default repr(C) enums to c_int size
This is what ISO C strongly implies this is correct, and
many processor-specific ABIs imply or mandate this size, so
"everyone" (LLVM, gcc...) defaults to emitting enums this way.
However, this is by no means guaranteed by ISO C,
and the bare-metal Arm targets show it can be overridden,
which rustc supports via `c-enum-min-bits` in a target.json.
The override is a flag named `-fshort-enums` in clang and gcc,
but introducing a CLI flag is probably unnecessary for rustc.
This flag can be used by non-Arm microcontroller targets,
like AVR and MSP430, but it is not enabled for them by default.
Rust programmers who know the size of a target's enums
can use explicit reprs, which also lets them match C23 code.
This change is most relevant to 16-bit targets: AVR and MSP430.
Most of rustc's targets use 32-bit ints, but ILP64 does exist.
Regardless, rustc should now correctly handle enums for
both very small and very large targets.
Thanks to William for confirming MSP430 behavior,
and to Waffle for better style and no-core size_of asserts.
Co-authored-by: William D. Jones <thor0505@comcast.net>
Co-authored-by: Waffle Maybe <waffle.lapkin@gmail.com>
2023-02-02 02:14:36 -06:00
|
|
|
#![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" {
|
2023-10-16 15:11:26 -05:00
|
|
|
#[stable(feature = "intrinsics_for_test", since = "3.3.3")]
|
|
|
|
#[rustc_const_stable(feature = "intrinsics_for_test", since = "3.3.3")]
|
Default repr(C) enums to c_int size
This is what ISO C strongly implies this is correct, and
many processor-specific ABIs imply or mandate this size, so
"everyone" (LLVM, gcc...) defaults to emitting enums this way.
However, this is by no means guaranteed by ISO C,
and the bare-metal Arm targets show it can be overridden,
which rustc supports via `c-enum-min-bits` in a target.json.
The override is a flag named `-fshort-enums` in clang and gcc,
but introducing a CLI flag is probably unnecessary for rustc.
This flag can be used by non-Arm microcontroller targets,
like AVR and MSP430, but it is not enabled for them by default.
Rust programmers who know the size of a target's enums
can use explicit reprs, which also lets them match C23 code.
This change is most relevant to 16-bit targets: AVR and MSP430.
Most of rustc's targets use 32-bit ints, but ILP64 does exist.
Regardless, rustc should now correctly handle enums for
both very small and very large targets.
Thanks to William for confirming MSP430 behavior,
and to Waffle for better style and no-core size_of asserts.
Co-authored-by: William D. Jones <thor0505@comcast.net>
Co-authored-by: Waffle Maybe <waffle.lapkin@gmail.com>
2023-02-02 02:14:36 -06:00
|
|
|
#[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];
|