Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b66fe58

Browse files
committedOct 25, 2023
Auto merge of #117113 - celinval:smir-stable-ty, r=oli-obk
Remove fold code and add `Const::internal()` to StableMIR We are not planning to support user generated constant in the foreseeable future, so we are cleaning up the fold logic and user created type for now. Users should use `Instance::resolve` in order to trigger monomorphization. The Instance::resolve was however incomplete, since we weren't handling internalizing constants yet. Thus, I added that. I decided to keep the `Const` fields private in case we decide to translate them lazily.
2 parents eb03d40 + 17f6df9 commit b66fe58

File tree

10 files changed

+155
-421
lines changed

10 files changed

+155
-421
lines changed
 

‎compiler/rustc_smir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![cfg_attr(not(bootstrap), doc(rust_logo))]
1414
#![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
1515
#![cfg_attr(not(bootstrap), allow(internal_features))]
16+
#![allow(rustc::usage_of_ty_tykind)]
1617

1718
pub mod rustc_internal;
1819

‎compiler/rustc_smir/src/rustc_internal/internal.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! due to incomplete stable coverage.
55
66
// Prefer importing stable_mir over internal rustc constructs to make this file more readable.
7-
use crate::rustc_smir::{MaybeStable, Tables};
7+
use crate::rustc_smir::Tables;
88
use rustc_middle::ty::{self as rustc_ty, Ty as InternalTy};
99
use stable_mir::ty::{Const, GenericArgKind, GenericArgs, Region, Ty};
1010
use stable_mir::DefId;
@@ -31,7 +31,7 @@ impl<'tcx> RustcInternal<'tcx> for GenericArgKind {
3131
match self {
3232
GenericArgKind::Lifetime(reg) => reg.internal(tables).into(),
3333
GenericArgKind::Type(ty) => ty.internal(tables).into(),
34-
GenericArgKind::Const(cnst) => cnst.internal(tables).into(),
34+
GenericArgKind::Const(cnst) => ty_const(cnst, tables).into(),
3535
}
3636
}
3737
}
@@ -46,16 +46,22 @@ impl<'tcx> RustcInternal<'tcx> for Region {
4646
impl<'tcx> RustcInternal<'tcx> for Ty {
4747
type T = InternalTy<'tcx>;
4848
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
49-
match tables.types[self.0] {
50-
MaybeStable::Stable(_) => todo!(),
51-
MaybeStable::Rustc(ty) => ty,
49+
tables.types[*self]
50+
}
51+
}
52+
53+
fn ty_const<'tcx>(constant: &Const, tables: &mut Tables<'tcx>) -> rustc_ty::Const<'tcx> {
54+
match constant.internal(tables) {
55+
rustc_middle::mir::Const::Ty(c) => c,
56+
cnst => {
57+
panic!("Trying to covert constant `{constant:?}` to type constant, but found {cnst:?}")
5258
}
5359
}
5460
}
5561

5662
impl<'tcx> RustcInternal<'tcx> for Const {
57-
type T = rustc_ty::Const<'tcx>;
58-
fn internal(&self, _tables: &mut Tables<'tcx>) -> Self::T {
59-
todo!()
63+
type T = rustc_middle::mir::Const<'tcx>;
64+
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T {
65+
tables.constants[self.id]
6066
}
6167
}

‎compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
167167
def_ids: IndexMap::default(),
168168
alloc_ids: IndexMap::default(),
169169
spans: IndexMap::default(),
170-
types: vec![],
170+
types: IndexMap::default(),
171171
instances: IndexMap::default(),
172+
constants: IndexMap::default(),
172173
}));
173174
stable_mir::run(&tables, || init(&tables, f));
174175
}

‎compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 78 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use rustc_target::abi::FieldIdx;
2020
use stable_mir::mir::mono::InstanceDef;
2121
use stable_mir::mir::{Body, CopyNonOverlapping, Statement, UserTypeProjection, VariantIdx};
2222
use stable_mir::ty::{
23-
FloatTy, GenericParamDef, IntTy, LineInfo, Movability, RigidTy, Span, TyKind, UintTy,
23+
Const, ConstId, ConstantKind, FloatTy, GenericParamDef, IntTy, LineInfo, Movability, RigidTy,
24+
Span, TyKind, UintTy,
2425
};
2526
use stable_mir::{self, opaque, Context, Filename};
2627
use std::cell::RefCell;
@@ -147,14 +148,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
147148

148149
fn ty_kind(&self, ty: stable_mir::ty::Ty) -> TyKind {
149150
let mut tables = self.0.borrow_mut();
150-
tables.types[ty.0].clone().stable(&mut *tables)
151-
}
152-
153-
fn mk_ty(&self, kind: TyKind) -> stable_mir::ty::Ty {
154-
let mut tables = self.0.borrow_mut();
155-
let n = tables.types.len();
156-
tables.types.push(MaybeStable::Stable(kind));
157-
stable_mir::ty::Ty(n)
151+
tables.types[ty].kind().stable(&mut *tables)
158152
}
159153

160154
fn generics_of(&self, def_id: stable_mir::DefId) -> stable_mir::ty::Generics {
@@ -213,8 +207,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
213207
fn instance_ty(&self, def: InstanceDef) -> stable_mir::ty::Ty {
214208
let mut tables = self.0.borrow_mut();
215209
let instance = tables.instances[def];
216-
let ty = instance.ty(tables.tcx, ParamEnv::empty());
217-
tables.intern_ty(ty)
210+
instance.ty(tables.tcx, ParamEnv::empty()).stable(&mut *tables)
218211
}
219212

220213
fn instance_def_id(&self, def: InstanceDef) -> stable_mir::DefId {
@@ -252,52 +245,25 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
252245
}
253246
}
254247

255-
#[derive(Clone)]
256-
pub enum MaybeStable<S, R> {
257-
Stable(S),
258-
Rustc(R),
259-
}
260-
261-
impl<'tcx, S, R> MaybeStable<S, R> {
262-
fn stable(self, tables: &mut Tables<'tcx>) -> S
263-
where
264-
R: Stable<'tcx, T = S>,
265-
{
266-
match self {
267-
MaybeStable::Stable(s) => s,
268-
MaybeStable::Rustc(r) => r.stable(tables),
269-
}
270-
}
271-
}
272-
273-
impl<S, R: PartialEq> PartialEq<R> for MaybeStable<S, R> {
274-
fn eq(&self, other: &R) -> bool {
275-
match self {
276-
MaybeStable::Stable(_) => false,
277-
MaybeStable::Rustc(r) => r == other,
278-
}
279-
}
280-
}
281-
282248
pub(crate) struct TablesWrapper<'tcx>(pub(crate) RefCell<Tables<'tcx>>);
283249

284250
pub struct Tables<'tcx> {
285251
pub(crate) tcx: TyCtxt<'tcx>,
286252
pub(crate) def_ids: IndexMap<DefId, stable_mir::DefId>,
287253
pub(crate) alloc_ids: IndexMap<AllocId, stable_mir::AllocId>,
288254
pub(crate) spans: IndexMap<rustc_span::Span, Span>,
289-
pub(crate) types: Vec<MaybeStable<TyKind, Ty<'tcx>>>,
255+
pub(crate) types: IndexMap<Ty<'tcx>, stable_mir::ty::Ty>,
290256
pub(crate) instances: IndexMap<ty::Instance<'tcx>, InstanceDef>,
257+
pub(crate) constants: IndexMap<mir::Const<'tcx>, ConstId>,
291258
}
292259

293260
impl<'tcx> Tables<'tcx> {
294261
fn intern_ty(&mut self, ty: Ty<'tcx>) -> stable_mir::ty::Ty {
295-
if let Some(id) = self.types.iter().position(|t| *t == ty) {
296-
return stable_mir::ty::Ty(id);
297-
}
298-
let id = self.types.len();
299-
self.types.push(MaybeStable::Rustc(ty));
300-
stable_mir::ty::Ty(id)
262+
self.types.create_or_fetch(ty)
263+
}
264+
265+
fn intern_const(&mut self, constant: mir::Const<'tcx>) -> ConstId {
266+
self.constants.create_or_fetch(constant)
301267
}
302268
}
303269

@@ -338,7 +304,7 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> {
338304
.local_decls
339305
.iter()
340306
.map(|decl| stable_mir::mir::LocalDecl {
341-
ty: tables.intern_ty(decl.ty),
307+
ty: decl.ty.stable(tables),
342308
span: decl.source_info.span.stable(tables),
343309
})
344310
.collect(),
@@ -436,7 +402,7 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
436402
Cast(cast_kind, op, ty) => stable_mir::mir::Rvalue::Cast(
437403
cast_kind.stable(tables),
438404
op.stable(tables),
439-
tables.intern_ty(*ty),
405+
ty.stable(tables),
440406
),
441407
BinaryOp(bin_op, ops) => stable_mir::mir::Rvalue::BinaryOp(
442408
bin_op.stable(tables),
@@ -449,7 +415,7 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
449415
ops.1.stable(tables),
450416
),
451417
NullaryOp(null_op, ty) => {
452-
stable_mir::mir::Rvalue::NullaryOp(null_op.stable(tables), tables.intern_ty(*ty))
418+
stable_mir::mir::Rvalue::NullaryOp(null_op.stable(tables), ty.stable(tables))
453419
}
454420
UnaryOp(un_op, op) => {
455421
stable_mir::mir::Rvalue::UnaryOp(un_op.stable(tables), op.stable(tables))
@@ -460,7 +426,7 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
460426
stable_mir::mir::Rvalue::Aggregate(agg_kind.stable(tables), operands)
461427
}
462428
ShallowInitBox(op, ty) => {
463-
stable_mir::mir::Rvalue::ShallowInitBox(op.stable(tables), tables.intern_ty(*ty))
429+
stable_mir::mir::Rvalue::ShallowInitBox(op.stable(tables), ty.stable(tables))
464430
}
465431
CopyForDeref(place) => stable_mir::mir::Rvalue::CopyForDeref(place.stable(tables)),
466432
}
@@ -604,7 +570,7 @@ impl<'tcx> Stable<'tcx> for ty::TermKind<'tcx> {
604570
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
605571
use stable_mir::ty::TermKind;
606572
match self {
607-
ty::TermKind::Ty(ty) => TermKind::Type(tables.intern_ty(*ty)),
573+
ty::TermKind::Ty(ty) => TermKind::Type(ty.stable(tables)),
608574
ty::TermKind::Const(cnst) => {
609575
let cnst = cnst.stable(tables);
610576
TermKind::Const(cnst)
@@ -885,7 +851,7 @@ impl<'tcx> Stable<'tcx> for mir::AggregateKind<'tcx> {
885851
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
886852
match self {
887853
mir::AggregateKind::Array(ty) => {
888-
stable_mir::mir::AggregateKind::Array(tables.intern_ty(*ty))
854+
stable_mir::mir::AggregateKind::Array(ty.stable(tables))
889855
}
890856
mir::AggregateKind::Tuple => stable_mir::mir::AggregateKind::Tuple,
891857
mir::AggregateKind::Adt(def_id, var_idx, generic_arg, user_ty_index, field_idx) => {
@@ -1053,7 +1019,7 @@ impl<'tcx> Stable<'tcx> for ty::GenericArgKind<'tcx> {
10531019
use stable_mir::ty::GenericArgKind;
10541020
match self {
10551021
ty::GenericArgKind::Lifetime(region) => GenericArgKind::Lifetime(region.stable(tables)),
1056-
ty::GenericArgKind::Type(ty) => GenericArgKind::Type(tables.intern_ty(*ty)),
1022+
ty::GenericArgKind::Type(ty) => GenericArgKind::Type(ty.stable(tables)),
10571023
ty::GenericArgKind::Const(cnst) => GenericArgKind::Const(cnst.stable(tables)),
10581024
}
10591025
}
@@ -1099,11 +1065,7 @@ impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> {
10991065
use stable_mir::ty::{Abi, FnSig};
11001066

11011067
FnSig {
1102-
inputs_and_output: self
1103-
.inputs_and_output
1104-
.iter()
1105-
.map(|ty| tables.intern_ty(ty))
1106-
.collect(),
1068+
inputs_and_output: self.inputs_and_output.iter().map(|ty| ty.stable(tables)).collect(),
11071069
c_variadic: self.c_variadic,
11081070
unsafety: self.unsafety.stable(tables),
11091071
abi: match self.abi {
@@ -1241,9 +1203,16 @@ impl<'tcx> Stable<'tcx> for hir::Movability {
12411203
}
12421204

12431205
impl<'tcx> Stable<'tcx> for Ty<'tcx> {
1206+
type T = stable_mir::ty::Ty;
1207+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1208+
tables.intern_ty(*self)
1209+
}
1210+
}
1211+
1212+
impl<'tcx> Stable<'tcx> for ty::TyKind<'tcx> {
12441213
type T = stable_mir::ty::TyKind;
12451214
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1246-
match self.kind() {
1215+
match self {
12471216
ty::Bool => TyKind::RigidTy(RigidTy::Bool),
12481217
ty::Char => TyKind::RigidTy(RigidTy::Char),
12491218
ty::Int(int_ty) => TyKind::RigidTy(RigidTy::Int(int_ty.stable(tables))),
@@ -1256,15 +1225,15 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
12561225
ty::Foreign(def_id) => TyKind::RigidTy(RigidTy::Foreign(tables.foreign_def(*def_id))),
12571226
ty::Str => TyKind::RigidTy(RigidTy::Str),
12581227
ty::Array(ty, constant) => {
1259-
TyKind::RigidTy(RigidTy::Array(tables.intern_ty(*ty), constant.stable(tables)))
1228+
TyKind::RigidTy(RigidTy::Array(ty.stable(tables), constant.stable(tables)))
12601229
}
1261-
ty::Slice(ty) => TyKind::RigidTy(RigidTy::Slice(tables.intern_ty(*ty))),
1230+
ty::Slice(ty) => TyKind::RigidTy(RigidTy::Slice(ty.stable(tables))),
12621231
ty::RawPtr(ty::TypeAndMut { ty, mutbl }) => {
1263-
TyKind::RigidTy(RigidTy::RawPtr(tables.intern_ty(*ty), mutbl.stable(tables)))
1232+
TyKind::RigidTy(RigidTy::RawPtr(ty.stable(tables), mutbl.stable(tables)))
12641233
}
12651234
ty::Ref(region, ty, mutbl) => TyKind::RigidTy(RigidTy::Ref(
12661235
region.stable(tables),
1267-
tables.intern_ty(*ty),
1236+
ty.stable(tables),
12681237
mutbl.stable(tables),
12691238
)),
12701239
ty::FnDef(def_id, generic_args) => {
@@ -1291,9 +1260,9 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
12911260
movability.stable(tables),
12921261
)),
12931262
ty::Never => TyKind::RigidTy(RigidTy::Never),
1294-
ty::Tuple(fields) => TyKind::RigidTy(RigidTy::Tuple(
1295-
fields.iter().map(|ty| tables.intern_ty(ty)).collect(),
1296-
)),
1263+
ty::Tuple(fields) => {
1264+
TyKind::RigidTy(RigidTy::Tuple(fields.iter().map(|ty| ty.stable(tables)).collect()))
1265+
}
12971266
ty::Alias(alias_kind, alias_ty) => {
12981267
TyKind::Alias(alias_kind.stable(tables), alias_ty.stable(tables))
12991268
}
@@ -1312,32 +1281,32 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
13121281
type T = stable_mir::ty::Const;
13131282

13141283
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1315-
stable_mir::ty::Const {
1316-
literal: match self.kind() {
1317-
ty::Value(val) => {
1318-
let const_val = tables.tcx.valtree_to_const_val((self.ty(), val));
1319-
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
1320-
self.ty(),
1321-
const_val,
1322-
tables,
1323-
))
1324-
}
1325-
ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
1326-
ty::ErrorCt(_) => unreachable!(),
1327-
ty::InferCt(_) => unreachable!(),
1328-
ty::BoundCt(_, _) => unimplemented!(),
1329-
ty::PlaceholderCt(_) => unimplemented!(),
1330-
ty::Unevaluated(uv) => {
1331-
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
1332-
def: tables.const_def(uv.def),
1333-
args: uv.args.stable(tables),
1334-
promoted: None,
1335-
})
1336-
}
1337-
ty::ExprCt(_) => unimplemented!(),
1338-
},
1339-
ty: tables.intern_ty(self.ty()),
1340-
}
1284+
let kind = match self.kind() {
1285+
ty::Value(val) => {
1286+
let const_val = tables.tcx.valtree_to_const_val((self.ty(), val));
1287+
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
1288+
self.ty(),
1289+
const_val,
1290+
tables,
1291+
))
1292+
}
1293+
ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
1294+
ty::ErrorCt(_) => unreachable!(),
1295+
ty::InferCt(_) => unreachable!(),
1296+
ty::BoundCt(_, _) => unimplemented!(),
1297+
ty::PlaceholderCt(_) => unimplemented!(),
1298+
ty::Unevaluated(uv) => {
1299+
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
1300+
def: tables.const_def(uv.def),
1301+
args: uv.args.stable(tables),
1302+
promoted: None,
1303+
})
1304+
}
1305+
ty::ExprCt(_) => unimplemented!(),
1306+
};
1307+
let ty = self.ty().stable(tables);
1308+
let id = tables.intern_const(mir::Const::Ty(*self));
1309+
Const::new(kind, ty, id)
13411310
}
13421311
}
13431312

@@ -1422,22 +1391,23 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
14221391
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
14231392
match *self {
14241393
mir::Const::Ty(c) => c.stable(tables),
1425-
mir::Const::Unevaluated(unev_const, ty) => stable_mir::ty::Const {
1426-
literal: stable_mir::ty::ConstantKind::Unevaluated(
1427-
stable_mir::ty::UnevaluatedConst {
1394+
mir::Const::Unevaluated(unev_const, ty) => {
1395+
let kind =
1396+
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
14281397
def: tables.const_def(unev_const.def),
14291398
args: unev_const.args.stable(tables),
14301399
promoted: unev_const.promoted.map(|u| u.as_u32()),
1431-
},
1432-
),
1433-
ty: tables.intern_ty(ty),
1434-
},
1435-
mir::Const::Val(val, ty) => stable_mir::ty::Const {
1436-
literal: stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
1437-
ty, val, tables,
1438-
)),
1439-
ty: tables.intern_ty(ty),
1440-
},
1400+
});
1401+
let ty = ty.stable(tables);
1402+
let id = tables.intern_const(*self);
1403+
Const::new(kind, ty, id)
1404+
}
1405+
mir::Const::Val(val, ty) => {
1406+
let kind = ConstantKind::Allocated(alloc::new_allocation(ty, val, tables));
1407+
let ty = ty.stable(tables);
1408+
let id = tables.intern_const(*self);
1409+
Const::new(kind, ty, id)
1410+
}
14411411
}
14421412
}
14431413
}
@@ -1562,7 +1532,7 @@ impl<'tcx> Stable<'tcx> for ty::ClauseKind<'tcx> {
15621532
ClauseKind::TypeOutlives(type_outlives) => {
15631533
let ty::OutlivesPredicate::<_, _>(a, b) = type_outlives;
15641534
stable_mir::ty::ClauseKind::TypeOutlives(stable_mir::ty::OutlivesPredicate(
1565-
tables.intern_ty(a),
1535+
a.stable(tables),
15661536
b.stable(tables),
15671537
))
15681538
}
@@ -1571,7 +1541,7 @@ impl<'tcx> Stable<'tcx> for ty::ClauseKind<'tcx> {
15711541
}
15721542
ClauseKind::ConstArgHasType(const_, ty) => stable_mir::ty::ClauseKind::ConstArgHasType(
15731543
const_.stable(tables),
1574-
tables.intern_ty(ty),
1544+
ty.stable(tables),
15751545
),
15761546
ClauseKind::WellFormed(generic_arg) => {
15771547
stable_mir::ty::ClauseKind::WellFormed(generic_arg.unpack().stable(tables))
@@ -1601,7 +1571,7 @@ impl<'tcx> Stable<'tcx> for ty::SubtypePredicate<'tcx> {
16011571

16021572
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
16031573
let ty::SubtypePredicate { a, b, a_is_expected: _ } = self;
1604-
stable_mir::ty::SubtypePredicate { a: tables.intern_ty(*a), b: tables.intern_ty(*b) }
1574+
stable_mir::ty::SubtypePredicate { a: a.stable(tables), b: b.stable(tables) }
16051575
}
16061576
}
16071577

@@ -1610,7 +1580,7 @@ impl<'tcx> Stable<'tcx> for ty::CoercePredicate<'tcx> {
16101580

16111581
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
16121582
let ty::CoercePredicate { a, b } = self;
1613-
stable_mir::ty::CoercePredicate { a: tables.intern_ty(*a), b: tables.intern_ty(*b) }
1583+
stable_mir::ty::CoercePredicate { a: a.stable(tables), b: b.stable(tables) }
16141584
}
16151585
}
16161586

‎compiler/stable_mir/src/fold.rs

Lines changed: 0 additions & 245 deletions
This file was deleted.

‎compiler/stable_mir/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use self::ty::{
3232
extern crate scoped_tls;
3333

3434
pub mod error;
35-
pub mod fold;
3635
pub mod mir;
3736
pub mod ty;
3837
pub mod visitor;
@@ -215,9 +214,6 @@ pub trait Context {
215214
/// Obtain the representation of a type.
216215
fn ty_kind(&self, ty: Ty) -> TyKind;
217216

218-
/// Create a new `Ty` from scratch without information from rustc.
219-
fn mk_ty(&self, kind: TyKind) -> Ty;
220-
221217
/// Get the body of an Instance.
222218
/// FIXME: Monomorphize the body.
223219
fn instance_body(&self, instance: InstanceDef) -> Body;

‎compiler/stable_mir/src/mir/body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl Operand {
477477

478478
impl Constant {
479479
pub fn ty(&self) -> Ty {
480-
self.literal.ty
480+
self.literal.ty()
481481
}
482482
}
483483

‎compiler/stable_mir/src/ty.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{
66
use crate::{Filename, Opaque};
77
use std::fmt::{self, Debug, Formatter};
88

9-
#[derive(Copy, Clone)]
9+
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
1010
pub struct Ty(pub usize);
1111

1212
impl Debug for Ty {
@@ -21,18 +21,37 @@ impl Ty {
2121
}
2222
}
2323

24-
impl From<TyKind> for Ty {
25-
fn from(value: TyKind) -> Self {
26-
with(|context| context.mk_ty(value))
24+
/// Represents a constant in MIR or from the Type system.
25+
#[derive(Clone, Debug)]
26+
pub struct Const {
27+
/// The constant kind.
28+
kind: ConstantKind,
29+
/// The constant type.
30+
ty: Ty,
31+
/// Used for internal tracking of the internal constant.
32+
pub id: ConstId,
33+
}
34+
35+
impl Const {
36+
/// Build a constant. Note that this should only be used by the compiler.
37+
pub fn new(kind: ConstantKind, ty: Ty, id: ConstId) -> Const {
38+
Const { kind, ty, id }
2739
}
28-
}
2940

30-
#[derive(Debug, Clone)]
31-
pub struct Const {
32-
pub literal: ConstantKind,
33-
pub ty: Ty,
41+
/// Retrieve the constant kind.
42+
pub fn kind(&self) -> &ConstantKind {
43+
&self.kind
44+
}
45+
46+
/// Get the constant type.
47+
pub fn ty(&self) -> Ty {
48+
self.ty
49+
}
3450
}
3551

52+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53+
pub struct ConstId(pub usize);
54+
3655
type Ident = Opaque;
3756

3857
#[derive(Debug, Clone)]
@@ -108,15 +127,6 @@ pub struct LineInfo {
108127
pub end_col: usize,
109128
}
110129

111-
impl IndexedVal for Span {
112-
fn to_val(index: usize) -> Self {
113-
Span(index)
114-
}
115-
fn to_index(&self) -> usize {
116-
self.0
117-
}
118-
}
119-
120130
#[derive(Clone, Debug)]
121131
pub enum TyKind {
122132
RigidTy(RigidTy),
@@ -603,3 +613,20 @@ pub trait IndexedVal {
603613

604614
fn to_index(&self) -> usize;
605615
}
616+
617+
macro_rules! index_impl {
618+
($name:ident) => {
619+
impl IndexedVal for $name {
620+
fn to_val(index: usize) -> Self {
621+
$name(index)
622+
}
623+
fn to_index(&self) -> usize {
624+
self.0
625+
}
626+
}
627+
};
628+
}
629+
630+
index_impl!(ConstId);
631+
index_impl!(Ty);
632+
index_impl!(Span);

‎compiler/stable_mir/src/visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ impl Visitable for Const {
4747
visitor.visit_const(self)
4848
}
4949
fn super_visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
50-
match &self.literal {
50+
match &self.kind() {
5151
super::ty::ConstantKind::Allocated(alloc) => alloc.visit(visitor)?,
5252
super::ty::ConstantKind::Unevaluated(uv) => uv.visit(visitor)?,
5353
super::ty::ConstantKind::Param(_) => {}
5454
}
55-
self.ty.visit(visitor)
55+
self.ty().visit(visitor)
5656
}
5757
}
5858

‎tests/ui-fulldeps/stable-mir/crate-info.rs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ extern crate stable_mir;
2222
use rustc_hir::def::DefKind;
2323
use rustc_middle::ty::TyCtxt;
2424
use rustc_smir::rustc_internal;
25-
26-
use stable_mir::fold::Foldable;
25+
use stable_mir::mir::mono::Instance;
26+
use stable_mir::ty::{RigidTy, TyKind};
2727
use std::assert_matches::assert_matches;
2828
use std::io::Write;
2929
use std::ops::ControlFlow;
@@ -119,40 +119,18 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
119119
}
120120

121121
let monomorphic = get_item(&items, (DefKind::Fn, "monomorphic")).unwrap();
122-
for block in monomorphic.body().blocks {
122+
let instance = Instance::try_from(monomorphic.clone()).unwrap();
123+
for block in instance.body().blocks {
123124
match &block.terminator.kind {
124-
stable_mir::mir::TerminatorKind::Call { func, .. } => match func {
125-
stable_mir::mir::Operand::Constant(c) => match &c.literal.literal {
126-
stable_mir::ty::ConstantKind::Allocated(alloc) => {
127-
assert!(alloc.bytes.is_empty());
128-
match c.literal.ty.kind() {
129-
stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::FnDef(
130-
def,
131-
mut args,
132-
)) => {
133-
let func = def.body();
134-
match func.locals[1].ty
135-
.fold(&mut args)
136-
.continue_value()
137-
.unwrap()
138-
.kind()
139-
{
140-
stable_mir::ty::TyKind::RigidTy(
141-
stable_mir::ty::RigidTy::Uint(_),
142-
) => {}
143-
stable_mir::ty::TyKind::RigidTy(
144-
stable_mir::ty::RigidTy::Tuple(_),
145-
) => {}
146-
other => panic!("{other:?}"),
147-
}
148-
}
149-
other => panic!("{other:?}"),
150-
}
151-
}
125+
stable_mir::mir::TerminatorKind::Call { func, .. } => {
126+
let TyKind::RigidTy(ty) = func.ty(&body.locals).kind() else { unreachable!() };
127+
let RigidTy::FnDef(def, args) = ty else { unreachable!() };
128+
let next_func = Instance::resolve(def, &args).unwrap();
129+
match next_func.body().locals[1].ty.kind() {
130+
TyKind::RigidTy(RigidTy::Uint(_)) | TyKind::RigidTy(RigidTy::Tuple(_)) => {}
152131
other => panic!("{other:?}"),
153-
},
154-
other => panic!("{other:?}"),
155-
},
132+
}
133+
}
156134
stable_mir::mir::TerminatorKind::Return => {}
157135
other => panic!("{other:?}"),
158136
}

0 commit comments

Comments
 (0)
Please sign in to comment.