From b56ec2f7e557be973677222ca28a28306bbf9c0e Mon Sep 17 00:00:00 2001 From: malezjaa Date: Tue, 24 Dec 2024 10:41:38 +0100 Subject: [PATCH] Display valid values in --crate-type --- compiler/rustc_session/src/config.rs | 30 +++++++++++++++++++++++++++- tests/ui/crate_type_flag.rs | 4 ++++ tests/ui/crate_type_flag.stderr | 2 ++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/ui/crate_type_flag.rs create mode 100644 tests/ui/crate_type_flag.stderr diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 5c36c98649020..88627b6236af9 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1296,6 +1296,29 @@ impl CrateType { CrateType::Executable | CrateType::Cdylib | CrateType::Staticlib => false, } } + + pub fn shorthand(&self) -> &'static str { + match *self { + CrateType::Executable => "bin", + CrateType::Dylib => "dylib", + CrateType::Rlib => "rlib", + CrateType::Staticlib => "lib", + CrateType::Cdylib => "cdylib", + CrateType::ProcMacro => "proc-macro", + } + } + + fn shorthands_display() -> String { + format!( + "`{}`, `{}`, `{}`, `{}`, `{}`, `{}`", + CrateType::Executable.shorthand(), + CrateType::Cdylib.shorthand(), + CrateType::Dylib.shorthand(), + CrateType::Staticlib.shorthand(), + CrateType::ProcMacro.shorthand(), + CrateType::Rlib.shorthand(), + ) + } } #[derive(Clone, Hash, Debug, PartialEq, Eq)] @@ -2678,7 +2701,12 @@ pub fn parse_crate_types_from_list(list_list: Vec) -> Result CrateType::Cdylib, "bin" => CrateType::Executable, "proc-macro" => CrateType::ProcMacro, - _ => return Err(format!("unknown crate type: `{part}`")), + _ => { + return Err(format!( + "unknown crate type `{part}`, expected one of: {display}", + display = CrateType::shorthands_display() + )); + } }; if !crate_types.contains(&new_part) { crate_types.push(new_part) diff --git a/tests/ui/crate_type_flag.rs b/tests/ui/crate_type_flag.rs new file mode 100644 index 0000000000000..afc0fdba1b977 --- /dev/null +++ b/tests/ui/crate_type_flag.rs @@ -0,0 +1,4 @@ +//@ compile-flags: --crate-type dynlib +//@ error-pattern: unknown crate type `dynlib`, expected one of: `bin`, `cdylib`, `dylib`, `lib`, `proc-macro`, `rlib` + +fn main() {} diff --git a/tests/ui/crate_type_flag.stderr b/tests/ui/crate_type_flag.stderr new file mode 100644 index 0000000000000..20fbc71388168 --- /dev/null +++ b/tests/ui/crate_type_flag.stderr @@ -0,0 +1,2 @@ +error: unknown crate type `dynlib`, expected one of: `bin`, `cdylib`, `dylib`, `lib`, `proc-macro`, `rlib` +