From c3ea358121e493ace14308e153b70692439fb154 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 5 Nov 2017 09:20:59 -0500 Subject: [PATCH] Display all emission types in error msg if user inputs invalid option. before: ``` > rustc --emit foo error: unknown emission type: `foo` ``` after: ``` > rustc --emit foo error: unknown emission type: `foo` - expected one of: `llvm-bc`, `asm`, `llvm-ir`, `mir`, `obj`, `metadata`, `link`, `dep-info` ``` --- src/librustc/session/config.rs | 48 +++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 8401d493b9d..a27371926de 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -138,6 +138,34 @@ fn shorthand(&self) -> &'static str { } } + fn from_shorthand(shorthand: &str) -> Option { + Some(match shorthand { + "asm" => OutputType::Assembly, + "llvm-ir" => OutputType::LlvmAssembly, + "mir" => OutputType::Mir, + "llvm-bc" => OutputType::Bitcode, + "obj" => OutputType::Object, + "metadata" => OutputType::Metadata, + "link" => OutputType::Exe, + "dep-info" => OutputType::DepInfo, + _ => return None, + }) + } + + fn shorthands_display() -> String { + format!( + "`{}`, `{}`, `{}`, `{}`, `{}`, `{}`, `{}`, `{}`", + OutputType::Bitcode.shorthand(), + OutputType::Assembly.shorthand(), + OutputType::LlvmAssembly.shorthand(), + OutputType::Mir.shorthand(), + OutputType::Object.shorthand(), + OutputType::Metadata.shorthand(), + OutputType::Exe.shorthand(), + OutputType::DepInfo.shorthand(), + ) + } + pub fn extension(&self) -> &'static str { match *self { OutputType::Bitcode => "bc", @@ -1479,19 +1507,13 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches) for list in matches.opt_strs("emit") { for output_type in list.split(',') { let mut parts = output_type.splitn(2, '='); - let output_type = match parts.next().unwrap() { - "asm" => OutputType::Assembly, - "llvm-ir" => OutputType::LlvmAssembly, - "mir" => OutputType::Mir, - "llvm-bc" => OutputType::Bitcode, - "obj" => OutputType::Object, - "metadata" => OutputType::Metadata, - "link" => OutputType::Exe, - "dep-info" => OutputType::DepInfo, - part => { - early_error(error_format, &format!("unknown emission type: `{}`", - part)) - } + let shorthand = parts.next().unwrap(); + let output_type = match OutputType::from_shorthand(shorthand) { + Some(output_type) => output_type, + None => early_error(error_format, &format!( + "unknown emission type: `{}` - expected one of: {}", + shorthand, OutputType::shorthands_display(), + )), }; let path = parts.next().map(PathBuf::from); output_types.insert(output_type, path); -- 2.44.0