Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ use rustc_hashes::Hash64;
use rustc_index::{Idx, IndexSlice, IndexVec};
#[cfg(feature = "nightly")]
use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_Generic};
#[cfg(feature = "nightly")]
use rustc_span::{Symbol, sym};

mod callconv;
mod canon_abi;
Expand Down Expand Up @@ -770,6 +772,14 @@ impl Endian {
Self::Big => "big",
}
}

#[cfg(feature = "nightly")]
pub fn desc_symbol(&self) -> Symbol {
match self {
Self::Little => sym::little,
Self::Big => sym::big,
}
}
}

impl fmt::Debug for Endian {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const GATED_CFGS: &[GatedCfg] = &[
sym::cfg_target_has_reliable_f16_f128,
Features::cfg_target_has_reliable_f16_f128,
),
(sym::target_object_format, sym::cfg_target_object_format, Features::cfg_target_object_format),
];

/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ declare_features! (
(unstable, cfg_target_has_atomic, "1.60.0", Some(94039)),
/// Allows `cfg(target_has_atomic_equal_alignment = "...")`.
(unstable, cfg_target_has_atomic_equal_alignment, "1.60.0", Some(93822)),
/// Allows `cfg(target_object_format = "...")`.
(unstable, cfg_target_object_format, "CURRENT_RUSTC_VERSION", Some(152586)),
/// Allows `cfg(target_thread_local)`.
(unstable, cfg_target_thread_local, "1.7.0", Some(29594)),
/// Allows the use of `#[cfg(ub_checks)` to check if UB checks are enabled.
Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_session/src/config/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
| (sym::target_endian, Some(_))
| (sym::target_env, None | Some(_))
| (sym::target_family, Some(_))
| (sym::target_object_format, Some(_))
Comment thread
Urgau marked this conversation as resolved.
| (sym::target_os, Some(_))
| (sym::target_pointer_width, Some(_))
| (sym::target_vendor, None | Some(_))
Expand Down Expand Up @@ -252,8 +253,9 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {

ins_sym!(sym::target_abi, sess.target.cfg_abi.desc_symbol());
ins_sym!(sym::target_arch, sess.target.arch.desc_symbol());
ins_str!(sym::target_endian, sess.target.endian.as_str());
ins_sym!(sym::target_endian, sess.target.endian.desc_symbol());
ins_sym!(sym::target_env, sess.target.env.desc_symbol());
ins_sym!(sym::target_object_format, sess.target.options.binary_format.desc_symbol());

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

// sym::target_*
{
const VALUES: [&Symbol; 8] = [
const VALUES: [&Symbol; 9] = [
&sym::target_abi,
&sym::target_arch,
&sym::target_endian,
&sym::target_env,
&sym::target_family,
&sym::target_object_format,
&sym::target_os,
&sym::target_pointer_width,
&sym::target_vendor,
Expand All @@ -449,6 +452,7 @@ impl CheckCfg {
Some(values_target_endian),
Some(values_target_env),
Some(values_target_family),
Some(values_target_object_format),
Some(values_target_os),
Some(values_target_pointer_width),
Some(values_target_vendor),
Expand All @@ -460,11 +464,12 @@ impl CheckCfg {
for target in Target::builtins().chain(iter::once(current_target.clone())) {
values_target_abi.insert(target.options.cfg_abi.desc_symbol());
values_target_arch.insert(target.arch.desc_symbol());
values_target_endian.insert(Symbol::intern(target.options.endian.as_str()));
values_target_endian.insert(target.options.endian.desc_symbol());
values_target_env.insert(target.options.env.desc_symbol());
values_target_family.extend(
target.options.families.iter().map(|family| Symbol::intern(family)),
);
values_target_object_format.insert(target.options.binary_format.desc_symbol());
values_target_os.insert(target.options.os.desc_symbol());
values_target_pointer_width.insert(sym::integer(target.pointer_width));
values_target_vendor.insert(target.vendor_symbol());
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ symbols! {
cfg_target_has_atomic,
cfg_target_has_atomic_equal_alignment,
cfg_target_has_reliable_f16_f128,
cfg_target_object_format,
cfg_target_thread_local,
cfg_target_vendor,
cfg_trace: "<cfg_trace>", // must not be a valid identifier
Expand Down Expand Up @@ -623,6 +624,7 @@ symbols! {
coerce_pointee_validated,
coerce_shared,
coerce_unsized,
coff,
cold,
cold_path,
collapse_debuginfo,
Expand Down Expand Up @@ -853,6 +855,7 @@ symbols! {
eii_internals,
eii_shared_macro,
element_ty,
elf,
// Notes about `sym::empty`:
// - It should only be used when it genuinely means "empty symbol". Use
// `Option<Symbol>` when "no symbol" is a possibility.
Expand Down Expand Up @@ -1167,6 +1170,7 @@ symbols! {
linkonce_odr,
lint_reasons,
literal,
little, big,
load,
loaded_from_disk,
local,
Expand All @@ -1193,6 +1197,7 @@ symbols! {
lt,
m68k,
m68k_target_feature,
macho: "mach-o",
macro_at_most_once_rep,
macro_attr,
macro_attributes_in_derive_output,
Expand Down Expand Up @@ -2014,6 +2019,7 @@ symbols! {
target_has_reliable_f16_math,
target_has_reliable_f128,
target_has_reliable_f128_math,
target_object_format,
target_os,
target_pointer_width,
target_thread_local,
Expand Down Expand Up @@ -2237,6 +2243,7 @@ symbols! {
vtable_size,
warn,
wasip2,
wasm,
wasm32,
wasm64,
wasm_abi,
Expand Down Expand Up @@ -2271,6 +2278,7 @@ symbols! {
x86_amx_intrinsics,
x87_reg,
x87_target_feature,
xcoff,
xer,
xmm_reg,
xop_target_feature,
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,16 @@ impl BinaryFormat {
Self::Xcoff => object::BinaryFormat::Xcoff,
}
}

pub fn desc_symbol(&self) -> Symbol {
match self {
Self::Coff => sym::coff,
Self::Elf => sym::elf,
Self::MachO => sym::macho,
Self::Wasm => sym::wasm,
Self::Xcoff => sym::xcoff,
}
}
}

impl ToJson for Align {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# `cfg_target_object_format`

The tracking issue for this feature is: [#152586]

[#152586]: https://github.com/rust-lang/rust/issues/152586

------------------------

The `cfg_target_object_format` feature makes it possible to execute different code
depending on the current target's object file format.

## Examples

```rust
#![feature(cfg_target_object_format)]

#[cfg(target_object_format = "elf")]
fn a() {
// ...
}

#[cfg(target_object_format = "mach-o")]
fn a() {
// ...
}

fn b() {
if cfg!(target_object_format = "wasm") {
// ...
} else {
// ...
}
}
```
7 changes: 7 additions & 0 deletions src/librustdoc/clean/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ impl fmt::Display for Display<'_> {
(sym::unix, None) => "Unix",
(sym::windows, None) => "Windows",
(sym::debug_assertions, None) => "debug-assertions enabled",
(sym::target_object_format, Some(format)) => match self.1 {
Format::LongHtml => {
return write!(fmt, "object format <code>{format}</code>");
}
Format::LongPlain => return write!(fmt, "object format `{format}`"),
Format::ShortHtml => return write!(fmt, "<code>{format}</code>"),
},
(sym::target_os, Some(os)) => human_readable_target_os(*os).unwrap_or_default(),
(sym::target_arch, Some(arch)) => {
human_readable_target_arch(*arch).unwrap_or_default()
Expand Down
6 changes: 6 additions & 0 deletions tests/auxiliary/minicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ macro_rules! stringify {
};
}

#[rustc_builtin_macro]
#[macro_export]
macro_rules! compile_error {
($msg:expr $(,)?) => {{ /* compiler built-in */ }};
}

#[lang = "add"]
pub trait Add<Rhs = Self> {
type Output;
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/doc-cfg-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `foo`
LL | #[doc(cfg(foo), cfg(bar))]
| ^^^
|
= help: expected names are: `FALSE` and `test` and 31 more
= help: expected names are: `FALSE` and `test` and 32 more
= help: to expect this configuration use `--check-cfg=cfg(foo)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default
Expand Down
105 changes: 105 additions & 0 deletions tests/ui/cfg/cfg_target_object_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//@ add-minicore
//@ check-pass
//@ ignore-backends: gcc
//
//@ revisions: linux_gnu linux_musl linux_ohos linux_powerpc
//@[linux_gnu] compile-flags: --target aarch64-unknown-linux-gnu
//@[linux_gnu] needs-llvm-components: aarch64
//@[linux_musl] compile-flags: --target aarch64-unknown-linux-musl
//@[linux_musl] needs-llvm-components: aarch64
//@[linux_ohos] compile-flags: --target aarch64-unknown-linux-ohos
//@[linux_ohos] needs-llvm-components: aarch64
//@[linux_powerpc] compile-flags: --target powerpc-unknown-linux-gnu
//@[linux_powerpc] needs-llvm-components: powerpc
//
//@ revisions: darwin ios
//@[darwin] compile-flags: --target aarch64-apple-darwin
//@[darwin] needs-llvm-components: aarch64
//@[ios] compile-flags: --target aarch64-apple-ios
//@[ios] needs-llvm-components: aarch64
//
//@ revisions: win_msvc win_gnu
//@[win_msvc] compile-flags: --target aarch64-pc-windows-msvc
//@[win_msvc] needs-llvm-components: aarch64
//@[win_gnu] compile-flags: --target x86_64-pc-windows-gnu
//@[win_gnu] needs-llvm-components: x86
//
//@ revisions: wasm32 wasm64
//@[wasm32] compile-flags: --target wasm32-unknown-unknown
//@[wasm32] needs-llvm-components: webassembly
//@[wasm64] compile-flags: --target wasm64-unknown-unknown
//@[wasm64] needs-llvm-components: webassembly
//
//@ revisions: aix
//@[aix] compile-flags: --target powerpc64-ibm-aix
//@[aix] needs-llvm-components: powerpc
//
//@ revisions: hermit sgx uefi
//@[hermit] compile-flags: --target x86_64-unknown-hermit
//@[hermit] needs-llvm-components: x86
//@[sgx] compile-flags: --target x86_64-fortanix-unknown-sgx
//@[sgx] needs-llvm-components: x86
//@[uefi] compile-flags: --target x86_64-unknown-uefi
//@[uefi] needs-llvm-components: x86
//
//@ revisions: bpfeb bpfel
//@[bpfeb] compile-flags: --target bpfeb-unknown-none
//@[bpfeb] needs-llvm-components: bpf
//@[bpfel] compile-flags: --target bpfel-unknown-none
//@[bpfel] needs-llvm-components: bpf
//
//@ revisions: avr
//@[avr] compile-flags: --target avr-none -Ctarget-cpu=atmega328
//@[avr] needs-llvm-components: avr
//
//@ revisions: msp430
//@[msp430] compile-flags: --target msp430-none-elf
//@[msp430] needs-llvm-components: msp430
//
//@ revisions: thumb
//@[thumb] compile-flags: --target thumbv7m-none-eabi
//@[thumb] needs-llvm-components: arm
#![crate_type = "lib"]
#![feature(no_core, lang_items, cfg_target_object_format)]
#![no_core]

extern crate minicore;
use minicore::*;

macro_rules! assert_cfg {
($rhs:ident = $rhs_val:literal) => {
#[cfg(not($rhs = $rhs_val))]
compile_error!(concat!("expected `", stringify!($rhs), " = ", $rhs_val, "`",));
};
}

const _: () = {
cfg_select!(
target_os = "linux" => assert_cfg!(target_object_format = "elf"),
target_os = "aix" => assert_cfg!(target_object_format = "xcoff"),
target_os = "uefi" => assert_cfg!(target_object_format = "coff"),
target_os = "windows" => assert_cfg!(target_object_format = "coff"),
target_os = "hermit" => assert_cfg!(target_object_format = "elf"),

target_arch = "bpf" => assert_cfg!(target_object_format = "elf"),
target_arch = "avr" => assert_cfg!(target_object_format = "elf"),
target_arch = "msp430" => assert_cfg!(target_object_format = "elf"),

target_abi = "eabi" => assert_cfg!(target_object_format = "elf"),
target_vendor = "apple" => assert_cfg!(target_object_format = "mach-o"),
target_family = "wasm" => assert_cfg!(target_object_format = "wasm"),

windows => assert_cfg!(target_object_format = "coff"),

_ => {}
);
};

const _: () = {
cfg_select!(
target_object_format = "mach-o" => assert_cfg!(target_vendor = "apple"),
target_object_format = "wasm" => assert_cfg!(target_family = "wasm"),
target_object_format = "xcoff" => assert_cfg!(target_os = "aix"),
_ => {}
);
};
7 changes: 4 additions & 3 deletions tests/ui/cfg/disallowed-cli-cfgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//@ revisions: sanitizer_cfi_generalize_pointers_ sanitizer_cfi_normalize_integers_
//@ revisions: proc_macro_ panic_ target_feature_ unix_ windows_ target_abi_
//@ revisions: target_arch_ target_endian_ target_env_ target_family_ target_os_
//@ revisions: target_pointer_width_ target_vendor_ target_has_atomic_
//@ revisions: target_has_atomic_equal_alignment_ target_has_atomic_load_store_
//@ revisions: target_thread_local_ relocation_model_
//@ revisions: target_object_format_ target_pointer_width_ target_vendor_
//@ revisions: target_has_atomic_ target_has_atomic_equal_alignment_
//@ revisions: target_has_atomic_load_store_ target_thread_local_ relocation_model_
//@ revisions: fmt_debug_
//@ revisions: emscripten_wasm_eh_
//@ revisions: reliable_f16_ reliable_f16_math_ reliable_f128_ reliable_f128_math_
Expand All @@ -26,6 +26,7 @@
//@ [target_endian_]compile-flags: --cfg target_endian="little"
//@ [target_env_]compile-flags: --cfg target_env
//@ [target_family_]compile-flags: --cfg target_family="unix"
//@ [target_object_format_]compile-flags: --cfg target_object_format="elf"
//@ [target_os_]compile-flags: --cfg target_os="linux"
//@ [target_pointer_width_]compile-flags: --cfg target_pointer_width="32"
//@ [target_vendor_]compile-flags: --cfg target_vendor
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/cfg/disallowed-cli-cfgs.target_object_format_.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected `--cfg target_object_format="elf"` flag
|
= note: config `target_object_format` is only supposed to be controlled by `--target`
= note: manually setting a built-in cfg can and does create incoherent behaviors
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default

error: aborting due to 1 previous error

2 changes: 1 addition & 1 deletion tests/ui/check-cfg/cargo-build-script.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `has_foo`
LL | #[cfg(has_foo)]
| ^^^^^^^
|
= help: expected names are: `has_bar` and 31 more
= help: expected names are: `has_bar` and 32 more
= help: consider using a Cargo feature instead
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
[lints.rust]
Expand Down
Loading
Loading