Skip to content

Commit a1a97f8

Browse files
committed
Auto merge of rust-lang#134720 - malezjaa:feat/crate-type-valid-values, r=jieyouxu
Display valid crate types in error message for --crate-type flag This PR improves the error message for the --crate-type flag. When an invalid crate type is provided, the compiler will now show a list of valid options. ### Before ![image](https://github.com/user-attachments/assets/4922e4e5-eeca-40cd-ac1c-1c6319a81aee) ### After ![image](https://github.com/user-attachments/assets/67ea1f35-aa41-4e4f-8691-47c273d0cff9) I based the implementation on `OutputType::shorthands_display` Closes rust-lang#70183
2 parents 7c002ff + b56ec2f commit a1a97f8

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

compiler/rustc_session/src/config.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,29 @@ impl CrateType {
12961296
CrateType::Executable | CrateType::Cdylib | CrateType::Staticlib => false,
12971297
}
12981298
}
1299+
1300+
pub fn shorthand(&self) -> &'static str {
1301+
match *self {
1302+
CrateType::Executable => "bin",
1303+
CrateType::Dylib => "dylib",
1304+
CrateType::Rlib => "rlib",
1305+
CrateType::Staticlib => "lib",
1306+
CrateType::Cdylib => "cdylib",
1307+
CrateType::ProcMacro => "proc-macro",
1308+
}
1309+
}
1310+
1311+
fn shorthands_display() -> String {
1312+
format!(
1313+
"`{}`, `{}`, `{}`, `{}`, `{}`, `{}`",
1314+
CrateType::Executable.shorthand(),
1315+
CrateType::Cdylib.shorthand(),
1316+
CrateType::Dylib.shorthand(),
1317+
CrateType::Staticlib.shorthand(),
1318+
CrateType::ProcMacro.shorthand(),
1319+
CrateType::Rlib.shorthand(),
1320+
)
1321+
}
12991322
}
13001323

13011324
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
@@ -2678,7 +2701,12 @@ pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateTy
26782701
"cdylib" => CrateType::Cdylib,
26792702
"bin" => CrateType::Executable,
26802703
"proc-macro" => CrateType::ProcMacro,
2681-
_ => return Err(format!("unknown crate type: `{part}`")),
2704+
_ => {
2705+
return Err(format!(
2706+
"unknown crate type `{part}`, expected one of: {display}",
2707+
display = CrateType::shorthands_display()
2708+
));
2709+
}
26822710
};
26832711
if !crate_types.contains(&new_part) {
26842712
crate_types.push(new_part)

tests/ui/crate_type_flag.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@ compile-flags: --crate-type dynlib
2+
//@ error-pattern: unknown crate type `dynlib`, expected one of: `bin`, `cdylib`, `dylib`, `lib`, `proc-macro`, `rlib`
3+
4+
fn main() {}

tests/ui/crate_type_flag.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: unknown crate type `dynlib`, expected one of: `bin`, `cdylib`, `dylib`, `lib`, `proc-macro`, `rlib`
2+

0 commit comments

Comments
 (0)