Skip to content

Commit 9d68571

Browse files
authored
Unrolled build for #155126
Rollup merge of #155126 - folkertdev:target-object-format, r=Urgau add `cfg(target_object_format = "...")` tracking issue: #152586 I'm implementing the predicate as `target_object_format`, because that's what is useful to me (for testing `#[link_section = "..."]` where `mach-o` has some extra restrictions) and maps cleanly to the `BinaryFormat` enum that is used internally. There is still room for a future `target_executable_format` when there is a use case. cc @joshtriplett as the lang sponsor of this feature, @workingjubilee as the author of the proposal. r? JonathanBrouwer a sidequest from the sidequest that is #155065
2 parents bf4fbfb + 6bcd172 commit 9d68571

35 files changed

+282
-53
lines changed

‎compiler/rustc_abi/src/lib.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ use rustc_hashes::Hash64;
5252
use rustc_index::{Idx, IndexSlice, IndexVec};
5353
#[cfg(feature = "nightly")]
5454
use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_Generic};
55+
#[cfg(feature = "nightly")]
56+
use rustc_span::{Symbol, sym};
5557

5658
mod callconv;
5759
mod canon_abi;
@@ -770,6 +772,14 @@ impl Endian {
770772
Self::Big => "big",
771773
}
772774
}
775+
776+
#[cfg(feature = "nightly")]
777+
pub fn desc_symbol(&self) -> Symbol {
778+
match self {
779+
Self::Little => sym::little,
780+
Self::Big => sym::big,
781+
}
782+
}
773783
}
774784

775785
impl fmt::Debug for Endian {

‎compiler/rustc_feature/src/builtin_attrs.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const GATED_CFGS: &[GatedCfg] = &[
6262
sym::cfg_target_has_reliable_f16_f128,
6363
Features::cfg_target_has_reliable_f16_f128,
6464
),
65+
(sym::target_object_format, sym::cfg_target_object_format, Features::cfg_target_object_format),
6566
];
6667

6768
/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.

‎compiler/rustc_feature/src/unstable.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ declare_features! (
410410
(unstable, cfg_target_has_atomic, "1.60.0", Some(94039)),
411411
/// Allows `cfg(target_has_atomic_equal_alignment = "...")`.
412412
(unstable, cfg_target_has_atomic_equal_alignment, "1.60.0", Some(93822)),
413+
/// Allows `cfg(target_object_format = "...")`.
414+
(unstable, cfg_target_object_format, "CURRENT_RUSTC_VERSION", Some(152586)),
413415
/// Allows `cfg(target_thread_local)`.
414416
(unstable, cfg_target_thread_local, "1.7.0", Some(29594)),
415417
/// Allows the use of `#[cfg(ub_checks)` to check if UB checks are enabled.

‎compiler/rustc_session/src/config/cfg.rs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
144144
| (sym::target_endian, Some(_))
145145
| (sym::target_env, None | Some(_))
146146
| (sym::target_family, Some(_))
147+
| (sym::target_object_format, Some(_))
147148
| (sym::target_os, Some(_))
148149
| (sym::target_pointer_width, Some(_))
149150
| (sym::target_vendor, None | Some(_))
@@ -252,8 +253,9 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
252253

253254
ins_sym!(sym::target_abi, sess.target.cfg_abi.desc_symbol());
254255
ins_sym!(sym::target_arch, sess.target.arch.desc_symbol());
255-
ins_str!(sym::target_endian, sess.target.endian.as_str());
256+
ins_sym!(sym::target_endian, sess.target.endian.desc_symbol());
256257
ins_sym!(sym::target_env, sess.target.env.desc_symbol());
258+
ins_sym!(sym::target_object_format, sess.target.options.binary_format.desc_symbol());
257259

258260
for family in sess.target.families.as_ref() {
259261
ins_str!(sym::target_family, family);
@@ -420,12 +422,13 @@ impl CheckCfg {
420422

421423
// sym::target_*
422424
{
423-
const VALUES: [&Symbol; 8] = [
425+
const VALUES: [&Symbol; 9] = [
424426
&sym::target_abi,
425427
&sym::target_arch,
426428
&sym::target_endian,
427429
&sym::target_env,
428430
&sym::target_family,
431+
&sym::target_object_format,
429432
&sym::target_os,
430433
&sym::target_pointer_width,
431434
&sym::target_vendor,
@@ -449,6 +452,7 @@ impl CheckCfg {
449452
Some(values_target_endian),
450453
Some(values_target_env),
451454
Some(values_target_family),
455+
Some(values_target_object_format),
452456
Some(values_target_os),
453457
Some(values_target_pointer_width),
454458
Some(values_target_vendor),
@@ -460,11 +464,12 @@ impl CheckCfg {
460464
for target in Target::builtins().chain(iter::once(current_target.clone())) {
461465
values_target_abi.insert(target.options.cfg_abi.desc_symbol());
462466
values_target_arch.insert(target.arch.desc_symbol());
463-
values_target_endian.insert(Symbol::intern(target.options.endian.as_str()));
467+
values_target_endian.insert(target.options.endian.desc_symbol());
464468
values_target_env.insert(target.options.env.desc_symbol());
465469
values_target_family.extend(
466470
target.options.families.iter().map(|family| Symbol::intern(family)),
467471
);
472+
values_target_object_format.insert(target.options.binary_format.desc_symbol());
468473
values_target_os.insert(target.options.os.desc_symbol());
469474
values_target_pointer_width.insert(sym::integer(target.pointer_width));
470475
values_target_vendor.insert(target.vendor_symbol());

‎compiler/rustc_span/src/symbol.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ symbols! {
589589
cfg_target_has_atomic,
590590
cfg_target_has_atomic_equal_alignment,
591591
cfg_target_has_reliable_f16_f128,
592+
cfg_target_object_format,
592593
cfg_target_thread_local,
593594
cfg_target_vendor,
594595
cfg_trace: "<cfg_trace>", // must not be a valid identifier
@@ -623,6 +624,7 @@ symbols! {
623624
coerce_pointee_validated,
624625
coerce_shared,
625626
coerce_unsized,
627+
coff,
626628
cold,
627629
cold_path,
628630
collapse_debuginfo,
@@ -853,6 +855,7 @@ symbols! {
853855
eii_internals,
854856
eii_shared_macro,
855857
element_ty,
858+
elf,
856859
// Notes about `sym::empty`:
857860
// - It should only be used when it genuinely means "empty symbol". Use
858861
// `Option<Symbol>` when "no symbol" is a possibility.
@@ -1167,6 +1170,7 @@ symbols! {
11671170
linkonce_odr,
11681171
lint_reasons,
11691172
literal,
1173+
little, big,
11701174
load,
11711175
loaded_from_disk,
11721176
local,
@@ -1193,6 +1197,7 @@ symbols! {
11931197
lt,
11941198
m68k,
11951199
m68k_target_feature,
1200+
macho: "mach-o",
11961201
macro_at_most_once_rep,
11971202
macro_attr,
11981203
macro_attributes_in_derive_output,
@@ -2014,6 +2019,7 @@ symbols! {
20142019
target_has_reliable_f16_math,
20152020
target_has_reliable_f128,
20162021
target_has_reliable_f128_math,
2022+
target_object_format,
20172023
target_os,
20182024
target_pointer_width,
20192025
target_thread_local,
@@ -2237,6 +2243,7 @@ symbols! {
22372243
vtable_size,
22382244
warn,
22392245
wasip2,
2246+
wasm,
22402247
wasm32,
22412248
wasm64,
22422249
wasm_abi,
@@ -2271,6 +2278,7 @@ symbols! {
22712278
x86_amx_intrinsics,
22722279
x87_reg,
22732280
x87_target_feature,
2281+
xcoff,
22742282
xer,
22752283
xmm_reg,
22762284
xop_target_feature,

‎compiler/rustc_target/src/spec/mod.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,16 @@ impl BinaryFormat {
13951395
Self::Xcoff => object::BinaryFormat::Xcoff,
13961396
}
13971397
}
1398+
1399+
pub fn desc_symbol(&self) -> Symbol {
1400+
match self {
1401+
Self::Coff => sym::coff,
1402+
Self::Elf => sym::elf,
1403+
Self::MachO => sym::macho,
1404+
Self::Wasm => sym::wasm,
1405+
Self::Xcoff => sym::xcoff,
1406+
}
1407+
}
13981408
}
13991409

14001410
impl ToJson for Align {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# `cfg_target_object_format`
2+
3+
The tracking issue for this feature is: [#152586]
4+
5+
[#152586]: https://github.com/rust-lang/rust/issues/152586
6+
7+
------------------------
8+
9+
The `cfg_target_object_format` feature makes it possible to execute different code
10+
depending on the current target's object file format.
11+
12+
## Examples
13+
14+
```rust
15+
#![feature(cfg_target_object_format)]
16+
17+
#[cfg(target_object_format = "elf")]
18+
fn a() {
19+
// ...
20+
}
21+
22+
#[cfg(target_object_format = "mach-o")]
23+
fn a() {
24+
// ...
25+
}
26+
27+
fn b() {
28+
if cfg!(target_object_format = "wasm") {
29+
// ...
30+
} else {
31+
// ...
32+
}
33+
}
34+
```

‎src/librustdoc/clean/cfg.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,13 @@ impl fmt::Display for Display<'_> {
423423
(sym::unix, None) => "Unix",
424424
(sym::windows, None) => "Windows",
425425
(sym::debug_assertions, None) => "debug-assertions enabled",
426+
(sym::target_object_format, Some(format)) => match self.1 {
427+
Format::LongHtml => {
428+
return write!(fmt, "object format <code>{format}</code>");
429+
}
430+
Format::LongPlain => return write!(fmt, "object format `{format}`"),
431+
Format::ShortHtml => return write!(fmt, "<code>{format}</code>"),
432+
},
426433
(sym::target_os, Some(os)) => human_readable_target_os(*os).unwrap_or_default(),
427434
(sym::target_arch, Some(arch)) => {
428435
human_readable_target_arch(*arch).unwrap_or_default()

‎tests/auxiliary/minicore.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ macro_rules! stringify {
187187
};
188188
}
189189

190+
#[rustc_builtin_macro]
191+
#[macro_export]
192+
macro_rules! compile_error {
193+
($msg:expr $(,)?) => {{ /* compiler built-in */ }};
194+
}
195+
190196
#[lang = "add"]
191197
pub trait Add<Rhs = Self> {
192198
type Output;

‎tests/rustdoc-ui/doc-cfg-2.stderr‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `foo`
44
LL | #[doc(cfg(foo), cfg(bar))]
55
| ^^^
66
|
7-
= help: expected names are: `FALSE` and `test` and 31 more
7+
= help: expected names are: `FALSE` and `test` and 32 more
88
= help: to expect this configuration use `--check-cfg=cfg(foo)`
99
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
1010
= note: `#[warn(unexpected_cfgs)]` on by default

0 commit comments

Comments
 (0)