Skip to content

Commit

Permalink
Auto merge of #85775 - adamrk:warn-unused-target-fields, r=nagisa
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jun 21, 2021
2 parents 3824017 + 88b01f1 commit d789de6
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 91 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_serialize/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,15 @@ impl Json {
}
}

/// If the Json value is an Object, deletes the value associated with the
/// provided key from the Object and returns it. Otherwise, returns None.
pub fn remove_key(&mut self, key: &str) -> Option<Json> {
match *self {
Json::Object(ref mut map) => map.remove(key),
_ => None,
}
}

/// Attempts to get a nested Json Object for each key in `keys`.
/// If any key is found not to exist, `find_path` will return `None`.
/// Otherwise, it will return the Json value associated with the final key.
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::impl_stable_hash_via_hash;

use rustc_target::abi::{Align, TargetDataLayout};
use rustc_target::spec::{SplitDebuginfo, Target, TargetTriple};
use rustc_target::spec::{SplitDebuginfo, Target, TargetTriple, TargetWarnings};

use rustc_serialize::json;

Expand Down Expand Up @@ -899,9 +899,11 @@ pub(super) fn build_target_config(
target_override: Option<Target>,
sysroot: &PathBuf,
) -> Target {
let target_result =
target_override.map_or_else(|| Target::search(&opts.target_triple, sysroot), Ok);
let target = target_result.unwrap_or_else(|e| {
let target_result = target_override.map_or_else(
|| Target::search(&opts.target_triple, sysroot),
|t| Ok((t, TargetWarnings::empty())),
);
let (target, target_warnings) = target_result.unwrap_or_else(|e| {
early_error(
opts.error_format,
&format!(
Expand All @@ -911,6 +913,9 @@ pub(super) fn build_target_config(
),
)
});
for warning in target_warnings.warning_messages() {
early_warn(opts.error_format, &warning)
}

if !matches!(target.pointer_width, 16 | 32 | 64) {
early_error(
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,9 +1284,12 @@ pub fn build_session(

let target_cfg = config::build_target_config(&sopts, target_override, &sysroot);
let host_triple = TargetTriple::from_triple(config::host_triple());
let host = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| {
let (host, target_warnings) = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| {
early_error(sopts.error_format, &format!("Error loading host specification: {}", e))
});
for warning in target_warnings.warning_messages() {
early_warn(sopts.error_format, &warning)
}

let loader = file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
let hash_kind = sopts.debugging_opts.src_hash_algorithm.unwrap_or_else(|| {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_target/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub mod abi;
pub mod asm;
pub mod spec;

#[cfg(test)]
mod tests;

/// Requirements for a `StableHashingContext` to be used in this crate.
/// This is a hack to allow using the `HashStable_Generic` derive macro
/// instead of implementing everything in `rustc_middle`.
Expand Down
Loading

0 comments on commit d789de6

Please sign in to comment.