Skip to content

Commit

Permalink
Auto merge of rust-lang#134720 - malezjaa:feat/crate-type-valid-value…
Browse files Browse the repository at this point in the history
…s, 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
  • Loading branch information
bors committed Dec 25, 2024
2 parents 7c002ff + b56ec2f commit a1a97f8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
30 changes: 29 additions & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -2678,7 +2701,12 @@ pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateTy
"cdylib" => 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)
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/crate_type_flag.rs
Original file line number Diff line number Diff line change
@@ -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() {}
2 changes: 2 additions & 0 deletions tests/ui/crate_type_flag.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: unknown crate type `dynlib`, expected one of: `bin`, `cdylib`, `dylib`, `lib`, `proc-macro`, `rlib`

0 comments on commit a1a97f8

Please sign in to comment.