Skip to content

Commit 692833a

Browse files
authored
Rollup merge of #87922 - Manishearth:c-enum-target-spec, r=nagisa,eddyb
Add c_enum_min_bits target spec field, use for arm-none and thumb-none targets Fixes #87917 <s>Haven't tested this yet, still playing around.</s> This seems to fix the issue.
2 parents df23264 + 4c0e424 commit 692833a

13 files changed

+529
-16
lines changed

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ impl IntegerExt for Integer {
112112
let unsigned_fit = Integer::fit_unsigned(cmp::max(min as u128, max as u128));
113113
let signed_fit = cmp::max(Integer::fit_signed(min), Integer::fit_signed(max));
114114

115-
let mut min_from_extern = None;
116-
let min_default = I8;
117-
118115
if let Some(ity) = repr.int {
119116
let discr = Integer::from_attr(&tcx, ity);
120117
let fit = if ity.is_signed() { signed_fit } else { unsigned_fit };
@@ -128,19 +125,14 @@ impl IntegerExt for Integer {
128125
return (discr, ity.is_signed());
129126
}
130127

131-
if repr.c() {
132-
match &tcx.sess.target.arch[..] {
133-
"hexagon" => min_from_extern = Some(I8),
134-
// WARNING: the ARM EABI has two variants; the one corresponding
135-
// to `at_least == I32` appears to be used on Linux and NetBSD,
136-
// but some systems may use the variant corresponding to no
137-
// lower bound. However, we don't run on those yet...?
138-
"arm" => min_from_extern = Some(I32),
139-
_ => min_from_extern = Some(I32),
140-
}
141-
}
142-
143-
let at_least = min_from_extern.unwrap_or(min_default);
128+
let at_least = if repr.c() {
129+
// This is usually I32, however it can be different on some platforms,
130+
// notably hexagon and arm-none/thumb-none
131+
tcx.data_layout().c_enum_min_size
132+
} else {
133+
// repr(Rust) enums try to be as small as possible
134+
I8
135+
};
144136

145137
// If there are no negative values, we can use the unsigned fit.
146138
if min >= 0 {

compiler/rustc_target/src/abi/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ pub struct TargetDataLayout {
3636
pub vector_align: Vec<(Size, AbiAndPrefAlign)>,
3737

3838
pub instruction_address_space: AddressSpace,
39+
40+
/// Minimum size of #[repr(C)] enums (default I32 bits)
41+
pub c_enum_min_size: Integer,
3942
}
4043

4144
impl Default for TargetDataLayout {
@@ -60,6 +63,7 @@ impl Default for TargetDataLayout {
6063
(Size::from_bits(128), AbiAndPrefAlign::new(align(128))),
6164
],
6265
instruction_address_space: AddressSpace::DATA,
66+
c_enum_min_size: Integer::I32,
6367
}
6468
}
6569
}
@@ -173,6 +177,8 @@ impl TargetDataLayout {
173177
));
174178
}
175179

180+
dl.c_enum_min_size = Integer::from_size(Size::from_bits(target.c_enum_min_bits))?;
181+
176182
Ok(dl)
177183
}
178184

@@ -610,6 +616,17 @@ impl Integer {
610616
}
611617
I8
612618
}
619+
620+
fn from_size(size: Size) -> Result<Self, String> {
621+
match size.bits() {
622+
8 => Ok(Integer::I8),
623+
16 => Ok(Integer::I16),
624+
32 => Ok(Integer::I32),
625+
64 => Ok(Integer::I64),
626+
128 => Ok(Integer::I128),
627+
_ => Err(format!("rust does not support integers with {} bits", size.bits())),
628+
}
629+
}
613630
}
614631

615632
/// Fundamental unit of memory access and layout.

compiler/rustc_target/src/spec/armebv7r_none_eabi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub fn target() -> Target {
2020
panic_strategy: PanicStrategy::Abort,
2121
max_atomic_width: Some(32),
2222
emit_debug_gdb_scripts: false,
23+
// GCC and Clang default to 8 for arm-none here
24+
c_enum_min_bits: 8,
2325
..Default::default()
2426
},
2527
}

compiler/rustc_target/src/spec/armebv7r_none_eabihf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub fn target() -> Target {
2121
features: "+vfp3,-d32,-fp16".to_string(),
2222
max_atomic_width: Some(32),
2323
emit_debug_gdb_scripts: false,
24+
// GCC and Clang default to 8 for arm-none here
25+
c_enum_min_bits: 8,
2426
..Default::default()
2527
},
2628
}

compiler/rustc_target/src/spec/armv7a_none_eabi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn target() -> Target {
2828
max_atomic_width: Some(64),
2929
panic_strategy: PanicStrategy::Abort,
3030
emit_debug_gdb_scripts: false,
31+
c_enum_min_bits: 8,
3132
..Default::default()
3233
};
3334
Target {

compiler/rustc_target/src/spec/armv7a_none_eabihf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn target() -> Target {
1919
max_atomic_width: Some(64),
2020
panic_strategy: PanicStrategy::Abort,
2121
emit_debug_gdb_scripts: false,
22+
// GCC and Clang default to 8 for arm-none here
23+
c_enum_min_bits: 8,
2224
..Default::default()
2325
};
2426
Target {

compiler/rustc_target/src/spec/armv7r_none_eabi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub fn target() -> Target {
1919
panic_strategy: PanicStrategy::Abort,
2020
max_atomic_width: Some(32),
2121
emit_debug_gdb_scripts: false,
22+
// GCC and Clang default to 8 for arm-none here
23+
c_enum_min_bits: 8,
2224
..Default::default()
2325
},
2426
}

compiler/rustc_target/src/spec/armv7r_none_eabihf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub fn target() -> Target {
2020
features: "+vfp3,-d32,-fp16".to_string(),
2121
max_atomic_width: Some(32),
2222
emit_debug_gdb_scripts: false,
23+
// GCC and Clang default to 8 for arm-none here
24+
c_enum_min_bits: 8,
2325
..Default::default()
2426
},
2527
}

compiler/rustc_target/src/spec/hexagon_unknown_linux_musl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub fn target() -> Target {
1313
base.dynamic_linking = true;
1414
base.executables = true;
1515

16+
base.c_enum_min_bits = 8;
17+
1618
Target {
1719
llvm_target: "hexagon-unknown-linux-musl".to_string(),
1820
pointer_width: 32,

compiler/rustc_target/src/spec/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,9 @@ pub struct TargetOptions {
13361336

13371337
/// If present it's a default value to use for adjusting the C ABI.
13381338
pub default_adjusted_cabi: Option<Abi>,
1339+
1340+
/// Minimum number of bits in #[repr(C)] enum. Defaults to 32.
1341+
pub c_enum_min_bits: u64,
13391342
}
13401343

13411344
impl Default for TargetOptions {
@@ -1440,6 +1443,7 @@ impl Default for TargetOptions {
14401443
split_debuginfo: SplitDebuginfo::Off,
14411444
supported_sanitizers: SanitizerSet::empty(),
14421445
default_adjusted_cabi: None,
1446+
c_enum_min_bits: 32,
14431447
}
14441448
}
14451449
}
@@ -1604,6 +1608,12 @@ impl Target {
16041608
base.$key_name = s;
16051609
}
16061610
} );
1611+
($key_name:ident, u64) => ( {
1612+
let name = (stringify!($key_name)).replace("_", "-");
1613+
if let Some(s) = obj.remove_key(&name).and_then(|j| Json::as_u64(&j)) {
1614+
base.$key_name = s;
1615+
}
1616+
} );
16071617
($key_name:ident, Option<u32>) => ( {
16081618
let name = (stringify!($key_name)).replace("_", "-");
16091619
if let Some(s) = obj.remove_key(&name).and_then(|j| Json::as_u64(&j)) {
@@ -2017,6 +2027,7 @@ impl Target {
20172027
key!(split_debuginfo, SplitDebuginfo)?;
20182028
key!(supported_sanitizers, SanitizerSet)?;
20192029
key!(default_adjusted_cabi, Option<Abi>)?;
2030+
key!(c_enum_min_bits, u64);
20202031

20212032
if base.is_builtin {
20222033
// This can cause unfortunate ICEs later down the line.
@@ -2255,6 +2266,7 @@ impl ToJson for Target {
22552266
target_option_val!(has_thumb_interworking);
22562267
target_option_val!(split_debuginfo);
22572268
target_option_val!(supported_sanitizers);
2269+
target_option_val!(c_enum_min_bits);
22582270

22592271
if let Some(abi) = self.default_adjusted_cabi {
22602272
d.insert("default-adjusted-cabi".to_string(), Abi::name(abi).to_json());

compiler/rustc_target/src/spec/thumb_base.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ pub fn opts() -> TargetOptions {
5353
// LLVM is eager to trash the link register when calling `noreturn` functions, which
5454
// breaks debugging. Preserve LR by default to prevent that from happening.
5555
frame_pointer: FramePointer::Always,
56+
// ARM supports multiple ABIs for enums, the linux one matches the default of 32 here
57+
// but any arm-none or thumb-none target will be defaulted to 8 on GCC and clang
58+
c_enum_min_bits: 8,
5659
..Default::default()
5760
}
5861
}

src/test/ui/layout/thumb-enum.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// compile-flags: --target thumbv8m.main-none-eabihf
2+
// needs-llvm-components: arm
3+
//
4+
// Verify that thumb targets implement the repr(C) for enums correctly.
5+
//
6+
// See #87917
7+
#![feature(never_type, rustc_attrs, no_core, lang_items)]
8+
#![crate_type = "lib"]
9+
#![no_core]
10+
11+
#[lang="sized"]
12+
trait Sized {}
13+
14+
#[rustc_layout(debug)]
15+
#[repr(C)]
16+
enum A { Apple } //~ ERROR: layout_of
17+
18+
#[rustc_layout(debug)]
19+
#[repr(C)]
20+
enum B { Banana = 255, } //~ ERROR: layout_of
21+
22+
#[rustc_layout(debug)]
23+
#[repr(C)]
24+
enum C { Chaenomeles = 256, } //~ ERROR: layout_of
25+
26+
#[rustc_layout(debug)]
27+
#[repr(C)]
28+
enum P { Peach = 0x1000_0000isize, } //~ ERROR: layout_of
29+
30+
const TANGERINE: usize = 0x8100_0000; // hack to get negative numbers without negation operator!
31+
32+
#[rustc_layout(debug)]
33+
#[repr(C)]
34+
enum T { Tangerine = TANGERINE as isize } //~ ERROR: layout_of

0 commit comments

Comments
 (0)