Skip to content

Commit d789de6

Browse files
committed
Auto merge of rust-lang#85775 - adamrk:warn-unused-target-fields, r=nagisa
Emit warnings for unused fields in custom targets. Add a warning which lists any fields in a custom target `json` file that aren't used. Currently unrecognized fields are ignored so, for example, a typo in the `json` will silently produce a target which isn't the one intended.
2 parents 3824017 + 88b01f1 commit d789de6

File tree

7 files changed

+235
-91
lines changed

7 files changed

+235
-91
lines changed

compiler/rustc_serialize/src/json.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,15 @@ impl Json {
11141114
}
11151115
}
11161116

1117+
/// If the Json value is an Object, deletes the value associated with the
1118+
/// provided key from the Object and returns it. Otherwise, returns None.
1119+
pub fn remove_key(&mut self, key: &str) -> Option<Json> {
1120+
match *self {
1121+
Json::Object(ref mut map) => map.remove(key),
1122+
_ => None,
1123+
}
1124+
}
1125+
11171126
/// Attempts to get a nested Json Object for each key in `keys`.
11181127
/// If any key is found not to exist, `find_path` will return `None`.
11191128
/// Otherwise, it will return the Json value associated with the final key.

compiler/rustc_session/src/config.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::fx::FxHashSet;
1212
use rustc_data_structures::impl_stable_hash_via_hash;
1313

1414
use rustc_target::abi::{Align, TargetDataLayout};
15-
use rustc_target::spec::{SplitDebuginfo, Target, TargetTriple};
15+
use rustc_target::spec::{SplitDebuginfo, Target, TargetTriple, TargetWarnings};
1616

1717
use rustc_serialize::json;
1818

@@ -899,9 +899,11 @@ pub(super) fn build_target_config(
899899
target_override: Option<Target>,
900900
sysroot: &PathBuf,
901901
) -> Target {
902-
let target_result =
903-
target_override.map_or_else(|| Target::search(&opts.target_triple, sysroot), Ok);
904-
let target = target_result.unwrap_or_else(|e| {
902+
let target_result = target_override.map_or_else(
903+
|| Target::search(&opts.target_triple, sysroot),
904+
|t| Ok((t, TargetWarnings::empty())),
905+
);
906+
let (target, target_warnings) = target_result.unwrap_or_else(|e| {
905907
early_error(
906908
opts.error_format,
907909
&format!(
@@ -911,6 +913,9 @@ pub(super) fn build_target_config(
911913
),
912914
)
913915
});
916+
for warning in target_warnings.warning_messages() {
917+
early_warn(opts.error_format, &warning)
918+
}
914919

915920
if !matches!(target.pointer_width, 16 | 32 | 64) {
916921
early_error(

compiler/rustc_session/src/session.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,12 @@ pub fn build_session(
12841284

12851285
let target_cfg = config::build_target_config(&sopts, target_override, &sysroot);
12861286
let host_triple = TargetTriple::from_triple(config::host_triple());
1287-
let host = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| {
1287+
let (host, target_warnings) = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| {
12881288
early_error(sopts.error_format, &format!("Error loading host specification: {}", e))
12891289
});
1290+
for warning in target_warnings.warning_messages() {
1291+
early_warn(sopts.error_format, &warning)
1292+
}
12901293

12911294
let loader = file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
12921295
let hash_kind = sopts.debugging_opts.src_hash_algorithm.unwrap_or_else(|| {

compiler/rustc_target/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub mod abi;
2727
pub mod asm;
2828
pub mod spec;
2929

30+
#[cfg(test)]
31+
mod tests;
32+
3033
/// Requirements for a `StableHashingContext` to be used in this crate.
3134
/// This is a hack to allow using the `HashStable_Generic` derive macro
3235
/// instead of implementing everything in `rustc_middle`.

0 commit comments

Comments
 (0)