From 1418df5888131578eae04b39360f30df4ffe5599 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 16 Oct 2021 03:45:14 +0200 Subject: [PATCH 1/2] Adopt let_else across the compiler This performs a substitution of code following the pattern: let = if let = ... { identity } else { ... : ! }; To simplify it to: let = ... { identity } else { ... : ! }; By adopting the let_else feature. --- compiler/rustc_borrowck/src/borrow_set.rs | 4 +--- .../src/diagnostics/outlives_suggestion.rs | 4 +--- compiler/rustc_borrowck/src/lib.rs | 1 + compiler/rustc_codegen_ssa/src/back/link.rs | 5 ++--- compiler/rustc_codegen_ssa/src/lib.rs | 1 + compiler/rustc_codegen_ssa/src/mir/operand.rs | 4 +--- compiler/rustc_const_eval/src/lib.rs | 1 + .../src/transform/check_consts/qualifs.rs | 4 +--- compiler/rustc_errors/src/emitter.rs | 6 +----- compiler/rustc_errors/src/lib.rs | 1 + compiler/rustc_expand/src/expand.rs | 4 +--- compiler/rustc_expand/src/lib.rs | 1 + compiler/rustc_expand/src/mbe/transcribe.rs | 10 +++------- compiler/rustc_incremental/src/lib.rs | 1 + compiler/rustc_incremental/src/persist/fs.rs | 13 +++++-------- .../nice_region_error/static_impl_trait.rs | 4 +--- compiler/rustc_infer/src/lib.rs | 1 + compiler/rustc_metadata/src/lib.rs | 1 + compiler/rustc_metadata/src/rmeta/decoder.rs | 4 +--- compiler/rustc_middle/src/lib.rs | 1 + compiler/rustc_middle/src/ty/diagnostics.rs | 4 +--- compiler/rustc_middle/src/ty/layout.rs | 13 ++++--------- compiler/rustc_mir_build/src/build/expr/as_place.rs | 6 ++---- compiler/rustc_mir_build/src/build/scope.rs | 6 +----- compiler/rustc_mir_build/src/lib.rs | 1 + compiler/rustc_mir_dataflow/src/lib.rs | 1 + compiler/rustc_mir_dataflow/src/rustc_peek.rs | 8 ++------ compiler/rustc_mir_transform/src/inline.rs | 4 +--- compiler/rustc_mir_transform/src/lib.rs | 1 + compiler/rustc_mir_transform/src/lower_slice_len.rs | 4 +--- .../rustc_mir_transform/src/normalize_array_len.rs | 6 ++---- .../src/uninhabited_enum_branching.rs | 9 +++------ compiler/rustc_monomorphize/src/lib.rs | 1 + .../rustc_monomorphize/src/partitioning/default.rs | 4 +--- compiler/rustc_query_system/src/lib.rs | 1 + compiler/rustc_query_system/src/query/job.rs | 4 +--- compiler/rustc_query_system/src/query/plumbing.rs | 6 ++---- compiler/rustc_resolve/src/diagnostics.rs | 4 +--- compiler/rustc_resolve/src/lib.rs | 1 + compiler/rustc_trait_selection/src/lib.rs | 1 + .../src/traits/error_reporting/suggestions.rs | 6 ++---- .../src/traits/object_safety.rs | 4 +--- .../src/traits/on_unimplemented.rs | 4 +--- .../src/traits/select/candidate_assembly.rs | 4 +--- compiler/rustc_typeck/src/astconv/mod.rs | 4 +--- compiler/rustc_typeck/src/check/demand.rs | 4 +--- compiler/rustc_typeck/src/check/op.rs | 4 +--- compiler/rustc_typeck/src/check/pat.rs | 5 +---- compiler/rustc_typeck/src/check/place_op.rs | 4 +--- compiler/rustc_typeck/src/check/upvar.rs | 10 ++-------- compiler/rustc_typeck/src/lib.rs | 1 + 51 files changed, 69 insertions(+), 137 deletions(-) diff --git a/compiler/rustc_borrowck/src/borrow_set.rs b/compiler/rustc_borrowck/src/borrow_set.rs index ee2ce1d3f74c3..e30d6c7fca731 100644 --- a/compiler/rustc_borrowck/src/borrow_set.rs +++ b/compiler/rustc_borrowck/src/borrow_set.rs @@ -315,9 +315,7 @@ impl<'a, 'tcx> GatherBorrows<'a, 'tcx> { // TEMP = &foo // // so extract `temp`. - let temp = if let Some(temp) = assigned_place.as_local() { - temp - } else { + let Some(temp) = assigned_place.as_local() else { span_bug!( self.body.source_info(start_location).span, "expected 2-phase borrow to assign to a local, not `{:?}`", diff --git a/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs b/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs index b15e55cd6675e..723b57ed970ad 100644 --- a/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs +++ b/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs @@ -90,9 +90,7 @@ impl OutlivesSuggestionBuilder { let mut unified_already = FxHashSet::default(); for (fr, outlived) in &self.constraints_to_add { - let fr_name = if let Some(fr_name) = self.region_vid_to_name(mbcx, *fr) { - fr_name - } else { + let Some(fr_name) = self.region_vid_to_name(mbcx, *fr) else { continue; }; diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index e6260157d11a7..08df87e0488b8 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -7,6 +7,7 @@ #![feature(format_args_capture)] #![feature(in_band_lifetimes)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(min_specialization)] #![feature(stmt_expr_attributes)] #![feature(trusted_step)] diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index cf1c60588978c..be50911f4e143 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -174,9 +174,8 @@ pub fn each_linked_rlib( _ => {} } } - let fmts = match fmts { - Some(f) => f, - None => return Err("could not find formats for rlibs".to_string()), + let Some(fmts) = fmts else { + return Err("could not find formats for rlibs".to_string()); }; for &cnum in crates { match fmts.get(cnum.as_usize() - 1) { diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index b759e3a7a7a33..f78196d7ec58e 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -3,6 +3,7 @@ #![feature(box_patterns)] #![feature(try_blocks)] #![feature(in_band_lifetimes)] +#![feature(let_else)] #![feature(once_cell)] #![feature(nll)] #![feature(associated_type_bounds)] diff --git a/compiler/rustc_codegen_ssa/src/mir/operand.rs b/compiler/rustc_codegen_ssa/src/mir/operand.rs index ce6cec67ad41e..bea55bbc87965 100644 --- a/compiler/rustc_codegen_ssa/src/mir/operand.rs +++ b/compiler/rustc_codegen_ssa/src/mir/operand.rs @@ -343,9 +343,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue { .unwrap_or_else(|| bug!("indirect_dest has non-pointer type: {:?}", indirect_dest)) .ty; - let (llptr, llextra) = if let OperandValue::Ref(llptr, Some(llextra), _) = self { - (llptr, llextra) - } else { + let OperandValue::Ref(llptr, Some(llextra), _) = self else { bug!("store_unsized called with a sized value") }; diff --git a/compiler/rustc_const_eval/src/lib.rs b/compiler/rustc_const_eval/src/lib.rs index 7ce40b319a1e5..f308e764e861d 100644 --- a/compiler/rustc_const_eval/src/lib.rs +++ b/compiler/rustc_const_eval/src/lib.rs @@ -13,6 +13,7 @@ Rust MIR: a lowered representation of Rust. #![feature(exact_size_is_empty)] #![feature(in_band_lifetimes)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(map_try_insert)] #![feature(min_specialization)] #![feature(slice_ptr_get)] diff --git a/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs b/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs index 5eb7d7a91cc76..14e97f6ea0ffd 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs @@ -122,9 +122,7 @@ impl Qualif for NeedsNonConstDrop { Ok([..]) => {} } - let drop_trait = if let Some(did) = cx.tcx.lang_items().drop_trait() { - did - } else { + let Some(drop_trait) = cx.tcx.lang_items().drop_trait() else { // there is no way to define a type that needs non-const drop // without having the lang item present. return false; diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 778d58eeadcf0..849ffa881df7d 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -449,11 +449,7 @@ pub trait Emitter { span: &mut MultiSpan, children: &mut Vec, ) { - let source_map = if let Some(ref sm) = source_map { - sm - } else { - return; - }; + let Some(source_map) = source_map else { return }; debug!("fix_multispans_in_extern_macros: before: span={:?} children={:?}", span, children); self.fix_multispan_in_extern_macros(source_map, span); for child in children.iter_mut() { diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 60a48b5a2d9c1..1ccfa06ff6729 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -8,6 +8,7 @@ #![feature(if_let_guard)] #![feature(format_args_capture)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(nll)] #[macro_use] diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 1d6703077acff..2c78acfd186e2 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -447,9 +447,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { let mut undetermined_invocations = Vec::new(); let (mut progress, mut force) = (false, !self.monotonic); loop { - let (invoc, ext) = if let Some(invoc) = invocations.pop() { - invoc - } else { + let Some((invoc, ext)) = invocations.pop() else { self.resolve_imports(); if undetermined_invocations.is_empty() { break; diff --git a/compiler/rustc_expand/src/lib.rs b/compiler/rustc_expand/src/lib.rs index 6dfeb047ec94c..521ca2135c6f2 100644 --- a/compiler/rustc_expand/src/lib.rs +++ b/compiler/rustc_expand/src/lib.rs @@ -4,6 +4,7 @@ #![feature(format_args_capture)] #![feature(if_let_guard)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(proc_macro_diagnostic)] #![feature(proc_macro_internals)] #![feature(proc_macro_span)] diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index 9ed5c8b8ffba5..4663dd80fa8bb 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -116,10 +116,8 @@ pub(super) fn transcribe<'a>( loop { // Look at the last frame on the stack. - let tree = if let Some(tree) = stack.last_mut().unwrap().next() { - // If it still has a TokenTree we have not looked at yet, use that tree. - tree - } else { + // If it still has a TokenTree we have not looked at yet, use that tree. + let Some(tree) = stack.last_mut().unwrap().next() else { // This else-case never produces a value for `tree` (it `continue`s or `return`s). // Otherwise, if we have just reached the end of a sequence and we can keep repeating, @@ -190,9 +188,7 @@ pub(super) fn transcribe<'a>( LockstepIterSize::Constraint(len, _) => { // We do this to avoid an extra clone above. We know that this is a // sequence already. - let (sp, seq) = if let mbe::TokenTree::Sequence(sp, seq) = seq { - (sp, seq) - } else { + let mbe::TokenTree::Sequence(sp, seq) = seq else { unreachable!() }; diff --git a/compiler/rustc_incremental/src/lib.rs b/compiler/rustc_incremental/src/lib.rs index f089cbcfca6e5..dd3f8c937f81a 100644 --- a/compiler/rustc_incremental/src/lib.rs +++ b/compiler/rustc_incremental/src/lib.rs @@ -2,6 +2,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![feature(in_band_lifetimes)] +#![feature(let_else)] #![feature(nll)] #![recursion_limit = "256"] diff --git a/compiler/rustc_incremental/src/persist/fs.rs b/compiler/rustc_incremental/src/persist/fs.rs index 2ed0539841aa3..38cbf5314ef31 100644 --- a/compiler/rustc_incremental/src/persist/fs.rs +++ b/compiler/rustc_incremental/src/persist/fs.rs @@ -241,9 +241,7 @@ pub fn prepare_session_directory( // have already tried before. let source_directory = find_source_directory(&crate_dir, &source_directories_already_tried); - let source_directory = if let Some(dir) = source_directory { - dir - } else { + let Some(source_directory) = source_directory else { // There's nowhere to copy from, we're done debug!( "no source directory found. Continuing with empty session \ @@ -397,15 +395,14 @@ fn copy_files(sess: &Session, target_dir: &Path, source_dir: &Path) -> Result { let item = tcx.hir().item(item_id); - let opaque = if let ItemKind::OpaqueTy(opaque) = &item.kind { - opaque - } else { + let ItemKind::OpaqueTy(opaque) = &item.kind else { return; }; diff --git a/compiler/rustc_infer/src/lib.rs b/compiler/rustc_infer/src/lib.rs index a4cfaddeeb96f..d0f1ff649d058 100644 --- a/compiler/rustc_infer/src/lib.rs +++ b/compiler/rustc_infer/src/lib.rs @@ -17,6 +17,7 @@ #![feature(box_patterns)] #![feature(extend_one)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(never_type)] #![feature(in_band_lifetimes)] #![feature(control_flow_enum)] diff --git a/compiler/rustc_metadata/src/lib.rs b/compiler/rustc_metadata/src/lib.rs index 644b849a9f899..6cf0dd8b1addb 100644 --- a/compiler/rustc_metadata/src/lib.rs +++ b/compiler/rustc_metadata/src/lib.rs @@ -2,6 +2,7 @@ #![feature(crate_visibility_modifier)] #![feature(drain_filter)] #![feature(in_band_lifetimes)] +#![feature(let_else)] #![feature(nll)] #![feature(once_cell)] #![feature(proc_macro_internals)] diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 89bb5797a828f..ca9daa49aa2d2 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -472,9 +472,7 @@ impl<'a, 'tcx> Decodable> for Span { let len = BytePos::decode(decoder)?; let hi = lo + len; - let sess = if let Some(sess) = decoder.sess { - sess - } else { + let Some(sess) = decoder.sess else { bug!("Cannot decode Span without Session.") }; diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index e41f5add457fb..0894b80507581 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -39,6 +39,7 @@ #![feature(new_uninit)] #![feature(nll)] #![feature(once_cell)] +#![feature(let_else)] #![feature(min_specialization)] #![feature(trusted_len)] #![feature(in_band_lifetimes)] diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 092eae0fc5c23..1b32c8a66989f 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -221,9 +221,7 @@ pub fn suggest_constraining_type_param( ) -> bool { let param = generics.params.iter().find(|p| p.name.ident().as_str() == param_name); - let param = if let Some(param) = param { - param - } else { + let Some(param) = param else { return false; }; diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index d0c7379c2d94d..8ec5f4c79781f 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -755,17 +755,14 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { } // Extract the number of elements from the layout of the array field: - let len = if let Ok(TyAndLayout { + let Ok(TyAndLayout { layout: Layout { fields: FieldsShape::Array { count, .. }, .. }, .. - }) = self.layout_of(f0_ty) - { - count - } else { + }) = self.layout_of(f0_ty) else { return Err(LayoutError::Unknown(ty)); }; - (*e_ty, *len, true) + (*e_ty, *count, true) } else { // First ADT field is not an array: (f0_ty, def.non_enum_variant().fields.len() as _, false) @@ -787,9 +784,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { // Compute the ABI of the element type: let e_ly = self.layout_of(e_ty)?; - let e_abi = if let Abi::Scalar(scalar) = e_ly.abi { - scalar - } else { + let Abi::Scalar(e_abi) = e_ly.abi else { // This error isn't caught in typeck, e.g., if // the element type of the vector is generic. tcx.sess.fatal(&format!( diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 05995ddcc0051..c6a34ece24576 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -221,15 +221,13 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>( let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local()); let closure_span = tcx.hir().span(closure_hir_id); - let (capture_index, capture) = if let Some(capture_details) = + let Some((capture_index, capture)) = find_capture_matching_projections( typeck_results, var_hir_id, closure_def_id, &from_builder.projection, - ) { - capture_details - } else { + ) else { if !enable_precise_capture(tcx, closure_span) { bug!( "No associated capture found for {:?}[{:#?}] even though \ diff --git a/compiler/rustc_mir_build/src/build/scope.rs b/compiler/rustc_mir_build/src/build/scope.rs index b74208edafea6..8dadbf5f02bd1 100644 --- a/compiler/rustc_mir_build/src/build/scope.rs +++ b/compiler/rustc_mir_build/src/build/scope.rs @@ -362,11 +362,7 @@ impl DropTree { blocks: &IndexVec>, ) { for (drop_idx, drop_data) in self.drops.iter_enumerated().rev() { - let block = if let Some(block) = blocks[drop_idx] { - block - } else { - continue; - }; + let Some(block) = blocks[drop_idx] else { continue }; match drop_data.0.kind { DropKind::Value => { let terminator = TerminatorKind::Drop { diff --git a/compiler/rustc_mir_build/src/lib.rs b/compiler/rustc_mir_build/src/lib.rs index 02023c48a6c98..b0f1e08562c16 100644 --- a/compiler/rustc_mir_build/src/lib.rs +++ b/compiler/rustc_mir_build/src/lib.rs @@ -6,6 +6,7 @@ #![feature(crate_visibility_modifier)] #![feature(bool_to_option)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(once_cell)] #![feature(min_specialization)] #![recursion_limit = "256"] diff --git a/compiler/rustc_mir_dataflow/src/lib.rs b/compiler/rustc_mir_dataflow/src/lib.rs index 402391b87eaa9..2f3de52965db1 100644 --- a/compiler/rustc_mir_dataflow/src/lib.rs +++ b/compiler/rustc_mir_dataflow/src/lib.rs @@ -6,6 +6,7 @@ #![feature(exact_size_is_empty)] #![feature(in_band_lifetimes)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(min_specialization)] #![feature(once_cell)] #![feature(stmt_expr_attributes)] diff --git a/compiler/rustc_mir_dataflow/src/rustc_peek.rs b/compiler/rustc_mir_dataflow/src/rustc_peek.rs index c0bf4b659aa96..2d27d085b4893 100644 --- a/compiler/rustc_mir_dataflow/src/rustc_peek.rs +++ b/compiler/rustc_mir_dataflow/src/rustc_peek.rs @@ -290,9 +290,7 @@ impl<'tcx> RustcPeekAt<'tcx> for MaybeMutBorrowedLocals<'_, 'tcx> { call: PeekCall, ) { info!(?place, "peek_at"); - let local = if let Some(l) = place.as_local() { - l - } else { + let Some(local) = place.as_local() else { tcx.sess.span_err(call.span, "rustc_peek: argument was not a local"); return; }; @@ -312,9 +310,7 @@ impl<'tcx> RustcPeekAt<'tcx> for MaybeLiveLocals { call: PeekCall, ) { info!(?place, "peek_at"); - let local = if let Some(l) = place.as_local() { - l - } else { + let Some(local) = place.as_local() else { tcx.sess.span_err(call.span, "rustc_peek: argument was not a local"); return; }; diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index ee4e91ecb62b0..84a1e3fb600fd 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -673,9 +673,7 @@ impl Inliner<'tcx> { assert!(args.next().is_none()); let tuple = Place::from(tuple); - let tuple_tys = if let ty::Tuple(s) = tuple.ty(caller_body, tcx).ty.kind() { - s - } else { + let ty::Tuple(tuple_tys) = tuple.ty(caller_body, tcx).ty.kind() else { bug!("Closure arguments are not passed as a tuple"); }; diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 9b11c8f0b24c0..60135ef2d8571 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -4,6 +4,7 @@ #![cfg_attr(bootstrap, feature(const_panic))] #![feature(in_band_lifetimes)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(map_try_insert)] #![feature(min_specialization)] #![feature(option_get_or_insert_default)] diff --git a/compiler/rustc_mir_transform/src/lower_slice_len.rs b/compiler/rustc_mir_transform/src/lower_slice_len.rs index a2cce9f1eda07..822a372d8ce90 100644 --- a/compiler/rustc_mir_transform/src/lower_slice_len.rs +++ b/compiler/rustc_mir_transform/src/lower_slice_len.rs @@ -17,9 +17,7 @@ impl<'tcx> MirPass<'tcx> for LowerSliceLenCalls { pub fn lower_slice_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { let language_items = tcx.lang_items(); - let slice_len_fn_item_def_id = if let Some(slice_len_fn_item) = language_items.slice_len_fn() { - slice_len_fn_item - } else { + let Some(slice_len_fn_item_def_id) = language_items.slice_len_fn() else { // there is no language item to compare to :) return; }; diff --git a/compiler/rustc_mir_transform/src/normalize_array_len.rs b/compiler/rustc_mir_transform/src/normalize_array_len.rs index 76f0e83c8c3d5..a04a0b5153150 100644 --- a/compiler/rustc_mir_transform/src/normalize_array_len.rs +++ b/compiler/rustc_mir_transform/src/normalize_array_len.rs @@ -208,7 +208,7 @@ fn normalize_array_len_call<'tcx>( operand, cast_ty, ) => { - let local = if let Some(local) = place.as_local() { local } else { return }; + let Some(local) = place.as_local() else { return }; match operand { Operand::Copy(place) | Operand::Move(place) => { let operand_local = @@ -255,9 +255,7 @@ fn normalize_array_len_call<'tcx>( } } Rvalue::Len(place) => { - let local = if let Some(local) = place.local_or_deref_local() { - local - } else { + let Some(local) = place.local_or_deref_local() else { return; }; if let Some(cast_statement_idx) = state.get(&local).copied() { diff --git a/compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs b/compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs index 5cef64d7786b3..2aa506112909d 100644 --- a/compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs +++ b/compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs @@ -83,12 +83,9 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching { let bb = BasicBlock::from_usize(bb); trace!("processing block {:?}", bb); - let discriminant_ty = - if let Some(ty) = get_switched_on_type(&body.basic_blocks()[bb], tcx, body) { - ty - } else { - continue; - }; + let Some(discriminant_ty) = get_switched_on_type(&body.basic_blocks()[bb], tcx, body) else { + continue; + }; let layout = tcx.layout_of(tcx.param_env(body.source.def_id()).and(discriminant_ty)); diff --git a/compiler/rustc_monomorphize/src/lib.rs b/compiler/rustc_monomorphize/src/lib.rs index 08b1d7b7fabd7..f4082153b6840 100644 --- a/compiler/rustc_monomorphize/src/lib.rs +++ b/compiler/rustc_monomorphize/src/lib.rs @@ -2,6 +2,7 @@ #![feature(bool_to_option)] #![feature(crate_visibility_modifier)] #![feature(control_flow_enum)] +#![feature(let_else)] #![feature(in_band_lifetimes)] #![recursion_limit = "256"] diff --git a/compiler/rustc_monomorphize/src/partitioning/default.rs b/compiler/rustc_monomorphize/src/partitioning/default.rs index 429ed53d37977..59997e9dc53cb 100644 --- a/compiler/rustc_monomorphize/src/partitioning/default.rs +++ b/compiler/rustc_monomorphize/src/partitioning/default.rs @@ -451,9 +451,7 @@ fn mono_item_visibility( let is_generic = instance.substs.non_erasable_generics().next().is_some(); // Upstream `DefId` instances get different handling than local ones. - let def_id = if let Some(def_id) = def_id.as_local() { - def_id - } else { + let Some(def_id) = def_id.as_local() else { return if export_generics && is_generic { // If it is an upstream monomorphization and we export generics, we must make // it available to downstream crates. diff --git a/compiler/rustc_query_system/src/lib.rs b/compiler/rustc_query_system/src/lib.rs index bc23de069b0d9..1b992cdb0c94b 100644 --- a/compiler/rustc_query_system/src/lib.rs +++ b/compiler/rustc_query_system/src/lib.rs @@ -3,6 +3,7 @@ #![feature(core_intrinsics)] #![feature(hash_raw_entry)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(min_specialization)] #![feature(thread_local_const_init)] diff --git a/compiler/rustc_query_system/src/query/job.rs b/compiler/rustc_query_system/src/query/job.rs index 98b2a450b19df..bd67303099220 100644 --- a/compiler/rustc_query_system/src/query/job.rs +++ b/compiler/rustc_query_system/src/query/job.rs @@ -644,9 +644,7 @@ pub fn print_query_stack( if Some(i) == num_frames { break; } - let query_info = if let Some(info) = query_map.as_ref().and_then(|map| map.get(&query)) { - info - } else { + let Some(query_info) = query_map.as_ref().and_then(|map| map.get(&query)) else { break; }; let mut diag = Diagnostic::new( diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index 07d7205997596..d3c75635783fa 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -761,11 +761,9 @@ where return false; } - let key = if let Some(key) = + let Some(key) = >::recover(*tcx.dep_context(), &dep_node) - { - key - } else { + else { return false; }; diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index e3970038a33b0..ad4231094d331 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1471,9 +1471,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { module: ModuleOrUniformRoot<'b>, ident: Ident, ) -> Option<(Option, Vec)> { - let mut crate_module = if let ModuleOrUniformRoot::Module(module) = module { - module - } else { + let ModuleOrUniformRoot::Module(mut crate_module) = module else { return None; }; diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 9652c483686f0..168191032e93a 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -15,6 +15,7 @@ #![feature(crate_visibility_modifier)] #![feature(format_args_capture)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(never_type)] #![feature(nll)] #![recursion_limit = "256"] diff --git a/compiler/rustc_trait_selection/src/lib.rs b/compiler/rustc_trait_selection/src/lib.rs index 017a7c45bbf9d..1a049e6ec649d 100644 --- a/compiler/rustc_trait_selection/src/lib.rs +++ b/compiler/rustc_trait_selection/src/lib.rs @@ -17,6 +17,7 @@ #![feature(hash_drain_filter)] #![feature(in_band_lifetimes)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(never_type)] #![feature(crate_visibility_modifier)] #![feature(control_flow_enum)] diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 1a8f863952e6a..970fb30487963 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1038,13 +1038,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { let hir = self.tcx.hir(); let parent_node = hir.get_parent_node(obligation.cause.body_id); let node = hir.find(parent_node); - let (sig, body_id) = if let Some(hir::Node::Item(hir::Item { + let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. })) = node - { - (sig, body_id) - } else { + else { return false; }; let body = hir.body(*body_id); diff --git a/compiler/rustc_trait_selection/src/traits/object_safety.rs b/compiler/rustc_trait_selection/src/traits/object_safety.rs index 0bb00dfeb43ad..ad9fd0ca62cfa 100644 --- a/compiler/rustc_trait_selection/src/traits/object_safety.rs +++ b/compiler/rustc_trait_selection/src/traits/object_safety.rs @@ -647,9 +647,7 @@ fn receiver_is_dispatchable<'tcx>( debug!("receiver_is_dispatchable: method = {:?}, receiver_ty = {:?}", method, receiver_ty); let traits = (tcx.lang_items().unsize_trait(), tcx.lang_items().dispatch_from_dyn_trait()); - let (unsize_did, dispatch_from_dyn_did) = if let (Some(u), Some(cu)) = traits { - (u, cu) - } else { + let (Some(unsize_did), Some(dispatch_from_dyn_did)) = traits else { debug!("receiver_is_dispatchable: Missing Unsize or DispatchFromDyn traits"); return false; }; diff --git a/compiler/rustc_trait_selection/src/traits/on_unimplemented.rs b/compiler/rustc_trait_selection/src/traits/on_unimplemented.rs index 209fd83b3ab34..85ca4db7d747d 100644 --- a/compiler/rustc_trait_selection/src/traits/on_unimplemented.rs +++ b/compiler/rustc_trait_selection/src/traits/on_unimplemented.rs @@ -164,9 +164,7 @@ impl<'tcx> OnUnimplementedDirective { ) -> Result, ErrorReported> { let attrs = tcx.get_attrs(impl_def_id); - let attr = if let Some(item) = tcx.sess.find_by_name(&attrs, sym::rustc_on_unimplemented) { - item - } else { + let Some(attr) = tcx.sess.find_by_name(&attrs, sym::rustc_on_unimplemented) else { return Ok(None); }; diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 856ea43b1ff4e..74e132097cc55 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -716,9 +716,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { cause.clone(), ); - let data = if let ty::Dynamic(ref data, ..) = normalized_ty.kind() { - data - } else { + let ty::Dynamic(data, ..) = normalized_ty.kind() else { return None; }; diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index 889b68773c27b..da751f2075399 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -1916,9 +1916,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { debug!("qpath_to_ty: trait_def_id={:?}", trait_def_id); - let self_ty = if let Some(ty) = opt_self_ty { - ty - } else { + let Some(self_ty) = opt_self_ty else { let path_str = tcx.def_path_str(trait_def_id); let def_id = self.item_def_id(); diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index 540365956a8fb..2b3672211e4f5 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -743,9 +743,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return false; } - let src = if let Ok(src) = self.tcx.sess.source_map().span_to_snippet(expr.span) { - src - } else { + let Ok(src) = self.tcx.sess.source_map().span_to_snippet(expr.span) else { return false; }; diff --git a/compiler/rustc_typeck/src/check/op.rs b/compiler/rustc_typeck/src/check/op.rs index 79e004a47db53..ec4b32b43a98c 100644 --- a/compiler/rustc_typeck/src/check/op.rs +++ b/compiler/rustc_typeck/src/check/op.rs @@ -502,9 +502,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // FIXME: Instead of exiting early when encountering bound vars in // the function signature, consider keeping the binder here and // propagating it downwards. - let fn_sig = if let Some(fn_sig) = self.tcx.fn_sig(def_id).no_bound_vars() { - fn_sig - } else { + let Some(fn_sig) = self.tcx.fn_sig(def_id).no_bound_vars() else { return false; }; diff --git a/compiler/rustc_typeck/src/check/pat.rs b/compiler/rustc_typeck/src/check/pat.rs index 635219146d0f5..5aa11cce25fb6 100644 --- a/compiler/rustc_typeck/src/check/pat.rs +++ b/compiler/rustc_typeck/src/check/pat.rs @@ -718,10 +718,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ti: TopInfo<'tcx>, ) -> Ty<'tcx> { // Resolve the path and check the definition for errors. - let (variant, pat_ty) = if let Some(variant_ty) = self.check_struct_path(qpath, pat.hir_id) - { - variant_ty - } else { + let Some((variant, pat_ty)) = self.check_struct_path(qpath, pat.hir_id) else { let err = self.tcx.ty_error(); for field in fields { let ti = TopInfo { parent_pat: Some(pat), ..ti }; diff --git a/compiler/rustc_typeck/src/check/place_op.rs b/compiler/rustc_typeck/src/check/place_op.rs index 849bf1e455cbf..5d9e6ebd50c85 100644 --- a/compiler/rustc_typeck/src/check/place_op.rs +++ b/compiler/rustc_typeck/src/check/place_op.rs @@ -411,9 +411,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { debug!("convert_place_op_to_mutable: method={:?}", method); self.write_method_call(expr.hir_id, method); - let region = if let ty::Ref(r, _, hir::Mutability::Mut) = method.sig.inputs()[0].kind() { - r - } else { + let ty::Ref(region, _, hir::Mutability::Mut) = method.sig.inputs()[0].kind() else { span_bug!(expr.span, "input to mutable place op is not a mut ref?"); }; diff --git a/compiler/rustc_typeck/src/check/upvar.rs b/compiler/rustc_typeck/src/check/upvar.rs index 3a10988bba0b9..a5ee557452f50 100644 --- a/compiler/rustc_typeck/src/check/upvar.rs +++ b/compiler/rustc_typeck/src/check/upvar.rs @@ -1000,11 +1000,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return None; } - let root_var_min_capture_list = if let Some(root_var_min_capture_list) = - min_captures.and_then(|m| m.get(&var_hir_id)) - { - root_var_min_capture_list - } else { + let Some(root_var_min_capture_list) = min_captures.and_then(|m| m.get(&var_hir_id)) else { // The upvar is mentioned within the closure but no path starting from it is // used. @@ -1077,9 +1073,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { closure_clause: hir::CaptureBy, min_captures: Option<&ty::RootVariableMinCaptureList<'tcx>>, ) -> (Vec, String) { - let upvars = if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) { - upvars - } else { + let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) else { return (Vec::new(), format!("")); }; diff --git a/compiler/rustc_typeck/src/lib.rs b/compiler/rustc_typeck/src/lib.rs index 971776c882a15..017e7ad8ca74a 100644 --- a/compiler/rustc_typeck/src/lib.rs +++ b/compiler/rustc_typeck/src/lib.rs @@ -63,6 +63,7 @@ This API is completely unstable and subject to change. #![feature(in_band_lifetimes)] #![feature(is_sorted)] #![feature(iter_zip)] +#![feature(let_else)] #![feature(min_specialization)] #![feature(nll)] #![feature(try_blocks)] From ef018be5c49d7a0bb8ad8e3485111e8b2ab9c5ac Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 16 Oct 2021 04:37:30 +0200 Subject: [PATCH 2/2] Update the syn crate and adopt let_else in three more places The syn crate has gained support for let_else syntax in version 1.0.76, see https://github.com/dtolnay/syn/pull/1057 . In the three instances that use let_else, we've sent code through an attr macro, which would create compile errors when there was no let_else support in syn. To avoid this, we ran `cargo +nightly update -p syn` for updating the syn crate. --- Cargo.lock | 8 ++++---- compiler/rustc_resolve/src/late/lifetimes.rs | 4 +--- compiler/rustc_typeck/src/check/coercion.rs | 4 +--- compiler/rustc_typeck/src/check/upvar.rs | 4 +--- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 102450188aacd..9536e6f4269bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2728,9 +2728,9 @@ checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" [[package]] name = "proc-macro2" -version = "1.0.24" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70" dependencies = [ "unicode-xid", ] @@ -5090,9 +5090,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.65" +version = "1.0.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3a1d708c221c5a612956ef9f75b37e454e88d1f7b899fbd3a18d4252012d663" +checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194" dependencies = [ "proc-macro2", "quote", diff --git a/compiler/rustc_resolve/src/late/lifetimes.rs b/compiler/rustc_resolve/src/late/lifetimes.rs index eb6f302a11da8..94563400a8b53 100644 --- a/compiler/rustc_resolve/src/late/lifetimes.rs +++ b/compiler/rustc_resolve/src/late/lifetimes.rs @@ -1057,9 +1057,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { match param.kind { GenericParamKind::Lifetime { .. } => { let (name, reg) = Region::early(&self.tcx.hir(), &mut index, ¶m); - let def_id = if let Region::EarlyBound(_, def_id, _) = reg { - def_id - } else { + let Region::EarlyBound(_, def_id, _) = reg else { bug!(); }; // We cannot predict what lifetimes are unused in opaque type. diff --git a/compiler/rustc_typeck/src/check/coercion.rs b/compiler/rustc_typeck/src/check/coercion.rs index a87318ff34e6d..40f456de18332 100644 --- a/compiler/rustc_typeck/src/check/coercion.rs +++ b/compiler/rustc_typeck/src/check/coercion.rs @@ -521,9 +521,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { let traits = (self.tcx.lang_items().unsize_trait(), self.tcx.lang_items().coerce_unsized_trait()); - let (unsize_did, coerce_unsized_did) = if let (Some(u), Some(cu)) = traits { - (u, cu) - } else { + let (Some(unsize_did), Some(coerce_unsized_did)) = traits else { debug!("missing Unsize or CoerceUnsized traits"); return Err(TypeError::Mismatch); }; diff --git a/compiler/rustc_typeck/src/check/upvar.rs b/compiler/rustc_typeck/src/check/upvar.rs index a5ee557452f50..caad28ff2b21f 100644 --- a/compiler/rustc_typeck/src/check/upvar.rs +++ b/compiler/rustc_typeck/src/check/upvar.rs @@ -1678,9 +1678,7 @@ impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> { diag_expr_id: hir::HirId, ) { let tcx = self.fcx.tcx; - let upvar_id = if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base { - upvar_id - } else { + let PlaceBase::Upvar(upvar_id) = place_with_id.place.base else { return; };