Skip to content

Commit da80a6b

Browse files
committed
make OptionDesc struct from tuple
1 parent 6f74f0f commit da80a6b

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

compiler/rustc_driver_impl/src/lib.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ use rustc_metadata::locator;
5252
use rustc_middle::ty::TyCtxt;
5353
use rustc_parse::{new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal};
5454
use rustc_session::config::{
55-
CG_OPTIONS, ErrorOutputType, Input, OutFileName, OutputType, UnstableOptions, Z_OPTIONS,
56-
nightly_options,
55+
CG_OPTIONS, ErrorOutputType, Input, OptionDesc, OutFileName, OutputType, UnstableOptions,
56+
Z_OPTIONS, nightly_options,
5757
};
5858
use rustc_session::getopts::{self, Matches};
5959
use rustc_session::lint::{Lint, LintId};
@@ -1148,19 +1148,16 @@ fn describe_codegen_flags() {
11481148
print_flag_list("-C", config::CG_OPTIONS);
11491149
}
11501150

1151-
fn print_flag_list<T>(
1152-
cmdline_opt: &str,
1153-
flag_list: &[(&'static str, T, &'static str, &'static str, bool)],
1154-
) {
1151+
fn print_flag_list<T>(cmdline_opt: &str, flag_list: &[OptionDesc<T>]) {
11551152
let max_len =
1156-
flag_list.iter().map(|&(name, _, _, _, _)| name.chars().count()).max().unwrap_or(0);
1153+
flag_list.iter().map(|opt_desc| opt_desc.name().chars().count()).max().unwrap_or(0);
11571154

1158-
for &(name, _, _, desc, _) in flag_list {
1155+
for opt_desc in flag_list {
11591156
safe_println!(
11601157
" {} {:>width$}=val -- {}",
11611158
cmdline_opt,
1162-
name.replace('_', "-"),
1163-
desc,
1159+
opt_desc.name().replace('_', "-"),
1160+
opt_desc.desc(),
11641161
width = max_len
11651162
);
11661163
}
@@ -1214,8 +1211,8 @@ pub fn handle_options(early_dcx: &EarlyDiagCtxt, args: &[String]) -> Option<geto
12141211
let msg: Option<String> = match e {
12151212
getopts::Fail::UnrecognizedOption(ref opt) => CG_OPTIONS
12161213
.iter()
1217-
.map(|&(name, ..)| ('C', name))
1218-
.chain(Z_OPTIONS.iter().map(|&(name, ..)| ('Z', name)))
1214+
.map(|opt_desc| ('C', opt_desc.name()))
1215+
.chain(Z_OPTIONS.iter().map(|opt_desc| ('Z', opt_desc.name())))
12191216
.find(|&(_, name)| *opt == name.replace('_', "-"))
12201217
.map(|(flag, _)| format!("{e}. Did you mean `-{flag} {opt}`?")),
12211218
getopts::Fail::ArgumentMissing(ref opt) => {

compiler/rustc_session/src/options.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ macro_rules! options {
281281
}
282282

283283
pub const $stat: OptionDescrs<$struct_name> =
284-
&[ $( (stringify!($opt), $optmod::$opt, desc::$parse, $desc, false $( || $dnn )? ) ),* ];
284+
&[ $( OptionDesc{ name: stringify!($opt), setter: $optmod::$opt,
285+
type_desc: desc::$parse, desc: $desc, is_deprecated_and_do_nothing: false $( || $dnn )? } ),* ];
285286

286287
mod $optmod {
287288
$(
@@ -316,8 +317,27 @@ macro_rules! redirect_field {
316317
}
317318

318319
type OptionSetter<O> = fn(&mut O, v: Option<&str>) -> bool;
319-
type OptionDescrs<O> =
320-
&'static [(&'static str, OptionSetter<O>, &'static str, &'static str, bool)];
320+
type OptionDescrs<O> = &'static [OptionDesc<O>];
321+
322+
pub struct OptionDesc<O> {
323+
name: &'static str,
324+
setter: OptionSetter<O>,
325+
// description for return value/type from mod desc
326+
type_desc: &'static str,
327+
// description for option from options table
328+
desc: &'static str,
329+
is_deprecated_and_do_nothing: bool,
330+
}
331+
332+
impl<O> OptionDesc<O> {
333+
pub fn name(&self) -> &'static str {
334+
self.name
335+
}
336+
337+
pub fn desc(&self) -> &'static str {
338+
self.desc
339+
}
340+
}
321341

322342
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
323343
fn build_options<O: Default>(
@@ -335,12 +355,12 @@ fn build_options<O: Default>(
335355
};
336356

337357
let option_to_lookup = key.replace('-', "_");
338-
match descrs.iter().find(|(name, ..)| *name == option_to_lookup) {
339-
Some((_, setter, type_desc, opt_desc, deprecated_do_nothing)) => {
340-
if *deprecated_do_nothing {
358+
match descrs.iter().find(|opt_desc| opt_desc.name == option_to_lookup) {
359+
Some(OptionDesc { name: _, setter, type_desc, desc, is_deprecated_and_do_nothing }) => {
360+
if *is_deprecated_and_do_nothing {
341361
// deprecation works for prefixed options only
342362
assert!(!prefix.is_empty());
343-
early_dcx.early_warn(format!("`-{prefix} {key}`: {opt_desc}"));
363+
early_dcx.early_warn(format!("`-{prefix} {key}`: {desc}"));
344364
}
345365
if !setter(&mut op, value) {
346366
match value {

0 commit comments

Comments
 (0)