rustc_target: Further simplify loading of built-in targets

using the fact that it is infallible.

JSON roundtrip check on every rustc run is also removed, it's already performed by unit tests.
This commit is contained in:
Vadim Petrochenkov 2020-10-05 16:24:04 +03:00
parent 021fcbd90c
commit 1444ad7ba1
2 changed files with 14 additions and 38 deletions

View File

@ -672,7 +672,8 @@ impl RustcDefaultCalls {
for req in &sess.opts.prints {
match *req {
TargetList => {
let mut targets = rustc_target::spec::get_targets().collect::<Vec<String>>();
let mut targets =
rustc_target::spec::TARGETS.iter().copied().collect::<Vec<_>>();
targets.sort();
println!("{}", targets.join("\n"));
}

View File

@ -430,11 +430,6 @@ impl fmt::Display for LinkOutputKind {
}
}
pub enum LoadTargetError {
BuiltinTargetNotFound(String),
Other(String),
}
pub type LinkArgs = BTreeMap<LinkerFlavor, Vec<String>>;
macro_rules! supported_targets {
@ -442,34 +437,16 @@ macro_rules! supported_targets {
$(mod $module;)+
/// List of supported targets
const TARGETS: &[&str] = &[$($($triple),+),+];
pub const TARGETS: &[&str] = &[$($($triple),+),+];
fn load_specific(target: &str) -> Result<Target, LoadTargetError> {
match target {
$(
$($triple)|+ => {
let mut t = $module::target();
t.options.is_builtin = true;
// round-trip through the JSON parser to ensure at
// run-time that the parser works correctly
t = Target::from_json(t.to_json())
.map_err(LoadTargetError::Other)?;
debug!("got builtin target: {:?}", t);
Ok(t)
},
)+
_ => Err(LoadTargetError::BuiltinTargetNotFound(
format!("Unable to find target: {}", target)))
}
}
pub fn get_targets() -> impl Iterator<Item = String> {
TARGETS.iter().filter_map(|t| -> Option<String> {
load_specific(t)
.and(Ok(t.to_string()))
.ok()
})
fn load_builtin(target: &str) -> Option<Target> {
let mut t = match target {
$( $($triple)|+ => $module::target(), )+
_ => return None,
};
t.options.is_builtin = true;
debug!("got builtin target: {:?}", t);
Some(t)
}
#[cfg(test)]
@ -1529,11 +1506,9 @@ impl Target {
match *target_triple {
TargetTriple::TargetTriple(ref target_triple) => {
// check if triple is in list of supported targets
match load_specific(target_triple) {
Ok(t) => return Ok(t),
Err(LoadTargetError::BuiltinTargetNotFound(_)) => (),
Err(LoadTargetError::Other(e)) => return Err(e),
// check if triple is in list of built-in targets
if let Some(t) = load_builtin(target_triple) {
return Ok(t);
}
// search for a file named `target_triple`.json in RUST_TARGET_PATH