Skip to content

Add the relocation_model to the cfg #113966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2023
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
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ declare_features! (
(active, c_variadic, "1.34.0", Some(44930), None),
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.
(active, cfg_overflow_checks, "1.71.0", Some(111466), None),
/// Provides the relocation model information as cfg entry
(active, cfg_relocation_model, "CURRENT_RUSTC_VERSION", Some(114929), None),
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.
(active, cfg_sanitize, "1.41.0", Some(39699), None),
/// Allows `cfg(target_abi = "...")`.
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 @@ -35,6 +35,7 @@ const GATED_CFGS: &[GatedCfg] = &[
(sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
(sym::sanitize, sym::cfg_sanitize, cfg_fn!(cfg_sanitize)),
(sym::version, sym::cfg_version, cfg_fn!(cfg_version)),
(sym::relocation_model, sym::cfg_relocation_model, cfg_fn!(cfg_relocation_model)),
];

/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{EarlyErrorHandler, Session};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey};
use rustc_target::abi::Align;
use rustc_target::spec::{PanicStrategy, SanitizerSet, SplitDebuginfo};
use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet, SplitDebuginfo};
use rustc_target::spec::{Target, TargetTriple, TargetWarnings, TARGETS};

use crate::parse::{CrateCheckConfig, CrateConfig};
Expand Down Expand Up @@ -1193,6 +1193,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
let os = &sess.target.os;
let env = &sess.target.env;
let abi = &sess.target.abi;
let relocation_model = sess.target.relocation_model.desc_symbol();
let vendor = &sess.target.vendor;
let min_atomic_width = sess.target.min_atomic_width();
let max_atomic_width = sess.target.max_atomic_width();
Expand All @@ -1218,6 +1219,9 @@ fn default_configuration(sess: &Session) -> CrateConfig {
ret.insert((sym::target_pointer_width, Some(Symbol::intern(&wordsz))));
ret.insert((sym::target_env, Some(Symbol::intern(env))));
ret.insert((sym::target_abi, Some(Symbol::intern(abi))));
if sess.is_nightly_build() {
ret.insert((sym::relocation_model, Some(relocation_model)));
}
ret.insert((sym::target_vendor, Some(Symbol::intern(vendor))));
if sess.target.has_thread_local {
ret.insert((sym::target_thread_local, None));
Expand Down Expand Up @@ -1415,6 +1419,8 @@ impl CrateCheckConfig {
.into_iter()
.map(|sanitizer| Symbol::intern(sanitizer.as_str().unwrap()));

let relocation_model_values = RelocModel::all();

// Unknown possible values:
// - `feature`
// - `target_feature`
Expand Down Expand Up @@ -1453,6 +1459,10 @@ impl CrateCheckConfig {
.entry(sym::target_has_atomic_equal_alignment)
.or_insert_with(no_values)
.extend(atomic_values);
self.expecteds
.entry(sym::relocation_model)
.or_insert_with(empty_values)
.extend(relocation_model_values);

// Target specific values
{
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 @@ -468,6 +468,7 @@ symbols! {
cfg_hide,
cfg_overflow_checks,
cfg_panic,
cfg_relocation_model,
cfg_sanitize,
cfg_target_abi,
cfg_target_compact,
Expand Down Expand Up @@ -661,6 +662,7 @@ symbols! {
dyn_metadata,
dyn_star,
dyn_trait,
dynamic_no_pic: "dynamic-no-pic",
e,
edition_panic,
effects,
Expand Down Expand Up @@ -1115,6 +1117,8 @@ symbols! {
path,
pattern_parentheses,
phantom_data,
pic,
pie,
pin,
platform_intrinsics,
plugin,
Expand Down Expand Up @@ -1223,6 +1227,7 @@ symbols! {
register_tool,
relaxed_adts,
relaxed_struct_unsize,
relocation_model,
rem,
rem_assign,
repr,
Expand All @@ -1243,6 +1248,8 @@ symbols! {
rintf64,
riscv_target_feature,
rlib,
ropi,
ropi_rwpi: "ropi-rwpi",
rotate_left,
rotate_right,
roundevenf32,
Expand Down Expand Up @@ -1354,6 +1361,7 @@ symbols! {
rustdoc_missing_doc_code_examples,
rustfmt,
rvalue_static_promotion,
rwpi,
s,
safety,
sanitize,
Expand Down
50 changes: 39 additions & 11 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::spec::crt_objects::{CrtObjects, LinkSelfContainedDefault};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_fs_util::try_canonicalize;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_span::symbol::{sym, Symbol};
use rustc_span::symbol::{kw, sym, Symbol};
use serde_json::Value;
use std::borrow::Cow;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -655,6 +655,43 @@ pub enum RelocModel {
RopiRwpi,
}

impl RelocModel {
pub fn desc(&self) -> &str {
match *self {
RelocModel::Static => "static",
RelocModel::Pic => "pic",
RelocModel::Pie => "pie",
RelocModel::DynamicNoPic => "dynamic-no-pic",
RelocModel::Ropi => "ropi",
RelocModel::Rwpi => "rwpi",
RelocModel::RopiRwpi => "ropi-rwpi",
}
}
pub const fn desc_symbol(&self) -> Symbol {
match *self {
RelocModel::Static => kw::Static,
RelocModel::Pic => sym::pic,
RelocModel::Pie => sym::pie,
RelocModel::DynamicNoPic => sym::dynamic_no_pic,
RelocModel::Ropi => sym::ropi,
RelocModel::Rwpi => sym::rwpi,
RelocModel::RopiRwpi => sym::ropi_rwpi,
}
}

pub const fn all() -> [Symbol; 7] {
[
RelocModel::Static.desc_symbol(),
RelocModel::Pic.desc_symbol(),
RelocModel::Pie.desc_symbol(),
RelocModel::DynamicNoPic.desc_symbol(),
RelocModel::Ropi.desc_symbol(),
RelocModel::Rwpi.desc_symbol(),
RelocModel::RopiRwpi.desc_symbol(),
]
}
}

impl FromStr for RelocModel {
type Err = ();

Expand All @@ -674,16 +711,7 @@ impl FromStr for RelocModel {

impl ToJson for RelocModel {
fn to_json(&self) -> Json {
match *self {
RelocModel::Static => "static",
RelocModel::Pic => "pic",
RelocModel::Pie => "pie",
RelocModel::DynamicNoPic => "dynamic-no-pic",
RelocModel::Ropi => "ropi",
RelocModel::Rwpi => "rwpi",
RelocModel::RopiRwpi => "ropi-rwpi",
}
.to_json()
self.desc().to_json()
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/abi/relocation_model_pic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// run-pass
// compile-flags: -C relocation-model=pic
// ignore-emscripten no pic
// ignore-wasm

#![feature(cfg_relocation_model)]

#[cfg(relocation_model = "pic")]
fn main() {}
4 changes: 4 additions & 0 deletions tests/ui/feature-gates/feature-gate-cfg-relocation-model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg(relocation_model = "pic")] //~ ERROR
fn _foo() {}

fn main() {}
12 changes: 12 additions & 0 deletions tests/ui/feature-gates/feature-gate-cfg-relocation-model.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0658]: `cfg(relocation_model)` is experimental and subject to change
--> $DIR/feature-gate-cfg-relocation-model.rs:1:7
|
LL | #[cfg(relocation_model = "pic")]
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #114929 <https://github.com/rust-lang/rust/issues/114929> for more information
= help: add `#![feature(cfg_relocation_model)]` to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.