From 8dc8e72c4d68c95ef67e49370129f4b66c493add Mon Sep 17 00:00:00 2001 From: est31 Date: Thu, 18 Nov 2021 17:21:21 +0100 Subject: [PATCH] Use more let_else in rustc_mir_build Helps avoid rightward drift. --- .../rustc_mir_build/src/build/matches/mod.rs | 11 +- compiler/rustc_mir_build/src/build/mod.rs | 101 +++++++++--------- .../rustc_mir_build/src/check_unsafety.rs | 23 ++-- 3 files changed, 66 insertions(+), 69 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/matches/mod.rs b/compiler/rustc_mir_build/src/build/matches/mod.rs index 6320d5d4749..e3a05e01ea8 100644 --- a/compiler/rustc_mir_build/src/build/matches/mod.rs +++ b/compiler/rustc_mir_build/src/build/matches/mod.rs @@ -1606,13 +1606,12 @@ fn test_candidates<'pat, 'b, 'c>( // encounter a candidate where the test is not relevant; at // that point, we stop sorting. while let Some(candidate) = candidates.first_mut() { - if let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) { - let (candidate, rest) = candidates.split_first_mut().unwrap(); - target_candidates[idx].push(candidate); - candidates = rest; - } else { + let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) else { break; - } + }; + let (candidate, rest) = candidates.split_first_mut().unwrap(); + target_candidates[idx].push(candidate); + candidates = rest; } // at least the first candidate ought to be tested assert!(total_candidate_count > candidates.len()); diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 4108ad50470..cb94e759972 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -966,59 +966,58 @@ fn args_and_body( DropKind::Value, ); - if let Some(arg) = arg_opt { - let pat = match tcx.hir().get(arg.pat.hir_id) { - Node::Pat(pat) | Node::Binding(pat) => pat, - node => bug!("pattern became {:?}", node), - }; - let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat); - let original_source_scope = self.source_scope; - let span = pattern.span; - self.set_correct_source_scope_for_arg(arg.hir_id, original_source_scope, span); - match *pattern.kind { - // Don't introduce extra copies for simple bindings - PatKind::Binding { - mutability, - var, - mode: BindingMode::ByValue, - subpattern: None, - .. - } => { - self.local_decls[local].mutability = mutability; - self.local_decls[local].source_info.scope = self.source_scope; - self.local_decls[local].local_info = if let Some(kind) = self_binding { - Some(Box::new(LocalInfo::User(ClearCrossCrate::Set( - BindingForm::ImplicitSelf(*kind), - )))) - } else { - let binding_mode = ty::BindingMode::BindByValue(mutability); - Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var( - VarBindingForm { - binding_mode, - opt_ty_info, - opt_match_place: Some((Some(place), span)), - pat_span: span, - }, - ))))) - }; - self.var_indices.insert(var, LocalsForNode::One(local)); - } - _ => { - scope = self.declare_bindings( - scope, - expr.span, - &pattern, - matches::ArmHasGuard(false), - Some((Some(&place), span)), - ); - let place_builder = PlaceBuilder::from(local); - unpack!( - block = self.place_into_pattern(block, pattern, place_builder, false) - ); - } + let Some(arg) = arg_opt else { + continue; + }; + let pat = match tcx.hir().get(arg.pat.hir_id) { + Node::Pat(pat) | Node::Binding(pat) => pat, + node => bug!("pattern became {:?}", node), + }; + let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat); + let original_source_scope = self.source_scope; + let span = pattern.span; + self.set_correct_source_scope_for_arg(arg.hir_id, original_source_scope, span); + match *pattern.kind { + // Don't introduce extra copies for simple bindings + PatKind::Binding { + mutability, + var, + mode: BindingMode::ByValue, + subpattern: None, + .. + } => { + self.local_decls[local].mutability = mutability; + self.local_decls[local].source_info.scope = self.source_scope; + self.local_decls[local].local_info = if let Some(kind) = self_binding { + Some(Box::new(LocalInfo::User(ClearCrossCrate::Set( + BindingForm::ImplicitSelf(*kind), + )))) + } else { + let binding_mode = ty::BindingMode::BindByValue(mutability); + Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var( + VarBindingForm { + binding_mode, + opt_ty_info, + opt_match_place: Some((Some(place), span)), + pat_span: span, + }, + ))))) + }; + self.var_indices.insert(var, LocalsForNode::One(local)); + } + _ => { + scope = self.declare_bindings( + scope, + expr.span, + &pattern, + matches::ArmHasGuard(false), + Some((Some(&place), span)), + ); + let place_builder = PlaceBuilder::from(local); + unpack!(block = self.place_into_pattern(block, pattern, place_builder, false)); } - self.source_scope = original_source_scope; } + self.source_scope = original_source_scope; } // Enter the argument pattern bindings source scope, if it exists. diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 0e82b187201..7940bd1f33d 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -256,23 +256,22 @@ fn visit_pat(&mut self, pat: &Pat<'tcx>) { } PatKind::Binding { mode: BindingMode::ByRef(borrow_kind), ty, .. } => { if self.inside_adt { - if let ty::Ref(_, ty, _) = ty.kind() { - match borrow_kind { - BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => { - if !ty.is_freeze(self.tcx.at(pat.span), self.param_env) { - self.requires_unsafe(pat.span, BorrowOfLayoutConstrainedField); - } - } - BorrowKind::Mut { .. } => { - self.requires_unsafe(pat.span, MutationOfLayoutConstrainedField); - } - } - } else { + let ty::Ref(_, ty, _) = ty.kind() else { span_bug!( pat.span, "BindingMode::ByRef in pattern, but found non-reference type {}", ty ); + }; + match borrow_kind { + BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => { + if !ty.is_freeze(self.tcx.at(pat.span), self.param_env) { + self.requires_unsafe(pat.span, BorrowOfLayoutConstrainedField); + } + } + BorrowKind::Mut { .. } => { + self.requires_unsafe(pat.span, MutationOfLayoutConstrainedField); + } } } visit::walk_pat(self, pat);