Skip to content

Commit be5b777

Browse files
committed
Replace try_upvars_resolved with try_to_place
1 parent 1c81979 commit be5b777

File tree

5 files changed

+35
-51
lines changed

5 files changed

+35
-51
lines changed

compiler/rustc_mir_build/src/build/expr/as_place.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,13 @@ impl<'tcx> PlaceBuilder<'tcx> {
267267
}
268268
}
269269

270-
pub(in crate::build) fn try_upvars_resolved(
271-
self,
272-
cx: &Builder<'_, 'tcx>,
273-
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
274-
match self.base {
275-
PlaceBase::Local(_) => Ok(self),
276-
PlaceBase::Upvar { .. } => self.resolve_upvar(cx).ok_or(self),
277-
}
270+
/// Creates a `Place` or returns `None` if an upvar cannot be resolved
271+
pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option<Place<'tcx>> {
272+
let resolved = self.resolve_upvar(cx);
273+
let builder = resolved.as_ref().unwrap_or(self);
274+
let PlaceBase::Local(local) = builder.base else { return None };
275+
let projection = cx.tcx.intern_place_elems(&builder.projection);
276+
Some(Place { local, projection })
278277
}
279278

280279
/// Attempts to resolve the `PlaceBuilder`.

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
369369
let place_builder =
370370
unpack!(block = this.as_place_builder(block, &this.thir[*thir_place]));
371371

372-
if let Ok(place_builder_resolved) = place_builder.try_upvars_resolved(this) {
373-
let mir_place = place_builder_resolved.into_place(this);
372+
if let Some(mir_place) = place_builder.try_to_place(this) {
374373
this.cfg.push_fake_read(
375374
block,
376375
this.source_info(this.tcx.hir().span(*hir_id)),

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
220220
let cause_matched_place = FakeReadCause::ForMatchedPlace(None);
221221
let source_info = self.source_info(scrutinee_span);
222222

223-
if let Ok(scrutinee_builder) = scrutinee_place_builder.clone().try_upvars_resolved(self) {
224-
let scrutinee_place = scrutinee_builder.into_place(self);
223+
if let Some(scrutinee_place) = scrutinee_place_builder.try_to_place(self) {
225224
self.cfg.push_fake_read(block, source_info, cause_matched_place, scrutinee_place);
226225
}
227226

@@ -334,7 +333,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
334333
let arm_scope = (arm.scope, arm_source_info);
335334
let match_scope = self.local_scope();
336335
self.in_scope(arm_scope, arm.lint_level, |this| {
337-
// `try_upvars_resolved` may fail if it is unable to resolve the given
336+
// `try_to_place` may fail if it is unable to resolve the given
338337
// `PlaceBuilder` inside a closure. In this case, we don't want to include
339338
// a scrutinee place. `scrutinee_place_builder` will fail to be resolved
340339
// if the only match arm is a wildcard (`_`).
@@ -345,14 +344,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
345344
// match foo { _ => () };
346345
// };
347346
// ```
348-
let mut opt_scrutinee_place: Option<(Option<&Place<'tcx>>, Span)> = None;
349-
let scrutinee_place: Place<'tcx>;
350-
if let Ok(scrutinee_builder) =
351-
scrutinee_place_builder.clone().try_upvars_resolved(this)
352-
{
353-
scrutinee_place = scrutinee_builder.into_place(this);
354-
opt_scrutinee_place = Some((Some(&scrutinee_place), scrutinee_span));
355-
}
347+
let scrutinee_place = scrutinee_place_builder.try_to_place(this);
348+
let opt_scrutinee_place =
349+
scrutinee_place.as_ref().map(|place| (Some(place), scrutinee_span));
356350
let scope = this.declare_bindings(
357351
None,
358352
arm.span,
@@ -591,7 +585,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
591585
while let Some(next) = {
592586
for binding in &candidate_ref.bindings {
593587
let local = self.var_local_id(binding.var_id, OutsideGuard);
594-
// `try_upvars_resolved` may fail if it is unable to resolve the given
588+
// `try_to_place` may fail if it is unable to resolve the given
595589
// `PlaceBuilder` inside a closure. In this case, we don't want to include
596590
// a scrutinee place. `scrutinee_place_builder` will fail for destructured
597591
// assignments. This is because a closure only captures the precise places
@@ -605,9 +599,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
605599
// let (v1, v2) = foo;
606600
// };
607601
// ```
608-
if let Ok(match_pair_resolved) = initializer.clone().try_upvars_resolved(self) {
609-
let place = match_pair_resolved.into_place(self);
610-
602+
if let Some(place) = initializer.try_to_place(self) {
611603
let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
612604
VarBindingForm { opt_match_place: Some((ref mut match_place, _)), .. },
613605
)))) = self.local_decls[local].local_info else {
@@ -1594,10 +1586,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15941586
}
15951587

15961588
// Insert a Shallow borrow of any places that is switched on.
1597-
if let Some(fb) = fake_borrows && let Ok(match_place_resolved) =
1598-
match_place.clone().try_upvars_resolved(self)
1589+
if let Some(fb) = fake_borrows
1590+
&& let Some(resolved_place) = match_place.try_to_place(self)
15991591
{
1600-
let resolved_place = match_place_resolved.into_place(self);
16011592
fb.insert(resolved_place);
16021593
}
16031594

@@ -1788,12 +1779,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17881779
false,
17891780
&mut [&mut guard_candidate, &mut otherwise_candidate],
17901781
);
1791-
let mut opt_expr_place: Option<(Option<&Place<'tcx>>, Span)> = None;
1792-
let expr_place: Place<'tcx>;
1793-
if let Ok(expr_builder) = expr_place_builder.try_upvars_resolved(self) {
1794-
expr_place = expr_builder.into_place(self);
1795-
opt_expr_place = Some((Some(&expr_place), expr_span));
1796-
}
1782+
let expr_place = expr_place_builder.try_to_place(self);
1783+
let opt_expr_place = expr_place.as_ref().map(|place| (Some(place), expr_span));
17971784
let otherwise_post_guard_block = otherwise_candidate.pre_binding_block.unwrap();
17981785
self.break_for_else(otherwise_post_guard_block, else_target, self.source_info(expr_span));
17991786

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
156156
ascription: thir::Ascription { ref annotation, variance },
157157
} => {
158158
// Apply the type ascription to the value at `match_pair.place`, which is the
159-
if let Ok(place_resolved) = match_pair.place.clone().try_upvars_resolved(self) {
159+
if let Some(source) = match_pair.place.try_to_place(self) {
160160
candidate.ascriptions.push(Ascription {
161161
annotation: annotation.clone(),
162-
source: place_resolved.into_place(self),
162+
source,
163163
variance,
164164
});
165165
}
@@ -183,10 +183,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
183183
ref subpattern,
184184
is_primary: _,
185185
} => {
186-
if let Ok(place_resolved) = match_pair.place.clone().try_upvars_resolved(self) {
186+
if let Some(source) = match_pair.place.try_to_place(self) {
187187
candidate.bindings.push(Binding {
188188
span: match_pair.pattern.span,
189-
source: place_resolved.into_place(self),
189+
source,
190190
var_id: var,
191191
binding_mode: mode,
192192
});

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3333
suffix: &'pat [Box<Pat<'tcx>>],
3434
) {
3535
let tcx = self.tcx;
36-
let (min_length, exact_size) =
37-
if let Ok(place_resolved) = place.clone().try_upvars_resolved(self) {
38-
match place_resolved.into_place(self).ty(&self.local_decls, tcx).ty.kind() {
39-
ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
40-
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
41-
}
42-
} else {
43-
((prefix.len() + suffix.len()).try_into().unwrap(), false)
44-
};
36+
let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
37+
match place_resolved.ty(&self.local_decls, tcx).ty.kind() {
38+
ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
39+
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
40+
}
41+
} else {
42+
((prefix.len() + suffix.len()).try_into().unwrap(), false)
43+
};
4544

4645
match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
4746
let elem =
@@ -97,15 +96,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9796

9897
impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
9998
pub(in crate::build) fn new(
100-
place: PlaceBuilder<'tcx>,
99+
mut place: PlaceBuilder<'tcx>,
101100
pattern: &'pat Pat<'tcx>,
102101
cx: &Builder<'_, 'tcx>,
103102
) -> MatchPair<'pat, 'tcx> {
104103
// Force the place type to the pattern's type.
105104
// FIXME(oli-obk): can we use this to simplify slice/array pattern hacks?
106-
let mut place = match place.try_upvars_resolved(cx) {
107-
Ok(val) | Err(val) => val,
108-
};
105+
if let Some(resolved) = place.resolve_upvar(cx) {
106+
place = resolved;
107+
}
109108

110109
// Only add the OpaqueCast projection if the given place is an opaque type and the
111110
// expected type from the pattern is not.

0 commit comments

Comments
 (0)