Skip to content

Commit

Permalink
Auto merge of #65195 - varkor:to_option, r=Centril
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 Dec 6, 2019
2 parents 9630dbb + f1db60c commit ae1b871
Show file tree
Hide file tree
Showing 55 changed files with 97 additions and 249 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.then_some(0), None);
/// assert_eq!(true.then_some(0), Some(0));
/// ```
#[unstable(feature = "bool_to_option", issue = "64260")]
#[inline]
pub fn then<T>(self, t: T) -> Option<T> {
pub fn then_some<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.then(|| 0), None);
/// assert_eq!(true.then(|| 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 then<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.then_some(0), None);
assert_eq!(true.then_some(0), Some(0));
assert_eq!(false.then(|| 0), None);
assert_eq!(true.then(|| 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 @@ -644,11 +645,7 @@ impl<'a> Parser<'a> {
break;
}
}
if found {
Some(cur)
} else {
None
}
found.then_some(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.then_some(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).then_some(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 @@ -242,11 +242,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() {
Some(local)
} else {
None
}
self.local_decls[local].is_user_variable().then_some(local)
})
}

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 @@ -363,11 +363,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).then_some(impl_def_id)
}

fn describe_generator(&self, body_id: hir::BodyId) -> Option<&'static str> {
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 @@ -2784,11 +2784,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
};

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

fn associated_item_from_trait_item_ref(self,
Expand Down Expand Up @@ -3253,7 +3249,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.then_some(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 @@ -303,13 +303,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).then_some(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 @@ -375,11 +375,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 @@ -245,11 +245,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.then_some(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().then(|| 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).then_some(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
.then(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
let bytecode = emit_bc
.then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
let bytecode_compressed = emit_bc_compressed.then(|| {
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 @@ -547,13 +547,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().then(|| 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 @@ -117,11 +117,9 @@ impl<'tcx> Queries<'tcx> {

pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
self.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().then(|| {
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 @@ -107,11 +107,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().then_some(STACK_SIZE)
}

struct Sink(Arc<Mutex<Vec<u8>>>);
Expand Down Expand Up @@ -285,11 +281,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
} else {
"rustc"
});
if candidate.exists() {
Some(candidate)
} else {
None
}
candidate.exists().then_some(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 @@ -1491,11 +1491,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).then_some(outlives.1)
}
_ => None
}
Expand Down Expand Up @@ -1554,11 +1550,7 @@ impl ExplicitOutlivesRequirements {
}),
_ => false,
};
if is_inferred {
Some((i, bound.span()))
} else {
None
}
is_inferred.then_some((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
7 changes: 2 additions & 5 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,11 +802,8 @@ impl<'a> CrateLoader<'a> {
// First up we check for global allocators. Look at the crate graph here
// and see what's a global allocator, including if we ourselves are a
// global allocator.
let mut global_allocator = if self.cstore.has_global_allocator {
Some(Symbol::intern("this crate"))
} else {
None
};
let mut global_allocator = self.cstore.has_global_allocator
.then(|| Symbol::intern("this crate"));
self.cstore.iter_crate_data(|_, data| {
if !data.has_global_allocator() {
return
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/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(core_intrinsics)]
#![feature(crate_visibility_modifier)]
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_mir/borrow_check/nll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
Option<Rc<Output<RegionVid, BorrowIndex, LocationIndex, Local, MovePathIndex>>>,
Option<ClosureRegionRequirements<'tcx>>,
) {
let mut all_facts = if AllFacts::enabled(infcx.tcx) {
Some(AllFacts::default())
} else {
None
};
let mut all_facts = AllFacts::enabled(infcx.tcx).then_some(AllFacts::default());

let universal_regions = Rc::new(universal_regions);

Expand Down
Loading

0 comments on commit ae1b871

Please sign in to comment.