Skip to content

Commit 1137762

Browse files
committed
Suggest similar target names on unrecognized --target
1 parent b6100cc commit 1137762

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

compiler/rustc_session/src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,19 @@ pub fn build_target_config(
16151615
let mut err =
16161616
early_dcx.early_struct_fatal(format!("error loading target specification: {e}"));
16171617
err.help("run `rustc --print target-list` for a list of built-in targets");
1618+
let typed = target.tuple();
1619+
let limit = typed.len() / 3 + 1;
1620+
if let Some(suggestion) = rustc_target::spec::TARGETS
1621+
.iter()
1622+
.filter_map(|&t| {
1623+
rustc_span::edit_distance::edit_distance_with_substrings(typed, t, limit)
1624+
.map(|d| (d, t))
1625+
})
1626+
.min_by_key(|(d, _)| *d)
1627+
.map(|(_, t)| t)
1628+
{
1629+
err.help(format!("did you mean `{suggestion}`?"));
1630+
}
16181631
err.emit()
16191632
}
16201633
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Checks that an unknown --target also suggests a similar known target.
2+
// See https://github.com/rust-lang/rust/issues/155085
3+
4+
// ignore-tidy-target-specific-tests
5+
//@ compile-flags: --target x86_64-linux-gnu
6+
7+
fn main() {}
8+
9+
//~? ERROR error loading target specification: could not find specification for target "x86_64-linux-gnu"
10+
//~? HELP run `rustc --print target-list` for a list of built-in targets
11+
//~? HELP did you mean `x86_64-unknown-linux-gnu`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: error loading target specification: could not find specification for target "x86_64-linux-gnu"
2+
|
3+
= help: run `rustc --print target-list` for a list of built-in targets
4+
= help: did you mean `x86_64-unknown-linux-gnu`?
5+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
error: error loading target specification: could not find specification for target "x86_64_unknown-linux-musl"
22
|
33
= help: run `rustc --print target-list` for a list of built-in targets
4+
= help: did you mean `x86_64-unknown-linux-musl`?
45

0 commit comments

Comments
 (0)