Skip to content

Commit 36c639a

Browse files
committed
Convenience funcs for some_option.unwrap_or(...)
This ensures consistent handling of default values for options that are None if not specified on the command line.
1 parent 4f550f1 commit 36c639a

File tree

15 files changed

+29
-55
lines changed

15 files changed

+29
-55
lines changed

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ fn test_debugging_options_tracking_hash() {
561561
tracked!(link_only, true);
562562
tracked!(merge_functions, Some(MergeFunctions::Disabled));
563563
tracked!(mir_emit_retag, true);
564-
tracked!(mir_opt_level, Some(3));
564+
tracked!(mir_opt_level, 3);
565565
tracked!(mutable_noalias, true);
566566
tracked!(new_llvm_pass_manager, true);
567567
tracked!(no_codegen, true);

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
663663
no_builtins: tcx.sess.contains_name(&attrs, sym::no_builtins),
664664
panic_runtime: tcx.sess.contains_name(&attrs, sym::panic_runtime),
665665
profiler_runtime: tcx.sess.contains_name(&attrs, sym::profiler_runtime),
666-
symbol_mangling_version: tcx
667-
.sess
668-
.opts
669-
.debugging_opts
670-
.symbol_mangling_version
671-
.unwrap_or(SymbolManglingVersion::default()),
666+
symbol_mangling_version: tcx.sess.opts.debugging_opts.get_symbol_mangling_version(),
672667

673668
crate_deps,
674669
dylib_dependency_formats,

compiler/rustc_mir/src/transform/const_prop.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use rustc_middle::ty::subst::{InternalSubsts, Subst};
2222
use rustc_middle::ty::{
2323
self, ConstInt, ConstKind, Instance, ParamEnv, ScalarInt, Ty, TyCtxt, TypeFoldable,
2424
};
25-
use rustc_session::config::MIR_OPT_LEVEL_DEFAULT;
2625
use rustc_session::lint;
2726
use rustc_span::{def_id::DefId, Span};
2827
use rustc_target::abi::{HasDataLayout, LayoutOf, Size, TargetDataLayout};
@@ -709,7 +708,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
709708
return None;
710709
}
711710

712-
if self.tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT) >= 3 {
711+
if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 3 {
713712
self.eval_rvalue_with_identities(rvalue, place)
714713
} else {
715714
self.use_ecx(|this| this.ecx.eval_rvalue_into_place(rvalue, place))
@@ -887,8 +886,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
887886

888887
/// Returns `true` if and only if this `op` should be const-propagated into.
889888
fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
890-
let mir_opt_level =
891-
self.tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT);
889+
let mir_opt_level = self.tcx.sess.opts.debugging_opts.mir_opt_level;
892890

893891
if mir_opt_level == 0 {
894892
return false;
@@ -1058,7 +1056,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
10581056

10591057
// Only const prop copies and moves on `mir_opt_level=2` as doing so
10601058
// currently slightly increases compile time in some cases.
1061-
if self.tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT) >= 2 {
1059+
if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
10621060
self.propagate_operand(operand)
10631061
}
10641062
}

compiler/rustc_mir/src/transform/dest_prop.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ use rustc_middle::mir::{
115115
Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
116116
};
117117
use rustc_middle::ty::{self, Ty, TyCtxt};
118-
use rustc_session::config::MIR_OPT_LEVEL_DEFAULT;
119118

120119
// Empirical measurements have resulted in some observations:
121120
// - Running on a body with a single block and 500 locals takes barely any time
@@ -130,7 +129,7 @@ impl<'tcx> MirPass<'tcx> for DestinationPropagation {
130129
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
131130
// Only run at mir-opt-level=2 or higher for now (we don't fix up debuginfo and remove
132131
// storage statements at the moment).
133-
if tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT) <= 1 {
132+
if tcx.sess.opts.debugging_opts.mir_opt_level <= 1 {
134133
return;
135134
}
136135

compiler/rustc_mir/src/transform/early_otherwise_branch.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{transform::MirPass, util::patch::MirPatch};
22
use rustc_middle::mir::*;
33
use rustc_middle::ty::{Ty, TyCtxt};
4-
use rustc_session::config::MIR_OPT_LEVEL_DEFAULT;
54
use std::fmt::Debug;
65

76
use super::simplify::simplify_cfg;
@@ -27,7 +26,7 @@ pub struct EarlyOtherwiseBranch;
2726

2827
impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
2928
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
30-
if tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT) < 2 {
29+
if tcx.sess.opts.debugging_opts.mir_opt_level < 2 {
3130
return;
3231
}
3332
trace!("running EarlyOtherwiseBranch on {:?}", body.source);

compiler/rustc_mir/src/transform/inline.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_middle::mir::visit::*;
99
use rustc_middle::mir::*;
1010
use rustc_middle::ty::subst::Subst;
1111
use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
12-
use rustc_session::config::MIR_OPT_LEVEL_DEFAULT;
1312
use rustc_span::{hygiene::ExpnKind, ExpnData, Span};
1413
use rustc_target::spec::abi::Abi;
1514

@@ -38,7 +37,7 @@ struct CallSite<'tcx> {
3837

3938
impl<'tcx> MirPass<'tcx> for Inline {
4039
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
41-
if tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT) < 2 {
40+
if tcx.sess.opts.debugging_opts.mir_opt_level < 2 {
4241
return;
4342
}
4443

compiler/rustc_mir/src/transform/match_branches.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::transform::MirPass;
22
use rustc_middle::mir::*;
33
use rustc_middle::ty::TyCtxt;
4-
use rustc_session::config::MIR_OPT_LEVEL_DEFAULT;
54

65
pub struct MatchBranchSimplification;
76

@@ -39,7 +38,7 @@ pub struct MatchBranchSimplification;
3938
4039
impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
4140
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
42-
if tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT) <= 1 {
41+
if tcx.sess.opts.debugging_opts.mir_opt_level <= 1 {
4342
return;
4443
}
4544

compiler/rustc_mir/src/transform/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_middle::mir::visit::Visitor as _;
1010
use rustc_middle::mir::{traversal, Body, ConstQualifs, MirPhase, Promoted};
1111
use rustc_middle::ty::query::Providers;
1212
use rustc_middle::ty::{self, TyCtxt, TypeFoldable};
13-
use rustc_session::config::MIR_OPT_LEVEL_DEFAULT;
1413
use rustc_span::{Span, Symbol};
1514
use std::borrow::Cow;
1615

@@ -374,7 +373,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
374373
}
375374

376375
fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
377-
let mir_opt_level = tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT);
376+
let mir_opt_level = tcx.sess.opts.debugging_opts.mir_opt_level;
378377

379378
// Lowering generator control-flow and variables has to happen before we do anything else
380379
// to them. We run some optimizations before that, because they may be harder to do on the state

compiler/rustc_mir/src/transform/multiple_return_terminators.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ use crate::transform::{simplify, MirPass};
55
use rustc_index::bit_set::BitSet;
66
use rustc_middle::mir::*;
77
use rustc_middle::ty::TyCtxt;
8-
use rustc_session::config::MIR_OPT_LEVEL_DEFAULT;
98

109
pub struct MultipleReturnTerminators;
1110

1211
impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
1312
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
14-
if tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT) < 3 {
13+
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 {
1514
return;
1615
}
1716

compiler/rustc_mir/src/transform/nrvo.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_index::bit_set::HybridBitSet;
55
use rustc_middle::mir::visit::{MutVisitor, NonUseContext, PlaceContext, Visitor};
66
use rustc_middle::mir::{self, BasicBlock, Local, Location};
77
use rustc_middle::ty::TyCtxt;
8-
use rustc_session::config::MIR_OPT_LEVEL_DEFAULT;
98

109
use crate::transform::MirPass;
1110

@@ -35,7 +34,7 @@ pub struct RenameReturnPlace;
3534

3635
impl<'tcx> MirPass<'tcx> for RenameReturnPlace {
3736
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {
38-
if tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT) == 0 {
37+
if tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
3938
return;
4039
}
4140

compiler/rustc_mir/src/transform/unreachable_prop.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ use crate::transform::MirPass;
77
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
88
use rustc_middle::mir::*;
99
use rustc_middle::ty::TyCtxt;
10-
use rustc_session::config::MIR_OPT_LEVEL_DEFAULT;
1110

1211
pub struct UnreachablePropagation;
1312

1413
impl MirPass<'_> for UnreachablePropagation {
1514
fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
16-
if tcx.sess.opts.debugging_opts.mir_opt_level.unwrap_or(MIR_OPT_LEVEL_DEFAULT) < 3 {
15+
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 {
1716
// Enable only under -Zmir-opt-level=3 as in some cases (check the deeply-nested-opt
1817
// perf benchmark) LLVM may spend quite a lot of time optimizing the generated code.
1918
return;

compiler/rustc_session/src/config.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ pub enum MirSpanview {
174174
Block,
175175
}
176176

177-
pub const MIR_OPT_LEVEL_DEFAULT: usize = 1;
178-
179177
#[derive(Clone, PartialEq, Hash)]
180178
pub enum LinkerPluginLto {
181179
LinkerPlugin(PathBuf),
@@ -214,12 +212,6 @@ pub enum SymbolManglingVersion {
214212
V0,
215213
}
216214

217-
impl SymbolManglingVersion {
218-
pub fn default() -> Self {
219-
SymbolManglingVersion::Legacy
220-
}
221-
}
222-
223215
impl_stable_hash_via_hash!(SymbolManglingVersion);
224216

225217
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
@@ -700,6 +692,10 @@ impl DebuggingOptions {
700692
deduplicate_diagnostics: self.deduplicate_diagnostics,
701693
}
702694
}
695+
696+
pub fn get_symbol_mangling_version(&self) -> SymbolManglingVersion {
697+
self.symbol_mangling_version.unwrap_or(SymbolManglingVersion::Legacy)
698+
}
703699
}
704700

705701
// The type of entry function, so users can have their own entry functions
@@ -1779,18 +1775,15 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
17791775
Some(SymbolManglingVersion::V0) => {}
17801776
}
17811777

1782-
match debugging_opts.mir_opt_level {
1783-
Some(level) if level > 1 => {
1784-
early_warn(
1785-
error_format,
1786-
&format!(
1787-
"`-Z mir-opt-level={}` (any level > 1) enables function inlining, which \
1788-
limits the effectiveness of `-Z instrument-coverage`.",
1789-
level,
1790-
),
1791-
);
1792-
}
1793-
_ => {}
1778+
if debugging_opts.mir_opt_level > 1 {
1779+
early_warn(
1780+
error_format,
1781+
&format!(
1782+
"`-Z mir-opt-level={}` (any level > 1) enables function inlining, which \
1783+
limits the effectiveness of `-Z instrument-coverage`.",
1784+
debugging_opts.mir_opt_level,
1785+
),
1786+
);
17941787
}
17951788
}
17961789

compiler/rustc_session/src/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
970970
mir_emit_retag: bool = (false, parse_bool, [TRACKED],
971971
"emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
972972
(default: no)"),
973-
mir_opt_level: Option<usize> = (None, parse_opt_uint, [TRACKED],
973+
mir_opt_level: usize = (1, parse_uint, [TRACKED],
974974
"MIR optimization level (0-3; default: 1)"),
975975
mutable_noalias: bool = (false, parse_bool, [TRACKED],
976976
"emit noalias metadata for mutable references (default: no)"),

compiler/rustc_symbol_mangling/src/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,7 @@ fn compute_symbol_name(
245245
// 2. we favor `instantiating_crate` where possible (i.e. when `Some`)
246246
let mangling_version_crate = instantiating_crate.unwrap_or(def_id.krate);
247247
let mangling_version = if mangling_version_crate == LOCAL_CRATE {
248-
tcx.sess
249-
.opts
250-
.debugging_opts
251-
.symbol_mangling_version
252-
.unwrap_or(SymbolManglingVersion::default())
248+
tcx.sess.opts.debugging_opts.get_symbol_mangling_version()
253249
} else {
254250
tcx.symbol_mangling_version(mangling_version_crate)
255251
};

src/tools/clippy/src/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
8585
// run on the unoptimized MIR. On the other hand this results in some false negatives. If
8686
// MIR passes can be enabled / disabled separately, we should figure out, what passes to
8787
// use for Clippy.
88-
config.opts.debugging_opts.mir_opt_level = Some(0);
88+
config.opts.debugging_opts.mir_opt_level = 0;
8989
}
9090
}
9191

0 commit comments

Comments
 (0)