Skip to content

Commit 5756977

Browse files
authored
Rollup merge of #155132 - jakedrew:fix/suggest-similar-target-on-unrecognized-155085, r=Kivooeo
Suggest similar target names on unrecognized `--target` When an unrecognized `--target` is passed check the list of available targets and suggest the closest matching built-in target as a help message. ### Before ``` error: error loading target specification: could not find specification for target "x86_64-linux-gnu" = help: run `rustc --print target-list` for a list of built-in targets ``` ### After ``` error: error loading target specification: could not find specification for target "x86_64-linux-gnu" = help: run `rustc --print target-list` for a list of built-in targets = help: did you mean `x86_64-unknown-linux-gnu`? ``` Regarding the expected test case in #155085 (comment) the `edit_distance_with_substrings` was used over `edit_distance` because in the case of `x86_64-linux-gnu` the `edit_distance` would return `x86_64-linux-android` instead of `x86_64-unknown-linux-gnu` #155085
2 parents 2c0dae2 + 1137762 commit 5756977

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
@@ -1616,6 +1616,19 @@ pub fn build_target_config(
16161616
let mut err =
16171617
early_dcx.early_struct_fatal(format!("error loading target specification: {e}"));
16181618
err.help("run `rustc --print target-list` for a list of built-in targets");
1619+
let typed = target.tuple();
1620+
let limit = typed.len() / 3 + 1;
1621+
if let Some(suggestion) = rustc_target::spec::TARGETS
1622+
.iter()
1623+
.filter_map(|&t| {
1624+
rustc_span::edit_distance::edit_distance_with_substrings(typed, t, limit)
1625+
.map(|d| (d, t))
1626+
})
1627+
.min_by_key(|(d, _)| *d)
1628+
.map(|(_, t)| t)
1629+
{
1630+
err.help(format!("did you mean `{suggestion}`?"));
1631+
}
16191632
err.emit()
16201633
}
16211634
}
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)