Skip to content

Commit 1444ad7

Browse files
committed
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.
1 parent 021fcbd commit 1444ad7

File tree

2 files changed

+15
-39
lines changed

2 files changed

+15
-39
lines changed

compiler/rustc_driver/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,8 @@ impl RustcDefaultCalls {
672672
for req in &sess.opts.prints {
673673
match *req {
674674
TargetList => {
675-
let mut targets = rustc_target::spec::get_targets().collect::<Vec<String>>();
675+
let mut targets =
676+
rustc_target::spec::TARGETS.iter().copied().collect::<Vec<_>>();
676677
targets.sort();
677678
println!("{}", targets.join("\n"));
678679
}

compiler/rustc_target/src/spec/mod.rs

+13-38
Original file line numberDiff line numberDiff line change
@@ -430,46 +430,23 @@ impl fmt::Display for LinkOutputKind {
430430
}
431431
}
432432

433-
pub enum LoadTargetError {
434-
BuiltinTargetNotFound(String),
435-
Other(String),
436-
}
437-
438433
pub type LinkArgs = BTreeMap<LinkerFlavor, Vec<String>>;
439434

440435
macro_rules! supported_targets {
441436
( $(($( $triple:literal, )+ $module:ident ),)+ ) => {
442437
$(mod $module;)+
443438

444439
/// List of supported targets
445-
const TARGETS: &[&str] = &[$($($triple),+),+];
446-
447-
fn load_specific(target: &str) -> Result<Target, LoadTargetError> {
448-
match target {
449-
$(
450-
$($triple)|+ => {
451-
let mut t = $module::target();
452-
t.options.is_builtin = true;
453-
454-
// round-trip through the JSON parser to ensure at
455-
// run-time that the parser works correctly
456-
t = Target::from_json(t.to_json())
457-
.map_err(LoadTargetError::Other)?;
458-
debug!("got builtin target: {:?}", t);
459-
Ok(t)
460-
},
461-
)+
462-
_ => Err(LoadTargetError::BuiltinTargetNotFound(
463-
format!("Unable to find target: {}", target)))
464-
}
465-
}
466-
467-
pub fn get_targets() -> impl Iterator<Item = String> {
468-
TARGETS.iter().filter_map(|t| -> Option<String> {
469-
load_specific(t)
470-
.and(Ok(t.to_string()))
471-
.ok()
472-
})
440+
pub const TARGETS: &[&str] = &[$($($triple),+),+];
441+
442+
fn load_builtin(target: &str) -> Option<Target> {
443+
let mut t = match target {
444+
$( $($triple)|+ => $module::target(), )+
445+
_ => return None,
446+
};
447+
t.options.is_builtin = true;
448+
debug!("got builtin target: {:?}", t);
449+
Some(t)
473450
}
474451

475452
#[cfg(test)]
@@ -1529,11 +1506,9 @@ impl Target {
15291506

15301507
match *target_triple {
15311508
TargetTriple::TargetTriple(ref target_triple) => {
1532-
// check if triple is in list of supported targets
1533-
match load_specific(target_triple) {
1534-
Ok(t) => return Ok(t),
1535-
Err(LoadTargetError::BuiltinTargetNotFound(_)) => (),
1536-
Err(LoadTargetError::Other(e)) => return Err(e),
1509+
// check if triple is in list of built-in targets
1510+
if let Some(t) = load_builtin(target_triple) {
1511+
return Ok(t);
15371512
}
15381513

15391514
// search for a file named `target_triple`.json in RUST_TARGET_PATH

0 commit comments

Comments
 (0)