Skip to content

Commit e64dbb1

Browse files
authored
Rollup merge of #82483 - tmiasko:option-from-str, r=matthewjasper
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.
2 parents 54ea8e1 + 1ec9057 commit e64dbb1

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ pub unsafe fn with_llvm_pmb(
10471047
// thresholds copied from clang.
10481048
match (opt_level, opt_size, inline_threshold) {
10491049
(.., Some(t)) => {
1050-
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, t as u32);
1050+
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, t);
10511051
}
10521052
(llvm::CodeGenOptLevel::Aggressive, ..) => {
10531053
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 275);

compiler/rustc_codegen_ssa/src/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub struct ModuleConfig {
107107
pub vectorize_loop: bool,
108108
pub vectorize_slp: bool,
109109
pub merge_functions: bool,
110-
pub inline_threshold: Option<usize>,
110+
pub inline_threshold: Option<u32>,
111111
pub new_llvm_pass_manager: bool,
112112
pub emit_lifetime_markers: bool,
113113
}

compiler/rustc_session/src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,7 @@ crate mod dep_tracking {
23322332
impl_dep_tracking_hash_via_hash!(PathBuf);
23332333
impl_dep_tracking_hash_via_hash!(lint::Level);
23342334
impl_dep_tracking_hash_via_hash!(Option<bool>);
2335+
impl_dep_tracking_hash_via_hash!(Option<u32>);
23352336
impl_dep_tracking_hash_via_hash!(Option<usize>);
23362337
impl_dep_tracking_hash_via_hash!(Option<NonZeroUsize>);
23372338
impl_dep_tracking_hash_via_hash!(Option<String>);

compiler/rustc_session/src/options.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ macro_rules! options {
251251
pub const parse_list: &str = "a space-separated list of strings";
252252
pub const parse_opt_list: &str = parse_list;
253253
pub const parse_opt_comma_list: &str = "a comma-separated list of strings";
254-
pub const parse_uint: &str = "a number";
255-
pub const parse_opt_uint: &str = parse_uint;
256-
pub const parse_threads: &str = parse_uint;
254+
pub const parse_number: &str = "a number";
255+
pub const parse_opt_number: &str = parse_number;
256+
pub const parse_threads: &str = parse_number;
257257
pub const parse_passes: &str = "a space-separated list of passes, or `all`";
258258
pub const parse_panic_strategy: &str = "either `unwind` or `abort`";
259259
pub const parse_relro_level: &str = "one of: `full`, `partial`, or `off`";
@@ -417,16 +417,16 @@ macro_rules! options {
417417
}
418418
}
419419

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

428-
/// Use this for any uint option that lacks a static default.
429-
fn parse_opt_uint(slot: &mut Option<usize>, v: Option<&str>) -> bool {
428+
/// Use this for any numeric option that lacks a static default.
429+
fn parse_opt_number<T: Copy + FromStr>(slot: &mut Option<T>, v: Option<&str>) -> bool {
430430
match v {
431431
Some(s) => { *slot = s.parse().ok(); slot.is_some() }
432432
None => false
@@ -787,13 +787,13 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
787787
"this option is deprecated and does nothing"),
788788
code_model: Option<CodeModel> = (None, parse_code_model, [TRACKED],
789789
"choose the code model to use (`rustc --print code-models` for details)"),
790-
codegen_units: Option<usize> = (None, parse_opt_uint, [UNTRACKED],
790+
codegen_units: Option<usize> = (None, parse_opt_number, [UNTRACKED],
791791
"divide crate into N units to optimize in parallel"),
792792
control_flow_guard: CFGuard = (CFGuard::Disabled, parse_cfguard, [TRACKED],
793793
"use Windows Control Flow Guard (default: no)"),
794794
debug_assertions: Option<bool> = (None, parse_opt_bool, [TRACKED],
795795
"explicitly enable the `cfg(debug_assertions)` directive"),
796-
debuginfo: usize = (0, parse_uint, [TRACKED],
796+
debuginfo: usize = (0, parse_number, [TRACKED],
797797
"debug info emission level (0 = no debug info, 1 = line tables only, \
798798
2 = full debug info with variable and type information; default: 0)"),
799799
default_linker_libraries: bool = (false, parse_bool, [UNTRACKED],
@@ -808,7 +808,7 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
808808
"force use of unwind tables"),
809809
incremental: Option<String> = (None, parse_opt_string, [UNTRACKED],
810810
"enable incremental compilation"),
811-
inline_threshold: Option<usize> = (None, parse_opt_uint, [TRACKED],
811+
inline_threshold: Option<u32> = (None, parse_opt_number, [TRACKED],
812812
"set the threshold for inlining a function"),
813813
link_arg: (/* redirected to link_args */) = ((), parse_string_push, [UNTRACKED],
814814
"a single extra argument to append to the linker invocation (can be used several times)"),
@@ -996,9 +996,9 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
996996
"verify incr. comp. hashes of green query instances (default: no)"),
997997
inline_mir: Option<bool> = (None, parse_opt_bool, [TRACKED],
998998
"enable MIR inlining (default: no)"),
999-
inline_mir_threshold: Option<usize> = (None, parse_opt_uint, [TRACKED],
999+
inline_mir_threshold: Option<usize> = (None, parse_opt_number, [TRACKED],
10001000
"a default MIR inlining threshold (default: 50)"),
1001-
inline_mir_hint_threshold: Option<usize> = (None, parse_opt_uint, [TRACKED],
1001+
inline_mir_hint_threshold: Option<usize> = (None, parse_opt_number, [TRACKED],
10021002
"inlining threshold for functions with inline hint (default: 100)"),
10031003
inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED],
10041004
"control whether `#[inline]` functions are in all CGUs"),
@@ -1034,7 +1034,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
10341034
mir_emit_retag: bool = (false, parse_bool, [TRACKED],
10351035
"emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
10361036
(default: no)"),
1037-
mir_opt_level: Option<usize> = (None, parse_opt_uint, [TRACKED],
1037+
mir_opt_level: Option<usize> = (None, parse_opt_number, [TRACKED],
10381038
"MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"),
10391039
mutable_noalias: Option<bool> = (None, parse_opt_bool, [TRACKED],
10401040
"emit noalias metadata for mutable references (default: yes for LLVM >= 12, otherwise no)"),
@@ -1155,7 +1155,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
11551155
"which mangling version to use for symbol names ('legacy' (default) or 'v0')"),
11561156
teach: bool = (false, parse_bool, [TRACKED],
11571157
"show extended diagnostic help (default: no)"),
1158-
terminal_width: Option<usize> = (None, parse_opt_uint, [UNTRACKED],
1158+
terminal_width: Option<usize> = (None, parse_opt_number, [UNTRACKED],
11591159
"set the current terminal width"),
11601160
tune_cpu: Option<String> = (None, parse_opt_string, [TRACKED],
11611161
"select processor to schedule for (`rustc --print target-cpus` for details)"),

0 commit comments

Comments
 (0)