Skip to content

Commit 1ec9057

Browse files
committed
Use FromStr trait for number option parsing
Replace `parse_uint` with generic `parse_number` based on `FromStr`. Use it for parsing inlining threshold to avoid casting later.
1 parent 3a5d45f commit 1ec9057

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ pub unsafe fn with_llvm_pmb(
10441044
// thresholds copied from clang.
10451045
match (opt_level, opt_size, inline_threshold) {
10461046
(.., Some(t)) => {
1047-
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, t as u32);
1047+
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, t);
10481048
}
10491049
(llvm::CodeGenOptLevel::Aggressive, ..) => {
10501050
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 275);

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub struct ModuleConfig {
106106
pub vectorize_loop: bool,
107107
pub vectorize_slp: bool,
108108
pub merge_functions: bool,
109-
pub inline_threshold: Option<usize>,
109+
pub inline_threshold: Option<u32>,
110110
pub new_llvm_pass_manager: bool,
111111
pub emit_lifetime_markers: bool,
112112
}

compiler/rustc_session/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,7 @@ crate mod dep_tracking {
23672367
impl_dep_tracking_hash_via_hash!(PathBuf);
23682368
impl_dep_tracking_hash_via_hash!(lint::Level);
23692369
impl_dep_tracking_hash_via_hash!(Option<bool>);
2370+
impl_dep_tracking_hash_via_hash!(Option<u32>);
23702371
impl_dep_tracking_hash_via_hash!(Option<usize>);
23712372
impl_dep_tracking_hash_via_hash!(Option<NonZeroUsize>);
23722373
impl_dep_tracking_hash_via_hash!(Option<String>);

compiler/rustc_session/src/options.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ macro_rules! options {
248248
pub const parse_list: &str = "a space-separated list of strings";
249249
pub const parse_opt_list: &str = parse_list;
250250
pub const parse_opt_comma_list: &str = "a comma-separated list of strings";
251-
pub const parse_uint: &str = "a number";
252-
pub const parse_opt_uint: &str = parse_uint;
253-
pub const parse_threads: &str = parse_uint;
251+
pub const parse_number: &str = "a number";
252+
pub const parse_opt_number: &str = parse_number;
253+
pub const parse_threads: &str = parse_number;
254254
pub const parse_passes: &str = "a space-separated list of passes, or `all`";
255255
pub const parse_panic_strategy: &str = "either `unwind` or `abort`";
256256
pub const parse_relro_level: &str = "one of: `full`, `partial`, or `off`";
@@ -413,16 +413,16 @@ macro_rules! options {
413413
}
414414
}
415415

416-
/// Use this for any uint option that has a static default.
417-
fn parse_uint(slot: &mut usize, v: Option<&str>) -> bool {
416+
/// Use this for any numeric option that has a static default.
417+
fn parse_number<T: Copy + FromStr>(slot: &mut T, v: Option<&str>) -> bool {
418418
match v.and_then(|s| s.parse().ok()) {
419419
Some(i) => { *slot = i; true },
420420
None => false
421421
}
422422
}
423423

424-
/// Use this for any uint option that lacks a static default.
425-
fn parse_opt_uint(slot: &mut Option<usize>, v: Option<&str>) -> bool {
424+
/// Use this for any numeric option that lacks a static default.
425+
fn parse_opt_number<T: Copy + FromStr>(slot: &mut Option<T>, v: Option<&str>) -> bool {
426426
match v {
427427
Some(s) => { *slot = s.parse().ok(); slot.is_some() }
428428
None => false
@@ -748,13 +748,13 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
748748
"this option is deprecated and does nothing"),
749749
code_model: Option<CodeModel> = (None, parse_code_model, [TRACKED],
750750
"choose the code model to use (`rustc --print code-models` for details)"),
751-
codegen_units: Option<usize> = (None, parse_opt_uint, [UNTRACKED],
751+
codegen_units: Option<usize> = (None, parse_opt_number, [UNTRACKED],
752752
"divide crate into N units to optimize in parallel"),
753753
control_flow_guard: CFGuard = (CFGuard::Disabled, parse_cfguard, [TRACKED],
754754
"use Windows Control Flow Guard (default: no)"),
755755
debug_assertions: Option<bool> = (None, parse_opt_bool, [TRACKED],
756756
"explicitly enable the `cfg(debug_assertions)` directive"),
757-
debuginfo: usize = (0, parse_uint, [TRACKED],
757+
debuginfo: usize = (0, parse_number, [TRACKED],
758758
"debug info emission level (0 = no debug info, 1 = line tables only, \
759759
2 = full debug info with variable and type information; default: 0)"),
760760
default_linker_libraries: bool = (false, parse_bool, [UNTRACKED],
@@ -769,7 +769,7 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
769769
"force use of unwind tables"),
770770
incremental: Option<String> = (None, parse_opt_string, [UNTRACKED],
771771
"enable incremental compilation"),
772-
inline_threshold: Option<usize> = (None, parse_opt_uint, [TRACKED],
772+
inline_threshold: Option<u32> = (None, parse_opt_number, [TRACKED],
773773
"set the threshold for inlining a function"),
774774
link_arg: (/* redirected to link_args */) = ((), parse_string_push, [UNTRACKED],
775775
"a single extra argument to append to the linker invocation (can be used several times)"),
@@ -959,9 +959,9 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
959959
"verify incr. comp. hashes of green query instances (default: no)"),
960960
inline_mir: Option<bool> = (None, parse_opt_bool, [TRACKED],
961961
"enable MIR inlining (default: no)"),
962-
inline_mir_threshold: Option<usize> = (None, parse_opt_uint, [TRACKED],
962+
inline_mir_threshold: Option<usize> = (None, parse_opt_number, [TRACKED],
963963
"a default MIR inlining threshold (default: 50)"),
964-
inline_mir_hint_threshold: Option<usize> = (None, parse_opt_uint, [TRACKED],
964+
inline_mir_hint_threshold: Option<usize> = (None, parse_opt_number, [TRACKED],
965965
"inlining threshold for functions with inline hint (default: 100)"),
966966
inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED],
967967
"control whether `#[inline]` functions are in all CGUs"),
@@ -999,7 +999,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
999999
mir_emit_retag: bool = (false, parse_bool, [TRACKED],
10001000
"emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
10011001
(default: no)"),
1002-
mir_opt_level: Option<usize> = (None, parse_opt_uint, [TRACKED],
1002+
mir_opt_level: Option<usize> = (None, parse_opt_number, [TRACKED],
10031003
"MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"),
10041004
mutable_noalias: bool = (false, parse_bool, [TRACKED],
10051005
"emit noalias metadata for mutable references (default: no)"),
@@ -1120,7 +1120,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
11201120
"which mangling version to use for symbol names ('legacy' (default) or 'v0')"),
11211121
teach: bool = (false, parse_bool, [TRACKED],
11221122
"show extended diagnostic help (default: no)"),
1123-
terminal_width: Option<usize> = (None, parse_opt_uint, [UNTRACKED],
1123+
terminal_width: Option<usize> = (None, parse_opt_number, [UNTRACKED],
11241124
"set the current terminal width"),
11251125
tune_cpu: Option<String> = (None, parse_opt_string, [TRACKED],
11261126
"select processor to schedule for (`rustc --print target-cpus` for details)"),

0 commit comments

Comments
 (0)