Skip to content

Commit

Permalink
Auto merge of #65195 - varkor:to_option, r=<try>
Browse files Browse the repository at this point in the history
Rename `bool::then_*` to `bool::to_option_*` and use where appropriate

Name change following rust-lang/rfcs#2757. Also try it out throughout the compiler in places I think makes the code more readable.
  • Loading branch information
bors committed Oct 20, 2019
2 parents 857a55b + 429435c commit d998a73
Show file tree
Hide file tree
Showing 55 changed files with 95 additions and 256 deletions.
12 changes: 6 additions & 6 deletions src/libcore/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ impl bool {
/// ```
/// #![feature(bool_to_option)]
///
/// assert_eq!(false.then(0), None);
/// assert_eq!(true.then(0), Some(0));
/// assert_eq!(false.to_option(0), None);
/// assert_eq!(true.to_option(0), Some(0));
/// ```
#[unstable(feature = "bool_to_option", issue = "64260")]
#[inline]
pub fn then<T>(self, t: T) -> Option<T> {
pub fn to_option<T>(self, t: T) -> Option<T> {
if self {
Some(t)
} else {
Expand All @@ -29,12 +29,12 @@ impl bool {
/// ```
/// #![feature(bool_to_option)]
///
/// assert_eq!(false.then_with(|| 0), None);
/// assert_eq!(true.then_with(|| 0), Some(0));
/// assert_eq!(false.to_option_with(|| 0), None);
/// assert_eq!(true.to_option_with(|| 0), Some(0));
/// ```
#[unstable(feature = "bool_to_option", issue = "64260")]
#[inline]
pub fn then_with<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
pub fn to_option_with<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
if self {
Some(f())
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/tests/bool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[test]
fn test_bool_to_option() {
assert_eq!(false.then(0), None);
assert_eq!(true.then(0), Some(0));
assert_eq!(false.then_with(|| 0), None);
assert_eq!(true.then_with(|| 0), Some(0));
assert_eq!(false.to_option(0), None);
assert_eq!(true.to_option(0), Some(0));
assert_eq!(false.to_option_with(|| 0), None);
assert_eq!(true.to_option_with(|| 0), Some(0));
}
7 changes: 2 additions & 5 deletions src/libfmt_macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![feature(nll)]
#![feature(rustc_private)]
#![feature(unicode_internals)]
#![feature(bool_to_option)]

pub use Piece::*;
pub use Position::*;
Expand Down Expand Up @@ -633,11 +634,7 @@ impl<'a> Parser<'a> {
break;
}
}
if found {
Some(cur)
} else {
None
}
found.to_option(cur)
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/librustc/hir/map/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,7 @@ impl<'a> FnLikeNode<'a> {
map::Node::Expr(e) => e.is_fn_like(),
_ => false
};
if fn_like {
Some(FnLikeNode {
node,
})
} else {
None
}
fn_like.to_option(FnLikeNode { node })
}

pub fn body(self) -> ast::BodyId {
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/infer/outlives/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
(r, p)
);
let p_ty = p.to_ty(tcx);
if compare_ty(p_ty) {
Some(ty::OutlivesPredicate(p_ty, r))
} else {
None
}
compare_ty(p_ty).to_option(ty::OutlivesPredicate(p_ty, r))
});

param_bounds
Expand Down
1 change: 1 addition & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]

#![feature(arbitrary_self_types)]
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_fn)]
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,7 @@ impl<'tcx> Body<'tcx> {
pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
(self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
let local = Local::new(index);
if self.local_decls[local].is_user_variable.is_some() {
Some(local)
} else {
None
}
self.local_decls[local].is_user_variable.as_ref().map(|_| local)
})
}

Expand Down
11 changes: 4 additions & 7 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,7 @@ impl Session {
}

pub fn incr_comp_session_dir_opt(&self) -> Option<cell::Ref<'_, PathBuf>> {
if self.opts.incremental.is_some() {
Some(self.incr_comp_session_dir())
} else {
None
}
self.opts.incremental.as_ref().map(|_| self.incr_comp_session_dir())
}

pub fn print_perf_stats(&self) {
Expand Down Expand Up @@ -1148,8 +1144,9 @@ fn build_session_(
None
}
}
}
else { None };
} else {
None
};

let host_triple = TargetTriple::from_triple(config::host_triple());
let host = Target::search(&host_triple).unwrap_or_else(|e|
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
return None
};

if tcx.has_attr(impl_def_id, sym::rustc_on_unimplemented) {
Some(impl_def_id)
} else {
None
}
tcx.has_attr(impl_def_id, sym::rustc_on_unimplemented).to_option(impl_def_id)
}

fn on_unimplemented_note(
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1572,11 +1572,7 @@ impl<'tcx> TyCtxt<'tcx> {
ty::FnDef(_, _) => {
let sig = ret_ty.fn_sig(*self);
let output = self.erase_late_bound_regions(&sig.output());
if output.is_impl_trait() {
Some(output)
} else {
None
}
output.is_impl_trait().to_option(output)
}
_ => None
}
Expand Down
8 changes: 2 additions & 6 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2824,11 +2824,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
};

if is_associated_item {
Some(self.associated_item(def_id))
} else {
None
}
is_associated_item.to_option_with(|| self.associated_item(def_id))
}

fn associated_item_from_trait_item_ref(self,
Expand Down Expand Up @@ -3292,7 +3288,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ParamEnv<'_> {
let unnormalized_env = ty::ParamEnv::new(
tcx.intern_predicates(&predicates),
traits::Reveal::UserFacing,
if tcx.sess.opts.debugging_opts.chalk { Some(def_id) } else { None }
tcx.sess.opts.debugging_opts.chalk.to_option(def_id),
);

let body_id = tcx.hir().as_local_hir_id(def_id).map_or(hir::DUMMY_HIR_ID, |id| {
Expand Down
9 changes: 2 additions & 7 deletions src/librustc/ty/query/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,8 @@ fn connected_to_root<'tcx>(
return true;
}

visit_waiters(query, |_, successor| {
if connected_to_root(successor, visited) {
Some(None)
} else {
None
}
}).is_some()
visit_waiters(query, |_, successor| connected_to_root(successor, visited).to_option(None))
.is_some()
}

// Deterministically pick an query from a list
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_codegen_llvm/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,7 @@ pub fn provide_extern(providers: &mut Providers<'_>) {
let native_libs = tcx.native_libraries(cnum);

let def_id_to_native_lib = native_libs.iter().filter_map(|lib|
if let Some(id) = lib.foreign_module {
Some((id, lib))
} else {
None
}
lib.foreign_module.map(|id| (id, lib))
).collect::<FxHashMap<_, _>>();

let mut ret = FxHashMap::default();
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_codegen_llvm/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
let (mut lo, mut hi) = (0u64, 0u64);
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
&mut hi, &mut lo);
if success {
Some(hi_lo_to_u128(lo, hi))
} else {
None
}
success.to_option(hi_lo_to_u128(lo, hi))
})
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc_codegen_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]

#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_cstr_unchecked)]
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_codegen_ssa/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {
use std::path::Component;

if path.is_absolute() != base.is_absolute() {
if path.is_absolute() {
Some(PathBuf::from(path))
} else {
None
}
path.is_absolute().to_option_with(|| PathBuf::from(path))
} else {
let mut ita = path.components();
let mut itb = base.components();
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_codegen_ssa/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ fn reachable_non_generics_provider(
match tcx.hir().get(hir_id) {
Node::ForeignItem(..) => {
let def_id = tcx.hir().local_def_id(hir_id);
if tcx.is_statically_included_foreign_item(def_id) {
Some(def_id)
} else {
None
}
tcx.is_statically_included_foreign_item(def_id).to_option(def_id)
}

// Only consider nodes that actually have exported symbols.
Expand Down
25 changes: 9 additions & 16 deletions src/librustc_codegen_ssa/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]

#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(core_intrinsics)]
Expand Down Expand Up @@ -68,22 +69,14 @@ impl<M> ModuleCodegen<M> {
emit_bc: bool,
emit_bc_compressed: bool,
outputs: &OutputFilenames) -> CompiledModule {
let object = if emit_obj {
Some(outputs.temp_path(OutputType::Object, Some(&self.name)))
} else {
None
};
let bytecode = if emit_bc {
Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)))
} else {
None
};
let bytecode_compressed = if emit_bc_compressed {
Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))
.with_extension(RLIB_BYTECODE_EXTENSION))
} else {
None
};
let object = emit_obj
.to_option_with(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
let bytecode = emit_bc
.to_option_with(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
let bytecode_compressed = emit_bc_compressed.to_option_with(|| {
outputs.temp_path(OutputType::Bitcode, Some(&self.name))
.with_extension(RLIB_BYTECODE_EXTENSION)
});

CompiledModule {
name: self.name.clone(),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_interface/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(bool_to_option)]
#![feature(box_syntax)]
#![feature(set_stdio)]
#![feature(nll)]
Expand Down
8 changes: 1 addition & 7 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,7 @@ fn output_contains_path(output_paths: &[PathBuf], input_path: &PathBuf) -> bool
}

fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<PathBuf> {
let check = |output_path: &PathBuf| {
if output_path.is_dir() {
Some(output_path.clone())
} else {
None
}
};
let check = |output_path: &PathBuf| output_path.is_dir().to_option_with(|| output_path.clone());
check_output(output_paths, check)
}

Expand Down
8 changes: 3 additions & 5 deletions src/librustc_interface/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,9 @@ pub(crate) struct Queries {
impl Compiler {
pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
self.queries.dep_graph_future.compute(|| {
Ok(if self.session().opts.build_dep_graph() {
Some(rustc_incremental::load_dep_graph(self.session()))
} else {
None
})
Ok(self.session().opts.build_dep_graph().to_option_with(|| {
rustc_incremental::load_dep_graph(self.session())
}))
})
}

Expand Down
12 changes: 2 additions & 10 deletions src/librustc_interface/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,7 @@ const STACK_SIZE: usize = 16 * 1024 * 1024;
fn get_stack_size() -> Option<usize> {
// FIXME: Hacks on hacks. If the env is trying to override the stack size
// then *don't* set it explicitly.
if env::var_os("RUST_MIN_STACK").is_none() {
Some(STACK_SIZE)
} else {
None
}
env::var_os("RUST_MIN_STACK").is_none().to_option(STACK_SIZE)
}

struct Sink(Arc<Mutex<Vec<u8>>>);
Expand Down Expand Up @@ -310,11 +306,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
} else {
"rustc"
});
if candidate.exists() {
Some(candidate)
} else {
None
}
candidate.exists().to_option(candidate)
})
.next()
}
Expand Down
12 changes: 2 additions & 10 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,11 +1545,7 @@ impl ExplicitOutlivesRequirements {
match pred {
ty::Predicate::TypeOutlives(outlives) => {
let outlives = outlives.skip_binder();
if outlives.0.is_param(index) {
Some(outlives.1)
} else {
None
}
outlives.0.is_param(index).to_option(outlives.1)
}
_ => None
}
Expand Down Expand Up @@ -1608,11 +1604,7 @@ impl ExplicitOutlivesRequirements {
}),
_ => false,
};
if is_inferred {
Some((i, bound.span()))
} else {
None
}
is_inferred.to_option((i, bound.span()))
} else {
None
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]

#![cfg_attr(test, feature(test))]
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(nll)]
Expand Down
Loading

0 comments on commit d998a73

Please sign in to comment.