Skip to content

Commit 03770f0

Browse files
committed
Auto merge of rust-lang#103880 - b-naber:field-ty-mir, r=lcnr
Use non-ascribed type as field's type in mir Fixes rust-lang#96514 r? `@lcnr`
2 parents 01ef4b2 + ff41359 commit 03770f0

File tree

14 files changed

+433
-99
lines changed

14 files changed

+433
-99
lines changed

compiler/rustc_middle/src/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ impl<'tcx> StatementKind<'tcx> {
14881488
///////////////////////////////////////////////////////////////////////////
14891489
// Places
14901490

1491-
impl<V, T> ProjectionElem<V, T> {
1491+
impl<V, T, U> ProjectionElem<V, T, U> {
14921492
/// Returns `true` if the target of this projection may refer to a different region of memory
14931493
/// than the base.
14941494
fn is_indirect(&self) -> bool {
@@ -1517,7 +1517,7 @@ impl<V, T> ProjectionElem<V, T> {
15171517

15181518
/// Alias for projections as they appear in `UserTypeProjection`, where we
15191519
/// need neither the `V` parameter for `Index` nor the `T` for `Field`.
1520-
pub type ProjectionKind = ProjectionElem<(), ()>;
1520+
pub type ProjectionKind = ProjectionElem<(), (), ()>;
15211521

15221522
rustc_index::newtype_index! {
15231523
/// A [newtype'd][wrapper] index type in the MIR [control-flow graph][CFG]

compiler/rustc_middle/src/mir/syntax.rs

+35-4
Original file line numberDiff line numberDiff line change
@@ -890,11 +890,18 @@ pub struct Place<'tcx> {
890890
pub projection: &'tcx List<PlaceElem<'tcx>>,
891891
}
892892

893+
/// The different kinds of projections that can be used in the projection of a `Place`.
894+
///
895+
/// `T1` is the generic type for a field projection. For an actual projection on a `Place`
896+
/// this parameter will always be `Ty`, but the field type can be unavailable when
897+
/// building (by using `PlaceBuilder`) places that correspond to upvars.
898+
/// `T2` is the generic type for an `OpaqueCast` (is generic since it's abstracted over
899+
/// in dataflow analysis, see `AbstractElem`).
893900
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
894901
#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
895-
pub enum ProjectionElem<V, T> {
902+
pub enum ProjectionElem<V, T1, T2> {
896903
Deref,
897-
Field(Field, T),
904+
Field(Field, T1),
898905
/// Index into a slice/array.
899906
///
900907
/// Note that this does not also dereference, and so it does not exactly correspond to slice
@@ -950,12 +957,36 @@ pub enum ProjectionElem<V, T> {
950957

951958
/// Like an explicit cast from an opaque type to a concrete type, but without
952959
/// requiring an intermediate variable.
953-
OpaqueCast(T),
960+
OpaqueCast(T2),
954961
}
955962

956963
/// Alias for projections as they appear in places, where the base is a place
957964
/// and the index is a local.
958-
pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>>;
965+
pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>, Ty<'tcx>>;
966+
967+
/// Alias for projections that appear in `PlaceBuilder::Upvar`, for which
968+
/// we cannot provide any field types.
969+
pub type UpvarProjectionElem<'tcx> = ProjectionElem<Local, (), Ty<'tcx>>;
970+
971+
impl<'tcx> From<PlaceElem<'tcx>> for UpvarProjectionElem<'tcx> {
972+
fn from(elem: PlaceElem<'tcx>) -> Self {
973+
match elem {
974+
ProjectionElem::Deref => ProjectionElem::Deref,
975+
ProjectionElem::Field(field, _) => ProjectionElem::Field(field, ()),
976+
ProjectionElem::Index(v) => ProjectionElem::Index(v),
977+
ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
978+
ProjectionElem::ConstantIndex { offset, min_length, from_end }
979+
}
980+
ProjectionElem::Subslice { from, to, from_end } => {
981+
ProjectionElem::Subslice { from, to, from_end }
982+
}
983+
ProjectionElem::Downcast(opt_sym, variant_idx) => {
984+
ProjectionElem::Downcast(opt_sym, variant_idx)
985+
}
986+
ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty),
987+
}
988+
}
989+
}
959990

960991
///////////////////////////////////////////////////////////////////////////
961992
// Operands

compiler/rustc_middle/src/mir/tcx.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ impl<'tcx> PlaceTy<'tcx> {
2828
/// `place_ty.field_ty(tcx, f)` computes the type at a given field
2929
/// of a record or enum-variant. (Most clients of `PlaceTy` can
3030
/// instead just extract the relevant type directly from their
31-
/// `PlaceElem`, but some instances of `ProjectionElem<V, T>` do
32-
/// not carry a `Ty` for `T`.)
31+
/// `PlaceElem`, but some instances of `ProjectionElem<V, T1, T2>` do
32+
/// not carry a `Ty` for `T1` or `T2`.)
3333
///
3434
/// Note that the resulting type has not been normalized.
3535
pub fn field_ty(self, tcx: TyCtxt<'tcx>, f: Field) -> Ty<'tcx> {
@@ -64,17 +64,18 @@ impl<'tcx> PlaceTy<'tcx> {
6464
/// `Ty` or downcast variant corresponding to that projection.
6565
/// The `handle_field` callback must map a `Field` to its `Ty`,
6666
/// (which should be trivial when `T` = `Ty`).
67-
pub fn projection_ty_core<V, T>(
67+
pub fn projection_ty_core<V, T1, T2>(
6868
self,
6969
tcx: TyCtxt<'tcx>,
7070
param_env: ty::ParamEnv<'tcx>,
71-
elem: &ProjectionElem<V, T>,
72-
mut handle_field: impl FnMut(&Self, Field, T) -> Ty<'tcx>,
73-
mut handle_opaque_cast: impl FnMut(&Self, T) -> Ty<'tcx>,
71+
elem: &ProjectionElem<V, T1, T2>,
72+
mut handle_field: impl FnMut(&Self, Field, T1) -> Ty<'tcx>,
73+
mut handle_opaque_cast: impl FnMut(&Self, T2) -> Ty<'tcx>,
7474
) -> PlaceTy<'tcx>
7575
where
7676
V: ::std::fmt::Debug,
77-
T: ::std::fmt::Debug + Copy,
77+
T1: ::std::fmt::Debug + Copy,
78+
T2: ::std::fmt::Debug + Copy,
7879
{
7980
if self.variant_index.is_some() && !matches!(elem, ProjectionElem::Field(..)) {
8081
bug!("cannot use non field projection on downcasted place")

0 commit comments

Comments
 (0)