Auto merge of #117517 - klinvill:smir-projections, r=ouz-a
Add richer structure for Stable MIR Projections Resolves https://github.com/rust-lang/project-stable-mir/issues/49. Projections in Stable MIR are currently just strings. This PR replaces that representation with a richer structure, namely projections become vectors of `ProjectionElem`s, just as in MIR. The `ProjectionElem` enum is heavily based off of the MIR `ProjectionElem`. This PR is a draft since there are several outstanding issues to resolve, including: - How should `UserTypeProjection`s be represented in Stable MIR? In MIR, the projections are just a vector of `ProjectionElem<(),()>`, meaning `ProjectionElem`s that don't have Local or Type arguments (for `Index`, `Field`, etc. objects). Should `UserTypeProjection`s be represented this way in Stable MIR as well? Or is there a more user-friendly representation that wouldn't drag along all the `ProjectionElem` variants that presumably can't appear? - What is the expected behavior of a `Place`'s `ty` function? Should it resolve down the chain of projections so that something like `*_1.f` would return the type referenced by field `f`? - Tests should be added for `UserTypeProjection`
This commit is contained in:
commit
698fcc8219
4 changed files with 355 additions and 7 deletions
|
@ -398,22 +398,128 @@ pub enum Operand {
|
|||
pub struct Place {
|
||||
pub local: Local,
|
||||
/// projection out of a place (access a field, deref a pointer, etc)
|
||||
pub projection: String,
|
||||
pub projection: Vec<ProjectionElem>,
|
||||
}
|
||||
|
||||
// In MIR ProjectionElem is parameterized on the second Field argument and the Index argument. This
|
||||
// is so it can be used for both Places (for which the projection elements are of type
|
||||
// ProjectionElem<Local, Ty>) and user-provided type annotations (for which the projection elements
|
||||
// are of type ProjectionElem<(), ()>). In SMIR we don't need this generality, so we just use
|
||||
// ProjectionElem for Places.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum ProjectionElem {
|
||||
/// Dereference projections (e.g. `*_1`) project to the address referenced by the base place.
|
||||
Deref,
|
||||
|
||||
/// A field projection (e.g., `f` in `_1.f`) project to a field in the base place. The field is
|
||||
/// referenced by source-order index rather than the name of the field. The fields type is also
|
||||
/// given.
|
||||
Field(FieldIdx, Ty),
|
||||
|
||||
/// Index into a slice/array. The value of the index is computed at runtime using the `V`
|
||||
/// argument.
|
||||
///
|
||||
/// Note that this does not also dereference, and so it does not exactly correspond to slice
|
||||
/// indexing in Rust. In other words, in the below Rust code:
|
||||
///
|
||||
/// ```rust
|
||||
/// let x = &[1, 2, 3, 4];
|
||||
/// let i = 2;
|
||||
/// x[i];
|
||||
/// ```
|
||||
///
|
||||
/// The `x[i]` is turned into a `Deref` followed by an `Index`, not just an `Index`. The same
|
||||
/// thing is true of the `ConstantIndex` and `Subslice` projections below.
|
||||
Index(Local),
|
||||
|
||||
/// Index into a slice/array given by offsets.
|
||||
///
|
||||
/// These indices are generated by slice patterns. Easiest to explain by example:
|
||||
///
|
||||
/// ```ignore (illustrative)
|
||||
/// [X, _, .._, _, _] => { offset: 0, min_length: 4, from_end: false },
|
||||
/// [_, X, .._, _, _] => { offset: 1, min_length: 4, from_end: false },
|
||||
/// [_, _, .._, X, _] => { offset: 2, min_length: 4, from_end: true },
|
||||
/// [_, _, .._, _, X] => { offset: 1, min_length: 4, from_end: true },
|
||||
/// ```
|
||||
ConstantIndex {
|
||||
/// index or -index (in Python terms), depending on from_end
|
||||
offset: u64,
|
||||
/// The thing being indexed must be at least this long. For arrays this
|
||||
/// is always the exact length.
|
||||
min_length: u64,
|
||||
/// Counting backwards from end? This is always false when indexing an
|
||||
/// array.
|
||||
from_end: bool,
|
||||
},
|
||||
|
||||
/// Projects a slice from the base place.
|
||||
///
|
||||
/// These indices are generated by slice patterns. If `from_end` is true, this represents
|
||||
/// `slice[from..slice.len() - to]`. Otherwise it represents `array[from..to]`.
|
||||
Subslice {
|
||||
from: u64,
|
||||
to: u64,
|
||||
/// Whether `to` counts from the start or end of the array/slice.
|
||||
from_end: bool,
|
||||
},
|
||||
|
||||
/// "Downcast" to a variant of an enum or a coroutine.
|
||||
Downcast(VariantIdx),
|
||||
|
||||
/// Like an explicit cast from an opaque type to a concrete type, but without
|
||||
/// requiring an intermediate variable.
|
||||
OpaqueCast(Ty),
|
||||
|
||||
/// A `Subtype(T)` projection is applied to any `StatementKind::Assign` where
|
||||
/// type of lvalue doesn't match the type of rvalue, the primary goal is making subtyping
|
||||
/// explicit during optimizations and codegen.
|
||||
///
|
||||
/// This projection doesn't impact the runtime behavior of the program except for potentially changing
|
||||
/// some type metadata of the interpreter or codegen backend.
|
||||
Subtype(Ty),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct UserTypeProjection {
|
||||
pub base: UserTypeAnnotationIndex,
|
||||
pub projection: String,
|
||||
|
||||
pub projection: Opaque,
|
||||
}
|
||||
|
||||
pub type Local = usize;
|
||||
|
||||
pub const RETURN_LOCAL: Local = 0;
|
||||
|
||||
/// The source-order index of a field in a variant.
|
||||
///
|
||||
/// For example, in the following types,
|
||||
/// ```ignore(illustrative)
|
||||
/// enum Demo1 {
|
||||
/// Variant0 { a: bool, b: i32 },
|
||||
/// Variant1 { c: u8, d: u64 },
|
||||
/// }
|
||||
/// struct Demo2 { e: u8, f: u16, g: u8 }
|
||||
/// ```
|
||||
/// `a`'s `FieldIdx` is `0`,
|
||||
/// `b`'s `FieldIdx` is `1`,
|
||||
/// `c`'s `FieldIdx` is `0`, and
|
||||
/// `g`'s `FieldIdx` is `2`.
|
||||
type FieldIdx = usize;
|
||||
|
||||
/// The source-order index of a variant in a type.
|
||||
///
|
||||
/// For example, in the following types,
|
||||
/// ```ignore(illustrative)
|
||||
/// enum Demo1 {
|
||||
/// Variant0 { a: bool, b: i32 },
|
||||
/// Variant1 { c: u8, d: u64 },
|
||||
/// }
|
||||
/// struct Demo2 { e: u8, f: u16, g: u8 }
|
||||
/// ```
|
||||
/// `a` is in the variant with the `VariantIdx` of `0`,
|
||||
/// `c` is in the variant with the `VariantIdx` of `1`, and
|
||||
/// `g` is in the variant with the `VariantIdx` of `0`.
|
||||
pub type VariantIdx = usize;
|
||||
|
||||
type UserTypeAnnotationIndex = usize;
|
||||
|
@ -536,6 +642,10 @@ impl Constant {
|
|||
}
|
||||
|
||||
impl Place {
|
||||
// FIXME(klinvill): This function is expected to resolve down the chain of projections to get
|
||||
// the type referenced at the end of it. E.g. calling `ty()` on `*(_1.f)` should end up
|
||||
// returning the type referenced by `f`. The information needed to do this may not currently be
|
||||
// present in Stable MIR since at least an implementation for AdtDef is probably needed.
|
||||
pub fn ty(&self, locals: &[LocalDecl]) -> Ty {
|
||||
let _start_ty = locals[self.local].ty;
|
||||
todo!("Implement projection")
|
||||
|
|
|
@ -76,6 +76,15 @@ pub trait MirVisitor {
|
|||
self.super_place(place, ptx, location)
|
||||
}
|
||||
|
||||
fn visit_projection_elem(
|
||||
&mut self,
|
||||
elem: &ProjectionElem,
|
||||
ptx: PlaceContext,
|
||||
location: Location,
|
||||
) {
|
||||
self.super_projection_elem(elem, ptx, location);
|
||||
}
|
||||
|
||||
fn visit_local(&mut self, local: &Local, ptx: PlaceContext, location: Location) {
|
||||
let _ = (local, ptx, location);
|
||||
}
|
||||
|
@ -264,7 +273,29 @@ pub trait MirVisitor {
|
|||
fn super_place(&mut self, place: &Place, ptx: PlaceContext, location: Location) {
|
||||
let _ = location;
|
||||
let _ = ptx;
|
||||
visit_opaque(&Opaque(place.projection.clone()));
|
||||
self.visit_local(&place.local, ptx, location);
|
||||
|
||||
for elem in &place.projection {
|
||||
self.visit_projection_elem(elem, ptx, location);
|
||||
}
|
||||
}
|
||||
|
||||
fn super_projection_elem(
|
||||
&mut self,
|
||||
elem: &ProjectionElem,
|
||||
ptx: PlaceContext,
|
||||
location: Location,
|
||||
) {
|
||||
match elem {
|
||||
ProjectionElem::Deref => {}
|
||||
ProjectionElem::Field(_idx, ty) => self.visit_ty(ty, location),
|
||||
ProjectionElem::Index(local) => self.visit_local(local, ptx, location),
|
||||
ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ } => {}
|
||||
ProjectionElem::Subslice { from: _, to: _, from_end: _ } => {}
|
||||
ProjectionElem::Downcast(_idx) => {}
|
||||
ProjectionElem::OpaqueCast(ty) => self.visit_ty(ty, location),
|
||||
ProjectionElem::Subtype(ty) => self.visit_ty(ty, location),
|
||||
}
|
||||
}
|
||||
|
||||
fn super_rvalue(&mut self, rvalue: &Rvalue, location: Location) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue