Skip to content

Commit 42b43c4

Browse files
authored
Unrolled build for rust-lang#118516
Rollup merge of rust-lang#118516 - celinval:smir-variants, r=ouz-a Add ADT variant infomation to StableMIR and finish implementing TyKind::internal() Introduce a `VariantDef` type and a mechanism to retrieve the definition from an `AdtDef`. The `VariantDef` representation itself is just a combination of `AdtDef` and `VariantIdx`, which allow us to retrieve further information of a variant. I don't think we need to cache extra information for now, and we can translate on an on demand manner. I am leaving the fields public today due to rust-lang/project-stable-mir#56, but they shouldn't. For this PR, I've only added a method to retrieve the variant name, and its fields. I also added an implementation of `RustcInternal` that allow users to retrieve more information using Rust internal APIs. I have also finished the implementation of `RustcInternal` for `TyKind` which fixes rust-lang/project-stable-mir#46. ## Motivation Both of these changes are needed in order to properly interpret things like projections. For example, - The variant definition is used to find out which variant we are downcasting to. - Being able to create `Ty` from `TyKind` helps for example processing each stage of a projection, like the code in `place.ty()`.
2 parents f32d298 + 326fea0 commit 42b43c4

File tree

12 files changed

+545
-53
lines changed

12 files changed

+545
-53
lines changed

compiler/rustc_smir/src/rustc_internal/internal.rs

+199-14
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ use rustc_middle::ty::{self as rustc_ty, Ty as InternalTy};
99
use rustc_span::Symbol;
1010
use stable_mir::mir::alloc::AllocId;
1111
use stable_mir::mir::mono::{Instance, MonoItem, StaticDef};
12+
use stable_mir::mir::{Mutability, Safety};
1213
use stable_mir::ty::{
13-
AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const,
14-
ExistentialTraitRef, FloatTy, GenericArgKind, GenericArgs, IntTy, Region, RigidTy, Span,
15-
TraitRef, Ty, UintTy,
14+
Abi, AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const,
15+
DynKind, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FloatTy, FnSig,
16+
GenericArgKind, GenericArgs, IndexedVal, IntTy, Movability, Region, RigidTy, Span, TermKind,
17+
TraitRef, Ty, UintTy, VariantDef, VariantIdx,
1618
};
1719
use stable_mir::{CrateItem, DefId};
1820

@@ -84,17 +86,38 @@ impl<'tcx> RustcInternal<'tcx> for RigidTy {
8486
}
8587
RigidTy::Str => rustc_ty::TyKind::Str,
8688
RigidTy::Slice(ty) => rustc_ty::TyKind::Slice(ty.internal(tables)),
87-
RigidTy::RawPtr(..)
88-
| RigidTy::Ref(..)
89-
| RigidTy::Foreign(_)
90-
| RigidTy::FnDef(_, _)
91-
| RigidTy::FnPtr(_)
92-
| RigidTy::Closure(..)
93-
| RigidTy::Coroutine(..)
94-
| RigidTy::CoroutineWitness(..)
95-
| RigidTy::Dynamic(..)
96-
| RigidTy::Tuple(..) => {
97-
todo!()
89+
RigidTy::RawPtr(ty, mutability) => rustc_ty::TyKind::RawPtr(rustc_ty::TypeAndMut {
90+
ty: ty.internal(tables),
91+
mutbl: mutability.internal(tables),
92+
}),
93+
RigidTy::Ref(region, ty, mutability) => rustc_ty::TyKind::Ref(
94+
region.internal(tables),
95+
ty.internal(tables),
96+
mutability.internal(tables),
97+
),
98+
RigidTy::Foreign(def) => rustc_ty::TyKind::Foreign(def.0.internal(tables)),
99+
RigidTy::FnDef(def, args) => {
100+
rustc_ty::TyKind::FnDef(def.0.internal(tables), args.internal(tables))
101+
}
102+
RigidTy::FnPtr(sig) => rustc_ty::TyKind::FnPtr(sig.internal(tables)),
103+
RigidTy::Closure(def, args) => {
104+
rustc_ty::TyKind::Closure(def.0.internal(tables), args.internal(tables))
105+
}
106+
RigidTy::Coroutine(def, args, mov) => rustc_ty::TyKind::Coroutine(
107+
def.0.internal(tables),
108+
args.internal(tables),
109+
mov.internal(tables),
110+
),
111+
RigidTy::CoroutineWitness(def, args) => {
112+
rustc_ty::TyKind::CoroutineWitness(def.0.internal(tables), args.internal(tables))
113+
}
114+
RigidTy::Dynamic(predicate, region, dyn_kind) => rustc_ty::TyKind::Dynamic(
115+
tables.tcx.mk_poly_existential_predicates(&predicate.internal(tables)),
116+
region.internal(tables),
117+
dyn_kind.internal(tables),
118+
),
119+
RigidTy::Tuple(tys) => {
120+
rustc_ty::TyKind::Tuple(tables.tcx.mk_type_list(&tys.internal(tables)))
98121
}
99122
}
100123
}
@@ -141,6 +164,57 @@ impl<'tcx> RustcInternal<'tcx> for FloatTy {
141164
}
142165
}
143166

167+
impl<'tcx> RustcInternal<'tcx> for Mutability {
168+
type T = rustc_ty::Mutability;
169+
170+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
171+
match self {
172+
Mutability::Not => rustc_ty::Mutability::Not,
173+
Mutability::Mut => rustc_ty::Mutability::Mut,
174+
}
175+
}
176+
}
177+
178+
impl<'tcx> RustcInternal<'tcx> for Movability {
179+
type T = rustc_ty::Movability;
180+
181+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
182+
match self {
183+
Movability::Static => rustc_ty::Movability::Static,
184+
Movability::Movable => rustc_ty::Movability::Movable,
185+
}
186+
}
187+
}
188+
189+
impl<'tcx> RustcInternal<'tcx> for FnSig {
190+
type T = rustc_ty::FnSig<'tcx>;
191+
192+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
193+
rustc_ty::FnSig {
194+
inputs_and_output: tables.tcx.mk_type_list(&self.inputs_and_output.internal(tables)),
195+
c_variadic: self.c_variadic,
196+
unsafety: self.unsafety.internal(tables),
197+
abi: self.abi.internal(tables),
198+
}
199+
}
200+
}
201+
202+
impl<'tcx> RustcInternal<'tcx> for VariantIdx {
203+
type T = rustc_target::abi::VariantIdx;
204+
205+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
206+
rustc_target::abi::VariantIdx::from(self.to_index())
207+
}
208+
}
209+
210+
impl<'tcx> RustcInternal<'tcx> for VariantDef {
211+
type T = &'tcx rustc_ty::VariantDef;
212+
213+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
214+
self.adt_def.internal(tables).variant(self.idx.internal(tables))
215+
}
216+
}
217+
144218
fn ty_const<'tcx>(constant: &Const, tables: &mut Tables<'tcx>) -> rustc_ty::Const<'tcx> {
145219
match constant.internal(tables) {
146220
rustc_middle::mir::Const::Ty(c) => c,
@@ -230,6 +304,58 @@ impl<'tcx> RustcInternal<'tcx> for BoundVariableKind {
230304
}
231305
}
232306

307+
impl<'tcx> RustcInternal<'tcx> for DynKind {
308+
type T = rustc_ty::DynKind;
309+
310+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
311+
match self {
312+
DynKind::Dyn => rustc_ty::DynKind::Dyn,
313+
DynKind::DynStar => rustc_ty::DynKind::DynStar,
314+
}
315+
}
316+
}
317+
318+
impl<'tcx> RustcInternal<'tcx> for ExistentialPredicate {
319+
type T = rustc_ty::ExistentialPredicate<'tcx>;
320+
321+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
322+
match self {
323+
ExistentialPredicate::Trait(trait_ref) => {
324+
rustc_ty::ExistentialPredicate::Trait(trait_ref.internal(tables))
325+
}
326+
ExistentialPredicate::Projection(proj) => {
327+
rustc_ty::ExistentialPredicate::Projection(proj.internal(tables))
328+
}
329+
ExistentialPredicate::AutoTrait(trait_def) => {
330+
rustc_ty::ExistentialPredicate::AutoTrait(trait_def.0.internal(tables))
331+
}
332+
}
333+
}
334+
}
335+
336+
impl<'tcx> RustcInternal<'tcx> for ExistentialProjection {
337+
type T = rustc_ty::ExistentialProjection<'tcx>;
338+
339+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
340+
rustc_ty::ExistentialProjection {
341+
def_id: self.def_id.0.internal(tables),
342+
args: self.generic_args.internal(tables),
343+
term: self.term.internal(tables),
344+
}
345+
}
346+
}
347+
348+
impl<'tcx> RustcInternal<'tcx> for TermKind {
349+
type T = rustc_ty::Term<'tcx>;
350+
351+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
352+
match self {
353+
TermKind::Type(ty) => ty.internal(tables).into(),
354+
TermKind::Const(const_) => ty_const(const_, tables).into(),
355+
}
356+
}
357+
}
358+
233359
impl<'tcx> RustcInternal<'tcx> for ExistentialTraitRef {
234360
type T = rustc_ty::ExistentialTraitRef<'tcx>;
235361

@@ -279,6 +405,53 @@ impl<'tcx> RustcInternal<'tcx> for AdtDef {
279405
}
280406
}
281407

408+
impl<'tcx> RustcInternal<'tcx> for Abi {
409+
type T = rustc_target::spec::abi::Abi;
410+
411+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
412+
match *self {
413+
Abi::Rust => rustc_target::spec::abi::Abi::Rust,
414+
Abi::C { unwind } => rustc_target::spec::abi::Abi::C { unwind },
415+
Abi::Cdecl { unwind } => rustc_target::spec::abi::Abi::Cdecl { unwind },
416+
Abi::Stdcall { unwind } => rustc_target::spec::abi::Abi::Stdcall { unwind },
417+
Abi::Fastcall { unwind } => rustc_target::spec::abi::Abi::Fastcall { unwind },
418+
Abi::Vectorcall { unwind } => rustc_target::spec::abi::Abi::Vectorcall { unwind },
419+
Abi::Thiscall { unwind } => rustc_target::spec::abi::Abi::Thiscall { unwind },
420+
Abi::Aapcs { unwind } => rustc_target::spec::abi::Abi::Aapcs { unwind },
421+
Abi::Win64 { unwind } => rustc_target::spec::abi::Abi::Win64 { unwind },
422+
Abi::SysV64 { unwind } => rustc_target::spec::abi::Abi::SysV64 { unwind },
423+
Abi::PtxKernel => rustc_target::spec::abi::Abi::PtxKernel,
424+
Abi::Msp430Interrupt => rustc_target::spec::abi::Abi::Msp430Interrupt,
425+
Abi::X86Interrupt => rustc_target::spec::abi::Abi::X86Interrupt,
426+
Abi::AmdGpuKernel => rustc_target::spec::abi::Abi::AmdGpuKernel,
427+
Abi::EfiApi => rustc_target::spec::abi::Abi::EfiApi,
428+
Abi::AvrInterrupt => rustc_target::spec::abi::Abi::AvrInterrupt,
429+
Abi::AvrNonBlockingInterrupt => rustc_target::spec::abi::Abi::AvrNonBlockingInterrupt,
430+
Abi::CCmseNonSecureCall => rustc_target::spec::abi::Abi::CCmseNonSecureCall,
431+
Abi::Wasm => rustc_target::spec::abi::Abi::Wasm,
432+
Abi::System { unwind } => rustc_target::spec::abi::Abi::System { unwind },
433+
Abi::RustIntrinsic => rustc_target::spec::abi::Abi::RustIntrinsic,
434+
Abi::RustCall => rustc_target::spec::abi::Abi::RustCall,
435+
Abi::PlatformIntrinsic => rustc_target::spec::abi::Abi::PlatformIntrinsic,
436+
Abi::Unadjusted => rustc_target::spec::abi::Abi::Unadjusted,
437+
Abi::RustCold => rustc_target::spec::abi::Abi::RustCold,
438+
Abi::RiscvInterruptM => rustc_target::spec::abi::Abi::RiscvInterruptM,
439+
Abi::RiscvInterruptS => rustc_target::spec::abi::Abi::RiscvInterruptS,
440+
}
441+
}
442+
}
443+
444+
impl<'tcx> RustcInternal<'tcx> for Safety {
445+
type T = rustc_hir::Unsafety;
446+
447+
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
448+
match self {
449+
Safety::Unsafe => rustc_hir::Unsafety::Unsafe,
450+
Safety::Normal => rustc_hir::Unsafety::Normal,
451+
}
452+
}
453+
}
454+
282455
impl<'tcx> RustcInternal<'tcx> for Span {
283456
type T = rustc_span::Span;
284457

@@ -297,6 +470,7 @@ where
297470
(*self).internal(tables)
298471
}
299472
}
473+
300474
impl<'tcx, T> RustcInternal<'tcx> for Option<T>
301475
where
302476
T: RustcInternal<'tcx>,
@@ -307,3 +481,14 @@ where
307481
self.as_ref().map(|inner| inner.internal(tables))
308482
}
309483
}
484+
485+
impl<'tcx, T> RustcInternal<'tcx> for Vec<T>
486+
where
487+
T: RustcInternal<'tcx>,
488+
{
489+
type T = Vec<T::T>;
490+
491+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
492+
self.iter().map(|e| e.internal(tables)).collect()
493+
}
494+
}

compiler/rustc_smir/src/rustc_smir/context.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use stable_mir::mir::alloc::GlobalAlloc;
1212
use stable_mir::mir::mono::{InstanceDef, StaticDef};
1313
use stable_mir::mir::Body;
1414
use stable_mir::ty::{
15-
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FnDef, GenericArgs, LineInfo,
16-
PolyFnSig, RigidTy, Span, TyKind,
15+
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs,
16+
LineInfo, PolyFnSig, RigidTy, Span, TyKind, VariantDef,
1717
};
1818
use stable_mir::{self, Crate, CrateItem, DefId, Error, Filename, ItemKind, Symbol};
1919
use std::cell::RefCell;
@@ -209,6 +209,21 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
209209
sig.stable(&mut *tables)
210210
}
211211

212+
fn adt_variants_len(&self, def: AdtDef) -> usize {
213+
let mut tables = self.0.borrow_mut();
214+
def.internal(&mut *tables).variants().len()
215+
}
216+
217+
fn variant_name(&self, def: VariantDef) -> Symbol {
218+
let mut tables = self.0.borrow_mut();
219+
def.internal(&mut *tables).name.to_string()
220+
}
221+
222+
fn variant_fields(&self, def: VariantDef) -> Vec<FieldDef> {
223+
let mut tables = self.0.borrow_mut();
224+
def.internal(&mut *tables).fields.iter().map(|f| f.stable(&mut *tables)).collect()
225+
}
226+
212227
fn eval_target_usize(&self, cnst: &Const) -> Result<u64, Error> {
213228
let mut tables = self.0.borrow_mut();
214229
let mir_const = cnst.internal(&mut *tables);
@@ -240,6 +255,13 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
240255
tables.tcx.type_of(item.internal(&mut *tables)).instantiate_identity().stable(&mut *tables)
241256
}
242257

258+
fn def_ty_with_args(&self, item: stable_mir::DefId, args: &GenericArgs) -> stable_mir::ty::Ty {
259+
let mut tables = self.0.borrow_mut();
260+
let args = args.internal(&mut *tables);
261+
let def_ty = tables.tcx.type_of(item.internal(&mut *tables));
262+
def_ty.instantiate(tables.tcx, args).stable(&mut *tables)
263+
}
264+
243265
fn const_literal(&self, cnst: &stable_mir::ty::Const) -> String {
244266
internal(cnst).to_string()
245267
}

compiler/rustc_smir/src/rustc_smir/convert/mir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ impl<'tcx> Stable<'tcx> for mir::AggregateKind<'tcx> {
517517
mir::AggregateKind::Adt(def_id, var_idx, generic_arg, user_ty_index, field_idx) => {
518518
stable_mir::mir::AggregateKind::Adt(
519519
tables.adt_def(*def_id),
520-
var_idx.index(),
520+
var_idx.stable(tables),
521521
generic_arg.stable(tables),
522522
user_ty_index.map(|idx| idx.index()),
523523
field_idx.map(|idx| idx.index()),

compiler/rustc_smir/src/rustc_smir/convert/mod.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Conversion of internal Rust compiler items to stable ones.
22
33
use rustc_target::abi::FieldIdx;
4-
use stable_mir::mir::VariantIdx;
4+
use stable_mir::ty::{IndexedVal, VariantIdx};
55

66
use crate::rustc_smir::{Stable, Tables};
77

@@ -25,17 +25,10 @@ impl<'tcx> Stable<'tcx> for FieldIdx {
2525
}
2626
}
2727

28-
impl<'tcx> Stable<'tcx> for (rustc_target::abi::VariantIdx, FieldIdx) {
29-
type T = (usize, usize);
30-
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
31-
(self.0.as_usize(), self.1.as_usize())
32-
}
33-
}
34-
3528
impl<'tcx> Stable<'tcx> for rustc_target::abi::VariantIdx {
3629
type T = VariantIdx;
3730
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
38-
self.as_usize()
31+
VariantIdx::to_val(self.as_usize())
3932
}
4033
}
4134

@@ -67,6 +60,14 @@ impl<'tcx> Stable<'tcx> for rustc_hir::CoroutineKind {
6760
}
6861
}
6962

63+
impl<'tcx> Stable<'tcx> for rustc_span::Symbol {
64+
type T = stable_mir::Symbol;
65+
66+
fn stable(&self, _tables: &mut Tables<'tcx>) -> Self::T {
67+
self.to_string()
68+
}
69+
}
70+
7071
impl<'tcx> Stable<'tcx> for rustc_span::Span {
7172
type T = stable_mir::ty::Span;
7273

compiler/rustc_smir/src/rustc_smir/convert/ty.rs

+11
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ impl<'tcx> Stable<'tcx> for ty::AdtKind {
137137
}
138138
}
139139

140+
impl<'tcx> Stable<'tcx> for ty::FieldDef {
141+
type T = stable_mir::ty::FieldDef;
142+
143+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
144+
stable_mir::ty::FieldDef {
145+
def: tables.create_def_id(self.did),
146+
name: self.name.stable(tables),
147+
}
148+
}
149+
}
150+
140151
impl<'tcx> Stable<'tcx> for ty::GenericArgs<'tcx> {
141152
type T = stable_mir::ty::GenericArgs;
142153
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {

0 commit comments

Comments
 (0)