From b2656e7420ade8c79fb6d53de700624fd1337971 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 2 Nov 2022 13:37:37 +0100 Subject: [PATCH 1/4] use non-ascribed field ty in place field projection --- compiler/rustc_middle/src/mir/mod.rs | 4 +- compiler/rustc_middle/src/mir/syntax.rs | 39 +- compiler/rustc_middle/src/mir/tcx.rs | 15 +- .../src/build/expr/as_place.rs | 355 ++++++++++++++---- .../src/build/expr/as_rvalue.rs | 9 +- .../rustc_mir_build/src/build/expr/into.rs | 8 +- .../src/build/matches/simplify.rs | 6 +- .../rustc_mir_build/src/build/matches/test.rs | 3 +- .../rustc_mir_build/src/build/matches/util.rs | 11 +- .../src/move_paths/abs_domain.rs | 2 +- .../rustc_mir_dataflow/src/value_analysis.rs | 4 +- src/test/ui/mir/field-projection-invariant.rs | 24 ++ src/test/ui/mir/field-ty-ascription-enums.rs | 15 + src/test/ui/mir/field-ty-ascription.rs | 37 ++ 14 files changed, 433 insertions(+), 99 deletions(-) create mode 100644 src/test/ui/mir/field-projection-invariant.rs create mode 100644 src/test/ui/mir/field-ty-ascription-enums.rs create mode 100644 src/test/ui/mir/field-ty-ascription.rs diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index bdaa586c69879..e909b2f74aa16 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -1488,7 +1488,7 @@ impl<'tcx> StatementKind<'tcx> { /////////////////////////////////////////////////////////////////////////// // Places -impl ProjectionElem { +impl ProjectionElem { /// Returns `true` if the target of this projection may refer to a different region of memory /// than the base. fn is_indirect(&self) -> bool { @@ -1517,7 +1517,7 @@ impl ProjectionElem { /// Alias for projections as they appear in `UserTypeProjection`, where we /// need neither the `V` parameter for `Index` nor the `T` for `Field`. -pub type ProjectionKind = ProjectionElem<(), ()>; +pub type ProjectionKind = ProjectionElem<(), (), ()>; rustc_index::newtype_index! { /// A [newtype'd][wrapper] index type in the MIR [control-flow graph][CFG] diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 99e59c770d754..a6ca04f5e627a 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -890,11 +890,18 @@ pub struct Place<'tcx> { pub projection: &'tcx List>, } +/// The different kinds of projections that can be used in the projection of a `Place`. +/// +/// `T1` is the generic type for a field projection. For an actual projection on a `Place` +/// this parameter will always be `Ty`, but the field type can be unavailable when +/// building (by using `PlaceBuilder`) places that correspond to upvars. +/// `T2` is the generic type for an `OpaqueCast` (is generic since it's abstracted over +/// in dataflow analysis, see `AbstractElem`). #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)] -pub enum ProjectionElem { +pub enum ProjectionElem { Deref, - Field(Field, T), + Field(Field, T1), /// Index into a slice/array. /// /// Note that this does not also dereference, and so it does not exactly correspond to slice @@ -950,12 +957,36 @@ pub enum ProjectionElem { /// Like an explicit cast from an opaque type to a concrete type, but without /// requiring an intermediate variable. - OpaqueCast(T), + OpaqueCast(T2), } /// Alias for projections as they appear in places, where the base is a place /// and the index is a local. -pub type PlaceElem<'tcx> = ProjectionElem>; +pub type PlaceElem<'tcx> = ProjectionElem, Ty<'tcx>>; + +/// Alias for projections that appear in `PlaceBuilder::Upvar`, for which +/// we cannot provide any field types. +pub type UpvarProjectionElem<'tcx> = ProjectionElem>; + +impl<'tcx> From> for UpvarProjectionElem<'tcx> { + fn from(elem: PlaceElem<'tcx>) -> Self { + match elem { + ProjectionElem::Deref => ProjectionElem::Deref, + ProjectionElem::Field(field, _) => ProjectionElem::Field(field, ()), + ProjectionElem::Index(v) => ProjectionElem::Index(v), + ProjectionElem::ConstantIndex { offset, min_length, from_end } => { + ProjectionElem::ConstantIndex { offset, min_length, from_end } + } + ProjectionElem::Subslice { from, to, from_end } => { + ProjectionElem::Subslice { from, to, from_end } + } + ProjectionElem::Downcast(opt_sym, variant_idx) => { + ProjectionElem::Downcast(opt_sym, variant_idx) + } + ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty), + } + } +} /////////////////////////////////////////////////////////////////////////// // Operands diff --git a/compiler/rustc_middle/src/mir/tcx.rs b/compiler/rustc_middle/src/mir/tcx.rs index 8d2a8f33d6aa9..1e289fc4abec3 100644 --- a/compiler/rustc_middle/src/mir/tcx.rs +++ b/compiler/rustc_middle/src/mir/tcx.rs @@ -28,8 +28,8 @@ impl<'tcx> PlaceTy<'tcx> { /// `place_ty.field_ty(tcx, f)` computes the type at a given field /// of a record or enum-variant. (Most clients of `PlaceTy` can /// instead just extract the relevant type directly from their - /// `PlaceElem`, but some instances of `ProjectionElem` do - /// not carry a `Ty` for `T`.) + /// `PlaceElem`, but some instances of `ProjectionElem` do + /// not carry a `Ty` for `T1` or `T2`.) /// /// Note that the resulting type has not been normalized. pub fn field_ty(self, tcx: TyCtxt<'tcx>, f: Field) -> Ty<'tcx> { @@ -64,17 +64,18 @@ impl<'tcx> PlaceTy<'tcx> { /// `Ty` or downcast variant corresponding to that projection. /// The `handle_field` callback must map a `Field` to its `Ty`, /// (which should be trivial when `T` = `Ty`). - pub fn projection_ty_core( + pub fn projection_ty_core( self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, - elem: &ProjectionElem, - mut handle_field: impl FnMut(&Self, Field, T) -> Ty<'tcx>, - mut handle_opaque_cast: impl FnMut(&Self, T) -> Ty<'tcx>, + elem: &ProjectionElem, + mut handle_field: impl FnMut(&Self, Field, T1) -> Ty<'tcx>, + mut handle_opaque_cast: impl FnMut(&Self, T2) -> Ty<'tcx>, ) -> PlaceTy<'tcx> where V: ::std::fmt::Debug, - T: ::std::fmt::Debug + Copy, + T1: ::std::fmt::Debug + Copy, + T2: ::std::fmt::Debug + Copy, { if self.variant_index.is_some() && !matches!(elem, ProjectionElem::Field(..)) { bug!("cannot use non field projection on downcasted place") diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index edd527286264a..8a35478dd8b34 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -7,6 +7,7 @@ use rustc_hir::def_id::LocalDefId; use rustc_middle::hir::place::Projection as HirProjection; use rustc_middle::hir::place::ProjectionKind as HirProjectionKind; use rustc_middle::middle::region; +use rustc_middle::mir::tcx::PlaceTy; use rustc_middle::mir::AssertKind::BoundsCheck; use rustc_middle::mir::*; use rustc_middle::thir::*; @@ -18,23 +19,31 @@ use rustc_target::abi::VariantIdx; use rustc_index::vec::Idx; use std::assert_matches::assert_matches; +use std::convert::From; use std::iter; -/// The "outermost" place that holds this value. -#[derive(Copy, Clone, Debug, PartialEq)] -pub(crate) enum PlaceBase { +/// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a +/// place by pushing more and more projections onto the end, and then convert the final set into a +/// place using the `into_place` method. +/// +/// This is used internally when building a place for an expression like `a.b.c`. The fields `b` +/// and `c` can be progressively pushed onto the place builder that is created when converting `a`. +#[derive(Clone, Debug, PartialEq)] +pub(in crate::build) enum PlaceBuilder<'tcx> { /// Denotes the start of a `Place`. - Local(Local), + /// + /// We use `PlaceElem` since this has all `Field` types available. + Local { local: Local, projection: Vec> }, /// When building place for an expression within a closure, the place might start off a /// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture /// index (within the desugared closure) of the captured path until most of the projections - /// are applied. We use `PlaceBase::Upvar` to keep track of the root variable off of which the + /// are applied. We use `PlaceBuilder::Upvar` to keep track of the root variable off of which the /// captured path starts, the closure the capture belongs to and the trait the closure /// implements. /// - /// Once we have figured out the capture index, we can convert the place builder to start from - /// `PlaceBase::Local`. + /// Once we have figured out the capture index, we can convert the place builder to + /// `PlaceBuilder::Local`. /// /// Consider the following example /// ```rust @@ -55,24 +64,16 @@ pub(crate) enum PlaceBase { /// /// When `capture_disjoint_fields` is enabled, `t.0.0.0` is captured and we won't be able to /// figure out that it is captured until all the `Field` projections are applied. - Upvar { - /// HirId of the upvar - var_hir_id: LocalVarId, - /// DefId of the closure - closure_def_id: LocalDefId, - }, + /// + /// Note: in contrast to `PlaceBuilder::Local` we have not yet determined all `Field` types + /// and will only do so once converting to `PlaceBuilder::Local`. + Upvar { upvar: Upvar, projection: Vec> }, } -/// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a -/// place by pushing more and more projections onto the end, and then convert the final set into a -/// place using the `to_place` method. -/// -/// This is used internally when building a place for an expression like `a.b.c`. The fields `b` -/// and `c` can be progressively pushed onto the place builder that is created when converting `a`. -#[derive(Clone, Debug, PartialEq)] -pub(in crate::build) struct PlaceBuilder<'tcx> { - base: PlaceBase, - projection: Vec>, +#[derive(Copy, Clone, Debug, PartialEq)] +pub(crate) struct Upvar { + var_hir_id: LocalVarId, + closure_def_id: LocalDefId, } /// Given a list of MIR projections, convert them to list of HIR ProjectionKind. @@ -82,7 +83,7 @@ pub(in crate::build) struct PlaceBuilder<'tcx> { /// part of a path that is captured by a closure. We stop applying projections once we see the first /// projection that isn't captured by a closure. fn convert_to_hir_projections_and_truncate_for_capture<'tcx>( - mir_projections: &[PlaceElem<'tcx>], + mir_projections: &[UpvarProjectionElem<'tcx>], ) -> Vec { let mut hir_projections = Vec::new(); let mut variant = None; @@ -156,7 +157,7 @@ fn is_ancestor_or_same_capture( fn find_capture_matching_projections<'a, 'tcx>( upvars: &'a CaptureMap<'tcx>, var_hir_id: LocalVarId, - projections: &[PlaceElem<'tcx>], + projections: &[UpvarProjectionElem<'tcx>], ) -> Option<(usize, &'a Capture<'tcx>)> { let hir_projections = convert_to_hir_projections_and_truncate_for_capture(projections); @@ -174,7 +175,7 @@ fn to_upvars_resolved_place_builder<'tcx>( cx: &Builder<'_, 'tcx>, var_hir_id: LocalVarId, closure_def_id: LocalDefId, - projection: &[PlaceElem<'tcx>], + projection: &[UpvarProjectionElem<'tcx>], ) -> Option> { let Some((capture_index, capture)) = find_capture_matching_projections( @@ -196,23 +197,32 @@ fn to_upvars_resolved_place_builder<'tcx>( var_hir_id, projection, ); } + return None; }; // Access the capture by accessing the field within the Closure struct. let capture_info = &cx.upvars[capture_index]; - let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place); + let Place { local: upvar_resolved_local, projection: local_projection } = + capture_info.use_place; // We used some of the projections to build the capture itself, // now we apply the remaining to the upvar resolved place. - trace!(?capture.captured_place, ?projection); - let remaining_projections = strip_prefix( + let upvar_projection = strip_prefix( capture.captured_place.place.base_ty, projection, &capture.captured_place.place.projections, ); - upvar_resolved_place_builder.projection.extend(remaining_projections); + + let upvar_resolved_place_builder = PlaceBuilder::construct_local_place_builder( + cx, + upvar_resolved_local, + local_projection.as_slice(), + upvar_projection, + ); + + assert!(matches!(upvar_resolved_place_builder, PlaceBuilder::Local { .. })); Some(upvar_resolved_place_builder) } @@ -225,15 +235,17 @@ fn to_upvars_resolved_place_builder<'tcx>( /// projection kinds are unsupported. fn strip_prefix<'a, 'tcx>( mut base_ty: Ty<'tcx>, - projections: &'a [PlaceElem<'tcx>], + projections: &'a [UpvarProjectionElem<'tcx>], prefix_projections: &[HirProjection<'tcx>], -) -> impl Iterator> + 'a { +) -> impl Iterator> + 'a { let mut iter = projections .iter() .copied() // Filter out opaque casts, they are unnecessary in the prefix. .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..))); for projection in prefix_projections { + debug!(?projection, ?projection.ty); + match projection.kind { HirProjectionKind::Deref => { assert_matches!(iter.next(), Some(ProjectionElem::Deref)); @@ -248,8 +260,10 @@ fn strip_prefix<'a, 'tcx>( bug!("unexpected projection kind: {:?}", projection); } } + base_ty = projection.ty; } + iter } @@ -262,9 +276,9 @@ impl<'tcx> PlaceBuilder<'tcx> { pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option> { let resolved = self.resolve_upvar(cx); let builder = resolved.as_ref().unwrap_or(self); - let PlaceBase::Local(local) = builder.base else { return None }; - let projection = cx.tcx.intern_place_elems(&builder.projection); - Some(Place { local, projection }) + let PlaceBuilder::Local{local, ref projection} = builder else { return None }; + let projection = cx.tcx.intern_place_elems(projection); + Some(Place { local: *local, projection }) } /// Attempts to resolve the `PlaceBuilder`. @@ -281,22 +295,31 @@ impl<'tcx> PlaceBuilder<'tcx> { &self, cx: &Builder<'_, 'tcx>, ) -> Option> { - let PlaceBase::Upvar { var_hir_id, closure_def_id } = self.base else { + let PlaceBuilder::Upvar{ upvar: Upvar {var_hir_id, closure_def_id }, projection} = self else { return None; }; - to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &self.projection) - } - pub(crate) fn base(&self) -> PlaceBase { - self.base + to_upvars_resolved_place_builder(cx, *var_hir_id, *closure_def_id, &projection) } - pub(crate) fn projection(&self) -> &[PlaceElem<'tcx>] { - &self.projection - } + #[instrument(skip(cx), level = "debug")] + pub(crate) fn field(self, cx: &Builder<'_, 'tcx>, f: Field) -> Self { + match self.clone() { + PlaceBuilder::Local { local, projection } => { + let base_place = PlaceBuilder::Local { local, projection }; + let PlaceTy { ty, variant_index } = + base_place.to_place(cx).ty(&cx.local_decls, cx.tcx); + let base_ty = cx.tcx.normalize_erasing_regions(cx.param_env, ty); + + let field_ty = PlaceBuilder::compute_field_ty(cx, f, base_ty, variant_index); - pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self { - self.project(PlaceElem::Field(f, ty)) + self.project(ProjectionElem::Field(f, field_ty)) + } + PlaceBuilder::Upvar { upvar, mut projection } => { + projection.push(ProjectionElem::Field(f, ())); + PlaceBuilder::Upvar { upvar, projection } + } + } } pub(crate) fn deref(self) -> Self { @@ -311,35 +334,236 @@ impl<'tcx> PlaceBuilder<'tcx> { self.project(PlaceElem::Index(index)) } - pub(crate) fn project(mut self, elem: PlaceElem<'tcx>) -> Self { - self.projection.push(elem); - self + #[instrument(level = "debug")] + pub(crate) fn project(self, elem: PlaceElem<'tcx>) -> Self { + let result = match self { + PlaceBuilder::Local { local, mut projection } => { + projection.push(elem); + PlaceBuilder::Local { local, projection } + } + PlaceBuilder::Upvar { upvar, mut projection } => { + projection.push(elem.into()); + PlaceBuilder::Upvar { upvar, projection } + } + }; + + debug!(?result); + result } /// Same as `.clone().project(..)` but more efficient pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self { - Self { - base: self.base, - projection: Vec::from_iter(self.projection.iter().copied().chain([elem])), + match self { + PlaceBuilder::Local { local, projection } => PlaceBuilder::Local { + local: *local, + projection: Vec::from_iter(projection.iter().copied().chain([elem.into()])), + }, + PlaceBuilder::Upvar { upvar, projection } => PlaceBuilder::Upvar { + upvar: *upvar, + projection: Vec::from_iter(projection.iter().copied().chain([elem.into()])), + }, } } + + /// Similar to `Place::ty` but needed during mir building. + /// + /// Applies the projections in the `PlaceBuilder` to the base + /// type. + /// + /// Fallible as the root of this place may be an upvar for + /// which no base type can be determined. + #[instrument(skip(cx), level = "debug")] + fn compute_field_ty( + cx: &Builder<'_, 'tcx>, + field: Field, + base_ty: Ty<'tcx>, + variant_index: Option, + ) -> Ty<'tcx> { + let field_idx = field.as_usize(); + let field_ty = match base_ty.kind() { + ty::Adt(adt_def, substs) if adt_def.is_enum() => { + let variant_idx = variant_index.unwrap(); + adt_def.variant(variant_idx).fields[field_idx].ty(cx.tcx, substs) + } + ty::Adt(adt_def, substs) => adt_def + .all_fields() + .nth(field_idx) + .unwrap_or_else(|| { + bug!( + "expected to take field with idx {:?} of fields of {:?}", + field_idx, + adt_def + ) + }) + .ty(cx.tcx, substs), + ty::Tuple(elems) => elems.iter().nth(field_idx).unwrap_or_else(|| { + bug!("expected to take field with idx {:?} of {:?}", field_idx, elems) + }), + ty::Closure(_, substs) => { + let substs = substs.as_closure(); + let Some(f_ty) = substs.upvar_tys().nth(field_idx) else { + bug!("expected to take field with idx {:?} of {:?}", field_idx, substs.upvar_tys().collect::>()); + }; + + f_ty + } + &ty::Generator(def_id, substs, _) => { + if let Some(var) = variant_index { + let gen_body = cx.tcx.optimized_mir(def_id); + let Some(layout) = gen_body.generator_layout() else { + bug!("No generator layout for {:?}", base_ty); + }; + + let Some(&local) = layout.variant_fields[var].get(field) else { + bug!("expected to take field {:?} of {:?}", field, layout.variant_fields[var]); + }; + + let Some(&f_ty) = layout.field_tys.get(local) else { + bug!("expected to get element for {:?} in {:?}", local, layout.field_tys); + }; + + f_ty + } else { + let Some(f_ty) = substs.as_generator().prefix_tys().nth(field.index()) else { + bug!( + "expected to take index {:?} in {:?}", + field.index(), + substs.as_generator().prefix_tys().collect::>() + ); + }; + + f_ty + } + } + _ => bug!("couldn't create field type, unexpected base type: {:?}", base_ty), + }; + + cx.tcx.normalize_erasing_regions(cx.param_env, field_ty) + } + + /// Creates a `PlaceBuilder::Local` from a `PlaceBuilder::Upvar` whose upvars + /// are resolved. This function takes two kinds of projections: `local_projection` + /// contains the projections of the captured upvar and `upvar_projection` the + /// projections that are applied to the captured upvar. The main purpose of this + /// function is to figure out the `Ty`s of the field projections in `upvar_projection`. + #[instrument(skip(cx, local, upvar_projection))] + fn construct_local_place_builder( + cx: &Builder<'_, 'tcx>, + local: Local, + local_projection: &[PlaceElem<'tcx>], + upvar_projection: impl Iterator>, + ) -> Self { + // We maintain a `Ty` to which we apply a projection in each iteration over `upvar_projection`. + // This `ancestor_ty` let's us infer the field type whenever we encounter a + // `ProjectionElem::Field`. + let (mut ancestor_ty, mut opt_variant_idx) = + local_projections_to_ty(cx, local, local_projection); + + // We add all projection elements we encounter to this `Vec`. + let mut local_projection = local_projection.to_vec(); + + for (i, proj) in upvar_projection.enumerate() { + debug!("i: {:?}, proj: {:?}, local_projection: {:?}", i, proj, local_projection); + match proj { + ProjectionElem::Field(field, _) => { + let field_ty = + PlaceBuilder::compute_field_ty(cx, field, ancestor_ty, opt_variant_idx); + debug!(?field_ty); + + local_projection.push(ProjectionElem::Field(field, field_ty)); + ancestor_ty = field_ty; + opt_variant_idx = None; + } + _ => { + let proj = upvar_proj_to_place_elem_no_field_proj(proj); + (ancestor_ty, opt_variant_idx) = project_ty(cx.tcx, ancestor_ty, proj); + local_projection.push(proj); + } + } + } + + PlaceBuilder::Local { local, projection: local_projection } + } } impl<'tcx> From for PlaceBuilder<'tcx> { fn from(local: Local) -> Self { - Self { base: PlaceBase::Local(local), projection: Vec::new() } + Self::Local { local, projection: Vec::new() } } } -impl<'tcx> From for PlaceBuilder<'tcx> { - fn from(base: PlaceBase) -> Self { - Self { base, projection: Vec::new() } +impl<'tcx> From> for PlaceBuilder<'tcx> { + fn from(p: Place<'tcx>) -> Self { + Self::Local { local: p.local, projection: p.projection.to_vec() } } } -impl<'tcx> From> for PlaceBuilder<'tcx> { - fn from(p: Place<'tcx>) -> Self { - Self { base: PlaceBase::Local(p.local), projection: p.projection.to_vec() } +fn project_ty<'tcx>( + tcx: TyCtxt<'tcx>, + ty: Ty<'tcx>, + elem: PlaceElem<'tcx>, +) -> (Ty<'tcx>, Option) { + match elem { + ProjectionElem::Deref => { + let updated_ty = ty + .builtin_deref(true) + .unwrap_or_else(|| bug!("deref projection of non-dereferenceable ty {:?}", ty)) + .ty; + + (updated_ty, None) + } + ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => { + (ty.builtin_index().unwrap(), None) + } + ProjectionElem::Subslice { from, to, from_end } => { + let ty = match ty.kind() { + ty::Slice(..) => ty, + ty::Array(inner, _) if !from_end => tcx.mk_array(*inner, (to - from) as u64), + ty::Array(inner, size) if from_end => { + let size = size.eval_usize(tcx, ty::ParamEnv::empty()); + let len = size - (from as u64) - (to as u64); + tcx.mk_array(*inner, len) + } + _ => bug!("cannot subslice non-array type: `{:?}`", ty), + }; + + (ty, None) + } + ProjectionElem::Downcast(_, variant_idx) => (ty, Some(variant_idx)), + ProjectionElem::Field(_, ty) => (ty, None), + ProjectionElem::OpaqueCast(..) => bug!("didn't expect OpaqueCast"), + } +} + +fn local_projections_to_ty<'a, 'tcx>( + cx: &'a Builder<'a, 'tcx>, + local: Local, + projection: &'a [PlaceElem<'tcx>], +) -> (Ty<'tcx>, Option) { + let local_ty = cx.local_decls.local_decls()[local].ty; + projection.iter().fold((local_ty, None), |ty_variant_idx, elem| { + let ty = ty_variant_idx.0; + project_ty(cx.tcx, ty, *elem) + }) +} + +// Converts an `UpvarProjectionElem` to `PlaceElem`, ICE'ing when being passed a +// field projection. +fn upvar_proj_to_place_elem_no_field_proj<'tcx>( + upvar_proj: UpvarProjectionElem<'tcx>, +) -> PlaceElem<'tcx> { + match upvar_proj { + ProjectionElem::Deref => ProjectionElem::Deref, + ProjectionElem::Index(i) => ProjectionElem::Index(i), + ProjectionElem::ConstantIndex { offset, min_length, from_end } => { + ProjectionElem::ConstantIndex { offset, min_length, from_end } + } + ProjectionElem::Subslice { from, to, from_end } => { + ProjectionElem::Subslice { from, to, from_end } + } + ProjectionElem::Downcast(ty, variant_idx) => ProjectionElem::Downcast(ty, variant_idx), + ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty), + ProjectionElem::Field(..) => bug!("should not be called with `ProjectionElem::Field`"), } } @@ -403,6 +627,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { self.expr_as_place(block, expr, Mutability::Not, None) } + #[instrument(skip(self, fake_borrow_temps), level = "debug")] fn expr_as_place( &mut self, mut block: BasicBlock, @@ -410,8 +635,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { mutability: Mutability, fake_borrow_temps: Option<&mut Vec>, ) -> BlockAnd> { - debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability); - let this = self; let expr_span = expr.span; let source_info = this.source_info(expr_span); @@ -425,12 +648,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let lhs = &this.thir[lhs]; let mut place_builder = unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,)); + debug!(?place_builder); if let ty::Adt(adt_def, _) = lhs.ty.kind() { if adt_def.is_enum() { place_builder = place_builder.downcast(*adt_def, variant_index); } } - block.and(place_builder.field(name, expr.ty)) + block.and(place_builder.field(this, name)) } ExprKind::Deref { arg } => { let place_builder = unpack!( @@ -572,7 +796,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } /// Lower a captured upvar. Note we might not know the actual capture index, - /// so we create a place starting from `PlaceBase::Upvar`, which will be resolved + /// so we create a place starting from `Upvar`, which will be resolved /// once all projections that allow us to identify a capture have been applied. fn lower_captured_upvar( &mut self, @@ -580,7 +804,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { closure_def_id: LocalDefId, var_hir_id: LocalVarId, ) -> BlockAnd> { - block.and(PlaceBuilder::from(PlaceBase::Upvar { var_hir_id, closure_def_id })) + block.and(PlaceBuilder::Upvar { + upvar: Upvar { var_hir_id, closure_def_id }, + projection: vec![], + }) } /// Lower an index expression @@ -671,8 +898,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { source_info: SourceInfo, ) { let tcx = self.tcx; - let place_ty = base_place.ty(&self.local_decls, tcx); + if let ty::Slice(_) = place_ty.ty.kind() { // We need to create fake borrows to ensure that the bounds // check that we just did stays valid. Since we can't assign to diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index c7b3eb44dc5fb..b420e82017148 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -4,9 +4,8 @@ use rustc_index::vec::Idx; use rustc_middle::ty::util::IntTypeExt; use rustc_target::abi::{Abi, Primitive}; -use crate::build::expr::as_place::PlaceBase; use crate::build::expr::category::{Category, RvalueFunc}; -use crate::build::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary}; +use crate::build::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary, PlaceBuilder}; use rustc_hir::lang_items::LangItem; use rustc_middle::middle::region; use rustc_middle::mir::AssertKind; @@ -651,15 +650,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let arg_place_builder = unpack!(block = this.as_place_builder(block, arg)); - let mutability = match arg_place_builder.base() { + let mutability = match arg_place_builder { // We are capturing a path that starts off a local variable in the parent. // The mutability of the current capture is same as the mutability // of the local declaration in the parent. - PlaceBase::Local(local) => this.local_decls[local].mutability, + PlaceBuilder::Local { local, .. } => this.local_decls[local].mutability, // Parent is a closure and we are capturing a path that is captured // by the parent itself. The mutability of the current capture // is same as that of the capture in the parent closure. - PlaceBase::Upvar { .. } => { + PlaceBuilder::Upvar { .. } => { let enclosing_upvars_resolved = arg_place_builder.to_place(this); match enclosing_upvars_resolved.as_ref() { diff --git a/compiler/rustc_mir_build/src/build/expr/into.rs b/compiler/rustc_mir_build/src/build/expr/into.rs index 38b1fa91d0a67..895051d7590b5 100644 --- a/compiler/rustc_mir_build/src/build/expr/into.rs +++ b/compiler/rustc_mir_build/src/build/expr/into.rs @@ -355,11 +355,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // base-supplied field, generate an operand that // reads it from the base. iter::zip(field_names, &**field_types) - .map(|(n, ty)| match fields_map.get(&n) { + .map(|(n, _ty)| match fields_map.get(&n) { Some(v) => v.clone(), None => { - let place = place_builder.clone_project(PlaceElem::Field(n, *ty)); - this.consume_by_copy_or_move(place.to_place(this)) + let place_builder = place_builder.clone(); + this.consume_by_copy_or_move( + place_builder.field(this, n).to_place(this), + ) } }) .collect() diff --git a/compiler/rustc_mir_build/src/build/matches/simplify.rs b/compiler/rustc_mir_build/src/build/matches/simplify.rs index f6b1955fdec4d..36aa7693e827f 100644 --- a/compiler/rustc_mir_build/src/build/matches/simplify.rs +++ b/compiler/rustc_mir_build/src/build/matches/simplify.rs @@ -272,9 +272,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { || !adt_def.is_variant_list_non_exhaustive()); if irrefutable { let place_builder = match_pair.place.downcast(adt_def, variant_index); - candidate - .match_pairs - .extend(self.field_match_pairs(place_builder, subpatterns)); + let field_match_pairs = + self.field_match_pairs(place_builder.clone(), subpatterns); + candidate.match_pairs.extend(field_match_pairs); Ok(()) } else { Err(match_pair) diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs index 46e14cc9ac3b1..6c10704c5db51 100644 --- a/compiler/rustc_mir_build/src/build/matches/test.rs +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -757,8 +757,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let downcast_place = match_pair.place.downcast(adt_def, variant_index); // `(x as Variant)` let consequent_match_pairs = subpatterns.iter().map(|subpattern| { // e.g., `(x as Variant).0` - let place = downcast_place - .clone_project(PlaceElem::Field(subpattern.field, subpattern.pattern.ty)); + let place = downcast_place.clone().field(self, subpattern.field); // e.g., `(x as Variant).0 @ P1` MatchPair::new(place, &subpattern.pattern, self) }); diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs index cbd494862a01f..d95dbfca78e84 100644 --- a/compiler/rustc_mir_build/src/build/matches/util.rs +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -1,4 +1,3 @@ -use crate::build::expr::as_place::PlaceBase; use crate::build::expr::as_place::PlaceBuilder; use crate::build::matches::MatchPair; use crate::build::Builder; @@ -17,8 +16,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { subpatterns .iter() .map(|fieldpat| { - let place = - place.clone_project(PlaceElem::Field(fieldpat.field, fieldpat.pattern.ty)); + let place = place.clone().field(self, fieldpat.field); + MatchPair::new(place, &fieldpat.pattern, self) }) .collect() @@ -107,9 +106,9 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> { // Only add the OpaqueCast projection if the given place is an opaque type and the // expected type from the pattern is not. - let may_need_cast = match place.base() { - PlaceBase::Local(local) => { - let ty = Place::ty_from(local, place.projection(), &cx.local_decls, cx.tcx).ty; + let may_need_cast = match place { + PlaceBuilder::Local { local, ref projection } => { + let ty = Place::ty_from(local, projection, &cx.local_decls, cx.tcx).ty; ty != pattern.ty && ty.has_opaque_types() } _ => true, diff --git a/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs b/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs index 7806e8f45d3ad..5cfbbb1ac01e7 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/abs_domain.rs @@ -18,7 +18,7 @@ use rustc_middle::ty::Ty; pub struct AbstractOperand; #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub struct AbstractType; -pub type AbstractElem = ProjectionElem; +pub type AbstractElem = ProjectionElem; pub trait Lift { type Abstract; diff --git a/compiler/rustc_mir_dataflow/src/value_analysis.rs b/compiler/rustc_mir_dataflow/src/value_analysis.rs index 7df0114226418..ab16b60f82df9 100644 --- a/compiler/rustc_mir_dataflow/src/value_analysis.rs +++ b/compiler/rustc_mir_dataflow/src/value_analysis.rs @@ -777,10 +777,10 @@ pub enum TrackElem { Field(Field), } -impl TryFrom> for TrackElem { +impl TryFrom> for TrackElem { type Error = (); - fn try_from(value: ProjectionElem) -> Result { + fn try_from(value: ProjectionElem) -> Result { match value { ProjectionElem::Field(field, _) => Ok(TrackElem::Field(field)), _ => Err(()), diff --git a/src/test/ui/mir/field-projection-invariant.rs b/src/test/ui/mir/field-projection-invariant.rs new file mode 100644 index 0000000000000..b5d6add043cb9 --- /dev/null +++ b/src/test/ui/mir/field-projection-invariant.rs @@ -0,0 +1,24 @@ +// build-pass +struct Inv<'a>(&'a mut &'a ()); +enum Foo { + Bar, + Var(T), +} +type Supertype = Foo fn(Inv<'a>, Inv<'a>)>; + +fn foo(x: Foo fn(Inv<'a>, Inv<'b>)>) { + match x { + Supertype::Bar => {} + Supertype::Var(x) => {} + } +} + +fn foo_nested(x: Foo fn(Inv<'a>, Inv<'b>)>>) { + match x { + Foo::Bar => {} + Foo::Var(Supertype::Bar) => {} + Foo::Var(Supertype::Var(x)) => {} + } +} + +fn main() {} diff --git a/src/test/ui/mir/field-ty-ascription-enums.rs b/src/test/ui/mir/field-ty-ascription-enums.rs new file mode 100644 index 0000000000000..179af61709063 --- /dev/null +++ b/src/test/ui/mir/field-ty-ascription-enums.rs @@ -0,0 +1,15 @@ +// build-pass + +enum Foo { + Var(T), +} // `T` is covariant. + +fn foo<'b>(x: Foo fn(&'a ())>) { + let Foo::Var(x): Foo = x; +} + +fn foo_nested<'b>(x: Foo fn(&'a ())>>) { + let Foo::Var(Foo::Var(x)): Foo> = x; +} + +fn main() {} diff --git a/src/test/ui/mir/field-ty-ascription.rs b/src/test/ui/mir/field-ty-ascription.rs new file mode 100644 index 0000000000000..178c7916bc59f --- /dev/null +++ b/src/test/ui/mir/field-ty-ascription.rs @@ -0,0 +1,37 @@ +// build-pass + +struct Foo(T); // `T` is covariant. + +struct Bar { + x: T, +} // `T` is covariant. + +fn bar<'b>(x: Bar fn(&'a ())>) { + let Bar { x }: Bar = x; +} + +fn bar_nested<'b>(x: Bar fn(&'a ())>>) { + let Bar { x: Bar { x } }: Bar> = x; +} + +fn bar_foo_nested<'b>(x: Bar fn(&'a ())>>) { + let Bar { x: Foo ( x ) }: Bar> = x; +} + +fn foo<'b>(x: Foo fn(&'a ())>) { + let Foo(y): Foo = x; +} + +fn foo_nested<'b>(x: Foo fn(&'a ())>>) { + let Foo(Foo(y)): Foo> = x; +} + +fn tuple<'b>(x: (u32, for<'a> fn(&'a ()))) { + let (_, y): (u32, fn(&'b ())) = x; +} + +fn tuple_nested<'b>(x: (u32, (u32, for<'a> fn(&'a ())))) { + let (_, (_, y)): (u32, (u32, fn(&'b ()))) = x; +} + +fn main() {} From b26fea54826de2a26c1d3a59acd15d2400eca749 Mon Sep 17 00:00:00 2001 From: b-naber Date: Tue, 20 Dec 2022 09:09:39 +0100 Subject: [PATCH 2/4] don't include additional opaquecast --- compiler/rustc_mir_build/src/build/matches/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs index d95dbfca78e84..cf39c8c68e60d 100644 --- a/compiler/rustc_mir_build/src/build/matches/util.rs +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -109,7 +109,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> { let may_need_cast = match place { PlaceBuilder::Local { local, ref projection } => { let ty = Place::ty_from(local, projection, &cx.local_decls, cx.tcx).ty; - ty != pattern.ty && ty.has_opaque_types() + ty.has_opaque_types() && !pattern.ty.has_opaque_types() } _ => true, }; From 15fa511a504d96884d0a3f24baa6c0101debe960 Mon Sep 17 00:00:00 2001 From: b-naber Date: Tue, 20 Dec 2022 15:06:17 +0100 Subject: [PATCH 3/4] add tests --- src/test/ui/mir/issue-105819.rs | 15 +++++++++++++ src/test/ui/mir/issue-105881.rs | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/test/ui/mir/issue-105819.rs create mode 100644 src/test/ui/mir/issue-105881.rs diff --git a/src/test/ui/mir/issue-105819.rs b/src/test/ui/mir/issue-105819.rs new file mode 100644 index 0000000000000..d23eaf917a702 --- /dev/null +++ b/src/test/ui/mir/issue-105819.rs @@ -0,0 +1,15 @@ +#![feature(type_alias_impl_trait)] +// check-pass + +fn main() {} + +fn upvar() { + #[derive(Copy, Clone)] + struct Foo((u32, u32)); + + type T = impl Copy; + let foo: T = Foo((1u32, 2u32)); + let x = move || { + let Foo((a, b)) = foo; + }; +} diff --git a/src/test/ui/mir/issue-105881.rs b/src/test/ui/mir/issue-105881.rs new file mode 100644 index 0000000000000..2acab6ee06d05 --- /dev/null +++ b/src/test/ui/mir/issue-105881.rs @@ -0,0 +1,38 @@ +// check-pass +// compile-flags: --edition 2018 + +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; + +async fn new_future() { + loop {} +} + +pub struct SelectAll { + _inner: Fut, +} + +fn select_all(_: I) -> SelectAll +where + I: IntoIterator, + I::Item: Future + Unpin, +{ + loop {} +} + +impl Future for SelectAll { + type Output = (); + + fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll { + loop {} + } +} + +async fn run_one_step() { + let mut select = select_all(vec![Box::pin(new_future())]); + let poll_fns = &mut |cx: &mut Context<'_>| Pin::new(&mut select).poll(cx); + for _poll_fn in [poll_fns] {} +} + +fn main() {} From 2bd63fc858736e117de06ea67417f52989258982 Mon Sep 17 00:00:00 2001 From: b-naber Date: Tue, 20 Dec 2022 15:06:32 +0100 Subject: [PATCH 4/4] cargo.lock --- Cargo.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d9a9240ccf2c..cee95c594cbd9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1990,9 +1990,9 @@ dependencies = [ [[package]] name = "io-lifetimes" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e394faa0efb47f9f227f1cd89978f854542b318a6f64fa695489c9c993056656" +checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c" dependencies = [ "libc", "windows-sys", @@ -2000,9 +2000,9 @@ dependencies = [ [[package]] name = "is-terminal" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aae5bc6e2eb41c9def29a3e0f1306382807764b9b53112030eff57435667352d" +checksum = "927609f78c2913a6f6ac3c27a4fe87f43e2a35367c0c4b0f8265e8f49a104330" dependencies = [ "hermit-abi 0.2.6", "io-lifetimes", @@ -2221,9 +2221,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f9f08d8963a6c613f4b1a78f4f4a4dbfadf8e6545b2d72861731e4858b8b47f" +checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" [[package]] name = "litemap"