Skip to content

Commit fd76e0e

Browse files
authored
Rollup merge of #97325 - tmiasko:capture-enum-field, r=arora-aman
Fix precise field capture of univariant enums When constructing a MIR from a THIR field expression, introduce an additional downcast projection before accessing a field of an enum. When rebasing a place builder on top of a captured place, account for the fact that a single HIR enum field projection corresponds to two MIR projection elements: a downcast element and a field element. Fixes #95271. Fixes #96299. Fixes #96512. Fixes #97378. r? ``@nikomatsakis`` ``@arora-aman``
2 parents a9c4a7e + 0e7eca7 commit fd76e0e

File tree

5 files changed

+85
-21
lines changed

5 files changed

+85
-21
lines changed

compiler/rustc_middle/src/thir.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,11 @@ pub enum ExprKind<'tcx> {
317317
lhs: ExprId,
318318
rhs: ExprId,
319319
},
320-
/// Access to a struct or tuple field.
320+
/// Access to a field of a struct, a tuple, an union, or an enum.
321321
Field {
322322
lhs: ExprId,
323+
/// Variant containing the field.
324+
variant_index: VariantIdx,
323325
/// This can be a named (`.foo`) or unnamed (`.0`) field.
324326
name: Field,
325327
},

compiler/rustc_middle/src/thir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
8080
visitor.visit_expr(&visitor.thir()[lhs]);
8181
visitor.visit_expr(&visitor.thir()[rhs]);
8282
}
83-
Field { lhs, name: _ } => visitor.visit_expr(&visitor.thir()[lhs]),
83+
Field { lhs, variant_index: _, name: _ } => visitor.visit_expr(&visitor.thir()[lhs]),
8484
Index { lhs, index } => {
8585
visitor.visit_expr(&visitor.thir()[lhs]);
8686
visitor.visit_expr(&visitor.thir()[index]);

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

+48-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
55
use crate::build::{BlockAnd, BlockAndExtension, Builder};
66
use rustc_hir::def_id::DefId;
77
use rustc_hir::HirId;
8+
use rustc_middle::hir::place::Projection as HirProjection;
89
use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
910
use rustc_middle::middle::region;
1011
use rustc_middle::mir::AssertKind::BoundsCheck;
@@ -268,20 +269,52 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
268269
ty::UpvarCapture::ByValue => upvar_resolved_place_builder,
269270
};
270271

271-
let next_projection = capture.place.projections.len();
272-
let mut curr_projections = from_builder.projection;
273-
274272
// We used some of the projections to build the capture itself,
275273
// now we apply the remaining to the upvar resolved place.
276-
upvar_resolved_place_builder
277-
.projection
278-
.extend(curr_projections.drain(next_projection..));
274+
let remaining_projections = strip_prefix(
275+
capture.place.base_ty,
276+
from_builder.projection,
277+
&capture.place.projections,
278+
);
279+
upvar_resolved_place_builder.projection.extend(remaining_projections);
279280

280281
Ok(upvar_resolved_place_builder)
281282
}
282283
}
283284
}
284285

286+
/// Returns projections remaining after stripping an initial prefix of HIR
287+
/// projections.
288+
///
289+
/// Supports only HIR projection kinds that represent a path that might be
290+
/// captured by a closure or a generator, i.e., an `Index` or a `Subslice`
291+
/// projection kinds are unsupported.
292+
fn strip_prefix<'tcx>(
293+
mut base_ty: Ty<'tcx>,
294+
projections: Vec<PlaceElem<'tcx>>,
295+
prefix_projections: &[HirProjection<'tcx>],
296+
) -> impl Iterator<Item = PlaceElem<'tcx>> {
297+
let mut iter = projections.into_iter();
298+
for projection in prefix_projections {
299+
match projection.kind {
300+
HirProjectionKind::Deref => {
301+
assert!(matches!(iter.next(), Some(ProjectionElem::Deref)));
302+
}
303+
HirProjectionKind::Field(..) => {
304+
if base_ty.is_enum() {
305+
assert!(matches!(iter.next(), Some(ProjectionElem::Downcast(..))));
306+
}
307+
assert!(matches!(iter.next(), Some(ProjectionElem::Field(..))));
308+
}
309+
HirProjectionKind::Index | HirProjectionKind::Subslice => {
310+
bug!("unexpected projection kind: {:?}", projection);
311+
}
312+
}
313+
base_ty = projection.ty;
314+
}
315+
iter
316+
}
317+
285318
impl<'tcx> PlaceBuilder<'tcx> {
286319
pub(crate) fn into_place<'a>(
287320
self,
@@ -438,11 +471,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
438471
this.expr_as_place(block, &this.thir[value], mutability, fake_borrow_temps)
439472
})
440473
}
441-
ExprKind::Field { lhs, name } => {
442-
let place_builder = unpack!(
443-
block =
444-
this.expr_as_place(block, &this.thir[lhs], mutability, fake_borrow_temps,)
445-
);
474+
ExprKind::Field { lhs, variant_index, name } => {
475+
let lhs = &this.thir[lhs];
476+
let mut place_builder =
477+
unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
478+
if let ty::Adt(adt_def, _) = lhs.ty.kind() {
479+
if adt_def.is_enum() {
480+
place_builder = place_builder.downcast(*adt_def, variant_index);
481+
}
482+
}
446483
block.and(place_builder.field(name, expr.ty))
447484
}
448485
ExprKind::Deref { arg } => {

compiler/rustc_mir_build/src/thir/cx/expr.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ impl<'tcx> Cx<'tcx> {
591591
}
592592
hir::ExprKind::Field(ref source, ..) => ExprKind::Field {
593593
lhs: self.mirror_expr(source),
594+
variant_index: VariantIdx::new(0),
594595
name: Field::new(tcx.field_index(expr.hir_id, self.typeck_results)),
595596
},
596597
hir::ExprKind::Cast(ref source, ref cast_ty) => {
@@ -994,14 +995,11 @@ impl<'tcx> Cx<'tcx> {
994995
HirProjectionKind::Deref => {
995996
ExprKind::Deref { arg: self.thir.exprs.push(captured_place_expr) }
996997
}
997-
HirProjectionKind::Field(field, ..) => {
998-
// Variant index will always be 0, because for multi-variant
999-
// enums, we capture the enum entirely.
1000-
ExprKind::Field {
1001-
lhs: self.thir.exprs.push(captured_place_expr),
1002-
name: Field::new(field as usize),
1003-
}
1004-
}
998+
HirProjectionKind::Field(field, variant_index) => ExprKind::Field {
999+
lhs: self.thir.exprs.push(captured_place_expr),
1000+
variant_index,
1001+
name: Field::new(field as usize),
1002+
},
10051003
HirProjectionKind::Index | HirProjectionKind::Subslice => {
10061004
// We don't capture these projections, so we can ignore them here
10071005
continue;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// edition:2021
2+
// run-pass
3+
4+
#[derive(Debug, PartialEq, Eq)]
5+
pub enum Color {
6+
RGB(u8, u8, u8),
7+
}
8+
9+
fn main() {
10+
let mut color = Color::RGB(0, 0, 0);
11+
let mut red = |v| {
12+
let Color::RGB(ref mut r, _, _) = color;
13+
*r = v;
14+
};
15+
let mut green = |v| {
16+
let Color::RGB(_, ref mut g, _) = color;
17+
*g = v;
18+
};
19+
let mut blue = |v| {
20+
let Color::RGB(_, _, ref mut b) = color;
21+
*b = v;
22+
};
23+
red(1);
24+
green(2);
25+
blue(3);
26+
assert_eq!(Color::RGB(1, 2, 3), color);
27+
}

0 commit comments

Comments
 (0)