Skip to content

Commit f37a6ca

Browse files
authored
Rollup merge of rust-lang#91018 - est31:let_else, r=matthewjasper
Adopt let_else in more places in rustc_mir_build Helps avoid rightward drift. followup of rust-lang#89933
2 parents 3d4f3ee + 8dc8e72 commit f37a6ca

File tree

3 files changed

+66
-69
lines changed

3 files changed

+66
-69
lines changed

compiler/rustc_mir_build/src/build/matches/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1606,13 +1606,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16061606
// encounter a candidate where the test is not relevant; at
16071607
// that point, we stop sorting.
16081608
while let Some(candidate) = candidates.first_mut() {
1609-
if let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) {
1610-
let (candidate, rest) = candidates.split_first_mut().unwrap();
1611-
target_candidates[idx].push(candidate);
1612-
candidates = rest;
1613-
} else {
1609+
let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) else {
16141610
break;
1615-
}
1611+
};
1612+
let (candidate, rest) = candidates.split_first_mut().unwrap();
1613+
target_candidates[idx].push(candidate);
1614+
candidates = rest;
16161615
}
16171616
// at least the first candidate ought to be tested
16181617
assert!(total_candidate_count > candidates.len());

compiler/rustc_mir_build/src/build/mod.rs

+50-51
Original file line numberDiff line numberDiff line change
@@ -966,59 +966,58 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
966966
DropKind::Value,
967967
);
968968

969-
if let Some(arg) = arg_opt {
970-
let pat = match tcx.hir().get(arg.pat.hir_id) {
971-
Node::Pat(pat) | Node::Binding(pat) => pat,
972-
node => bug!("pattern became {:?}", node),
973-
};
974-
let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat);
975-
let original_source_scope = self.source_scope;
976-
let span = pattern.span;
977-
self.set_correct_source_scope_for_arg(arg.hir_id, original_source_scope, span);
978-
match *pattern.kind {
979-
// Don't introduce extra copies for simple bindings
980-
PatKind::Binding {
981-
mutability,
982-
var,
983-
mode: BindingMode::ByValue,
984-
subpattern: None,
985-
..
986-
} => {
987-
self.local_decls[local].mutability = mutability;
988-
self.local_decls[local].source_info.scope = self.source_scope;
989-
self.local_decls[local].local_info = if let Some(kind) = self_binding {
990-
Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(
991-
BindingForm::ImplicitSelf(*kind),
992-
))))
993-
} else {
994-
let binding_mode = ty::BindingMode::BindByValue(mutability);
995-
Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
996-
VarBindingForm {
997-
binding_mode,
998-
opt_ty_info,
999-
opt_match_place: Some((Some(place), span)),
1000-
pat_span: span,
1001-
},
1002-
)))))
1003-
};
1004-
self.var_indices.insert(var, LocalsForNode::One(local));
1005-
}
1006-
_ => {
1007-
scope = self.declare_bindings(
1008-
scope,
1009-
expr.span,
1010-
&pattern,
1011-
matches::ArmHasGuard(false),
1012-
Some((Some(&place), span)),
1013-
);
1014-
let place_builder = PlaceBuilder::from(local);
1015-
unpack!(
1016-
block = self.place_into_pattern(block, pattern, place_builder, false)
1017-
);
1018-
}
969+
let Some(arg) = arg_opt else {
970+
continue;
971+
};
972+
let pat = match tcx.hir().get(arg.pat.hir_id) {
973+
Node::Pat(pat) | Node::Binding(pat) => pat,
974+
node => bug!("pattern became {:?}", node),
975+
};
976+
let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat);
977+
let original_source_scope = self.source_scope;
978+
let span = pattern.span;
979+
self.set_correct_source_scope_for_arg(arg.hir_id, original_source_scope, span);
980+
match *pattern.kind {
981+
// Don't introduce extra copies for simple bindings
982+
PatKind::Binding {
983+
mutability,
984+
var,
985+
mode: BindingMode::ByValue,
986+
subpattern: None,
987+
..
988+
} => {
989+
self.local_decls[local].mutability = mutability;
990+
self.local_decls[local].source_info.scope = self.source_scope;
991+
self.local_decls[local].local_info = if let Some(kind) = self_binding {
992+
Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(
993+
BindingForm::ImplicitSelf(*kind),
994+
))))
995+
} else {
996+
let binding_mode = ty::BindingMode::BindByValue(mutability);
997+
Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
998+
VarBindingForm {
999+
binding_mode,
1000+
opt_ty_info,
1001+
opt_match_place: Some((Some(place), span)),
1002+
pat_span: span,
1003+
},
1004+
)))))
1005+
};
1006+
self.var_indices.insert(var, LocalsForNode::One(local));
1007+
}
1008+
_ => {
1009+
scope = self.declare_bindings(
1010+
scope,
1011+
expr.span,
1012+
&pattern,
1013+
matches::ArmHasGuard(false),
1014+
Some((Some(&place), span)),
1015+
);
1016+
let place_builder = PlaceBuilder::from(local);
1017+
unpack!(block = self.place_into_pattern(block, pattern, place_builder, false));
10191018
}
1020-
self.source_scope = original_source_scope;
10211019
}
1020+
self.source_scope = original_source_scope;
10221021
}
10231022

10241023
// Enter the argument pattern bindings source scope, if it exists.

compiler/rustc_mir_build/src/check_unsafety.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -256,23 +256,22 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
256256
}
257257
PatKind::Binding { mode: BindingMode::ByRef(borrow_kind), ty, .. } => {
258258
if self.inside_adt {
259-
if let ty::Ref(_, ty, _) = ty.kind() {
260-
match borrow_kind {
261-
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {
262-
if !ty.is_freeze(self.tcx.at(pat.span), self.param_env) {
263-
self.requires_unsafe(pat.span, BorrowOfLayoutConstrainedField);
264-
}
265-
}
266-
BorrowKind::Mut { .. } => {
267-
self.requires_unsafe(pat.span, MutationOfLayoutConstrainedField);
268-
}
269-
}
270-
} else {
259+
let ty::Ref(_, ty, _) = ty.kind() else {
271260
span_bug!(
272261
pat.span,
273262
"BindingMode::ByRef in pattern, but found non-reference type {}",
274263
ty
275264
);
265+
};
266+
match borrow_kind {
267+
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {
268+
if !ty.is_freeze(self.tcx.at(pat.span), self.param_env) {
269+
self.requires_unsafe(pat.span, BorrowOfLayoutConstrainedField);
270+
}
271+
}
272+
BorrowKind::Mut { .. } => {
273+
self.requires_unsafe(pat.span, MutationOfLayoutConstrainedField);
274+
}
276275
}
277276
}
278277
visit::walk_pat(self, pat);

0 commit comments

Comments
 (0)