diff options
| author | est31 <MTest31@outlook.com> | 2022-02-02 12:45:20 +0100 |
|---|---|---|
| committer | est31 <MTest31@outlook.com> | 2022-02-02 17:11:01 +0100 |
| commit | 670f5c6ef385f251df5cd7bcba9e8039a80bdb4d (patch) | |
| tree | cdf352502795e4fb84b125e3a9c6f4162f6f4d24 /compiler/rustc_mir_build | |
| parent | d5f9c40e6a9ecc62432e71e886cef83a4c2c9b98 (diff) | |
| download | rust-670f5c6ef385f251df5cd7bcba9e8039a80bdb4d.tar.gz rust-670f5c6ef385f251df5cd7bcba9e8039a80bdb4d.zip | |
More let_else adoptions
Diffstat (limited to 'compiler/rustc_mir_build')
| -rw-r--r-- | compiler/rustc_mir_build/src/build/matches/mod.rs | 47 | ||||
| -rw-r--r-- | compiler/rustc_mir_build/src/build/matches/test.rs | 94 |
2 files changed, 67 insertions, 74 deletions
diff --git a/compiler/rustc_mir_build/src/build/matches/mod.rs b/compiler/rustc_mir_build/src/build/matches/mod.rs index 3294f2cf641..6329bcee4fa 100644 --- a/compiler/rustc_mir_build/src/build/matches/mod.rs +++ b/compiler/rustc_mir_build/src/build/matches/mod.rs @@ -602,33 +602,30 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { for binding in &candidate_ref.bindings { let local = self.var_local_id(binding.var_id, OutsideGuard); - if let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var( + let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var( VarBindingForm { opt_match_place: Some((ref mut match_place, _)), .. }, - )))) = self.local_decls[local].local_info - { - // `try_upvars_resolved` may fail if it is unable to resolve the given - // `PlaceBuilder` inside a closure. In this case, we don't want to include - // a scrutinee place. `scrutinee_place_builder` will fail for destructured - // assignments. This is because a closure only captures the precise places - // that it will read and as a result a closure may not capture the entire - // tuple/struct and rather have individual places that will be read in the - // final MIR. - // Example: - // ``` - // let foo = (0, 1); - // let c = || { - // let (v1, v2) = foo; - // }; - // ``` - if let Ok(match_pair_resolved) = - initializer.clone().try_upvars_resolved(self.tcx, self.typeck_results) - { - let place = - match_pair_resolved.into_place(self.tcx, self.typeck_results); - *match_place = Some(place); - } - } else { + )))) = self.local_decls[local].local_info else { bug!("Let binding to non-user variable.") + }; + // `try_upvars_resolved` may fail if it is unable to resolve the given + // `PlaceBuilder` inside a closure. In this case, we don't want to include + // a scrutinee place. `scrutinee_place_builder` will fail for destructured + // assignments. This is because a closure only captures the precise places + // that it will read and as a result a closure may not capture the entire + // tuple/struct and rather have individual places that will be read in the + // final MIR. + // Example: + // ``` + // let foo = (0, 1); + // let c = || { + // let (v1, v2) = foo; + // }; + // ``` + if let Ok(match_pair_resolved) = + initializer.clone().try_upvars_resolved(self.tcx, self.typeck_results) + { + let place = match_pair_resolved.into_place(self.tcx, self.typeck_results); + *match_place = Some(place); } } // All of the subcandidates should bind the same locals, so we diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs index f4bf28bfa5c..49cd21c2137 100644 --- a/compiler/rustc_mir_build/src/build/matches/test.rs +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -227,16 +227,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let target_blocks = make_target_blocks(self); let terminator = if *switch_ty.kind() == ty::Bool { assert!(!options.is_empty() && options.len() <= 2); - if let [first_bb, second_bb] = *target_blocks { - let (true_bb, false_bb) = match options[0] { - 1 => (first_bb, second_bb), - 0 => (second_bb, first_bb), - v => span_bug!(test.span, "expected boolean value but got {:?}", v), - }; - TerminatorKind::if_(self.tcx, Operand::Copy(place), true_bb, false_bb) - } else { + let [first_bb, second_bb] = *target_blocks else { bug!("`TestKind::SwitchInt` on `bool` should have two targets") - } + }; + let (true_bb, false_bb) = match options[0] { + 1 => (first_bb, second_bb), + 0 => (second_bb, first_bb), + v => span_bug!(test.span, "expected boolean value but got {:?}", v), + }; + TerminatorKind::if_(self.tcx, Operand::Copy(place), true_bb, false_bb) } else { // The switch may be inexhaustive so we have a catch all block debug_assert_eq!(options.len() + 1, target_blocks.len()); @@ -285,24 +284,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let hi = self.literal_operand(test.span, hi); let val = Operand::Copy(place); - if let [success, fail] = *target_blocks { - self.compare( - block, - lower_bound_success, - fail, - source_info, - BinOp::Le, - lo, - val.clone(), - ); - let op = match *end { - RangeEnd::Included => BinOp::Le, - RangeEnd::Excluded => BinOp::Lt, - }; - self.compare(lower_bound_success, success, fail, source_info, op, val, hi); - } else { + let [success, fail] = *target_blocks else { bug!("`TestKind::Range` should have two target blocks"); - } + }; + self.compare( + block, + lower_bound_success, + fail, + source_info, + BinOp::Le, + lo, + val.clone(), + ); + let op = match *end { + RangeEnd::Included => BinOp::Le, + RangeEnd::Excluded => BinOp::Lt, + }; + self.compare(lower_bound_success, success, fail, source_info, op, val, hi); } TestKind::Len { len, op } => { @@ -317,21 +315,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // expected = <N> let expected = self.push_usize(block, source_info, len); - if let [true_bb, false_bb] = *target_blocks { - // result = actual == expected OR result = actual < expected - // branch based on result - self.compare( - block, - true_bb, - false_bb, - source_info, - op, - Operand::Move(actual), - Operand::Move(expected), - ); - } else { + let [true_bb, false_bb] = *target_blocks else { bug!("`TestKind::Len` should have two target blocks"); - } + }; + // result = actual == expected OR result = actual < expected + // branch based on result + self.compare( + block, + true_bb, + false_bb, + source_info, + op, + Operand::Move(actual), + Operand::Move(expected), + ); } } } @@ -459,16 +456,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ); self.diverge_from(block); - if let [success_block, fail_block] = *make_target_blocks(self) { - // check the result - self.cfg.terminate( - eq_block, - source_info, - TerminatorKind::if_(self.tcx, Operand::Move(eq_result), success_block, fail_block), - ); - } else { + let [success_block, fail_block] = *make_target_blocks(self) else { bug!("`TestKind::Eq` should have two target blocks") - } + }; + // check the result + self.cfg.terminate( + eq_block, + source_info, + TerminatorKind::if_(self.tcx, Operand::Move(eq_result), success_block, fail_block), + ); } /// Given that we are performing `test` against `test_place`, this job |
