Skip to content

Commit f7e2846

Browse files
committed
Split smir Const into TyConst and MirConst
1 parent ada1a6f commit f7e2846

File tree

12 files changed

+235
-133
lines changed

12 files changed

+235
-133
lines changed

compiler/rustc_smir/src/rustc_internal/internal.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
66
// Prefer importing stable_mir over internal rustc constructs to make this file more readable.
77
use crate::rustc_smir::Tables;
8-
use rustc_middle::ty::{self as rustc_ty, Ty as InternalTy, TyCtxt};
8+
use rustc_middle::ty::{self as rustc_ty, Const as InternalConst, Ty as InternalTy, TyCtxt};
99
use rustc_span::Symbol;
1010
use stable_mir::abi::Layout;
1111
use stable_mir::mir::alloc::AllocId;
1212
use stable_mir::mir::mono::{Instance, MonoItem, StaticDef};
1313
use stable_mir::mir::{BinOp, Mutability, Place, ProjectionElem, Safety, UnOp};
1414
use stable_mir::ty::{
15-
Abi, AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, Const,
16-
DynKind, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FloatTy, FnSig,
17-
GenericArgKind, GenericArgs, IndexedVal, IntTy, Movability, Pattern, Region, RigidTy, Span,
18-
TermKind, TraitRef, Ty, UintTy, VariantDef, VariantIdx,
15+
Abi, AdtDef, Binder, BoundRegionKind, BoundTyKind, BoundVariableKind, ClosureKind, DynKind,
16+
ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FloatTy, FnSig,
17+
GenericArgKind, GenericArgs, IndexedVal, IntTy, MirConst, Movability, Pattern, Region, RigidTy,
18+
Span, TermKind, TraitRef, Ty, TyConst, UintTy, VariantDef, VariantIdx,
1919
};
2020
use stable_mir::{CrateItem, CrateNum, DefId};
2121

@@ -55,7 +55,7 @@ impl RustcInternal for GenericArgKind {
5555
let arg: rustc_ty::GenericArg<'tcx> = match self {
5656
GenericArgKind::Lifetime(reg) => reg.internal(tables, tcx).into(),
5757
GenericArgKind::Type(ty) => ty.internal(tables, tcx).into(),
58-
GenericArgKind::Const(cnst) => ty_const(cnst, tables, tcx).into(),
58+
GenericArgKind::Const(cnst) => cnst.internal(tables, tcx).into(),
5959
};
6060
tcx.lift(arg).unwrap()
6161
}
@@ -76,13 +76,20 @@ impl RustcInternal for Ty {
7676
}
7777
}
7878

79+
impl RustcInternal for TyConst {
80+
type T<'tcx> = InternalConst<'tcx>;
81+
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
82+
tcx.lift(tables.ty_consts[self.id]).unwrap()
83+
}
84+
}
85+
7986
impl RustcInternal for Pattern {
8087
type T<'tcx> = rustc_ty::Pattern<'tcx>;
8188
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
8289
tcx.mk_pat(match self {
8390
Pattern::Range { start, end, include_end } => rustc_ty::PatternKind::Range {
84-
start: start.as_ref().map(|c| ty_const(c, tables, tcx)),
85-
end: end.as_ref().map(|c| ty_const(c, tables, tcx)),
91+
start: start.as_ref().map(|c| c.internal(tables, tcx)),
92+
end: end.as_ref().map(|c| c.internal(tables, tcx)),
8693
include_end: *include_end,
8794
},
8895
})
@@ -101,7 +108,7 @@ impl RustcInternal for RigidTy {
101108
RigidTy::Float(float_ty) => rustc_ty::TyKind::Float(float_ty.internal(tables, tcx)),
102109
RigidTy::Never => rustc_ty::TyKind::Never,
103110
RigidTy::Array(ty, cnst) => {
104-
rustc_ty::TyKind::Array(ty.internal(tables, tcx), ty_const(cnst, tables, tcx))
111+
rustc_ty::TyKind::Array(ty.internal(tables, tcx), cnst.internal(tables, tcx))
105112
}
106113
RigidTy::Pat(ty, pat) => {
107114
rustc_ty::TyKind::Pat(ty.internal(tables, tcx), pat.internal(tables, tcx))
@@ -239,23 +246,10 @@ impl RustcInternal for VariantDef {
239246
}
240247
}
241248

242-
fn ty_const<'tcx>(
243-
constant: &Const,
244-
tables: &mut Tables<'_>,
245-
tcx: TyCtxt<'tcx>,
246-
) -> rustc_ty::Const<'tcx> {
247-
match constant.internal(tables, tcx) {
248-
rustc_middle::mir::Const::Ty(_, c) => c,
249-
cnst => {
250-
panic!("Trying to convert constant `{constant:?}` to type constant, but found {cnst:?}")
251-
}
252-
}
253-
}
254-
255-
impl RustcInternal for Const {
249+
impl RustcInternal for MirConst {
256250
type T<'tcx> = rustc_middle::mir::Const<'tcx>;
257251
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
258-
let constant = tables.constants[self.id];
252+
let constant = tables.mir_consts[self.id];
259253
match constant {
260254
rustc_middle::mir::Const::Ty(ty, ct) => {
261255
rustc_middle::mir::Const::Ty(tcx.lift(ty).unwrap(), tcx.lift(ct).unwrap())
@@ -394,7 +388,7 @@ impl RustcInternal for TermKind {
394388
fn internal<'tcx>(&self, tables: &mut Tables<'_>, tcx: TyCtxt<'tcx>) -> Self::T<'tcx> {
395389
match self {
396390
TermKind::Type(ty) => ty.internal(tables, tcx).into(),
397-
TermKind::Const(const_) => ty_const(const_, tables, tcx).into(),
391+
TermKind::Const(cnst) => cnst.internal(tables, tcx).into(),
398392
}
399393
}
400394
}

compiler/rustc_smir/src/rustc_internal/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ where
214214
spans: IndexMap::default(),
215215
types: IndexMap::default(),
216216
instances: IndexMap::default(),
217-
constants: IndexMap::default(),
217+
ty_consts: IndexMap::default(),
218+
mir_consts: IndexMap::default(),
218219
layouts: IndexMap::default(),
219220
}));
220221
stable_mir::compiler_interface::run(&tables, || init(&tables, f))

compiler/rustc_smir/src/rustc_smir/context.rs

+48-13
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
#![allow(rustc::usage_of_qualified_ty)]
77

88
use rustc_abi::HasDataLayout;
9-
use rustc_middle::ty;
109
use rustc_middle::ty::layout::{
1110
FnAbiOf, FnAbiOfHelpers, HasParamEnv, HasTyCtxt, LayoutOf, LayoutOfHelpers,
1211
};
1312
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
1413
use rustc_middle::ty::{
1514
GenericPredicates, Instance, List, ParamEnv, ScalarInt, TyCtxt, TypeVisitableExt, ValTree,
1615
};
16+
use rustc_middle::{mir, ty};
1717
use rustc_span::def_id::LOCAL_CRATE;
1818
use stable_mir::abi::{FnAbi, Layout, LayoutShape};
1919
use stable_mir::compiler_interface::Context;
@@ -22,9 +22,9 @@ use stable_mir::mir::mono::{InstanceDef, StaticDef};
2222
use stable_mir::mir::{BinOp, Body, Place, UnOp};
2323
use stable_mir::target::{MachineInfo, MachineSize};
2424
use stable_mir::ty::{
25-
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, ForeignDef,
26-
ForeignItemKind, GenericArgs, IntrinsicDef, LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind,
27-
UintTy, VariantDef,
25+
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
26+
ForeignItemKind, GenericArgs, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, Ty,
27+
TyConst, TyKind, UintTy, VariantDef,
2828
};
2929
use stable_mir::{Crate, CrateDef, CrateItem, CrateNum, DefId, Error, Filename, ItemKind, Symbol};
3030
use std::cell::RefCell;
@@ -360,7 +360,15 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
360360
def.internal(&mut *tables, tcx).fields.iter().map(|f| f.stable(&mut *tables)).collect()
361361
}
362362

363-
fn eval_target_usize(&self, cnst: &Const) -> Result<u64, Error> {
363+
fn eval_target_usize(&self, cnst: &MirConst) -> Result<u64, Error> {
364+
let mut tables = self.0.borrow_mut();
365+
let tcx = tables.tcx;
366+
let mir_const = cnst.internal(&mut *tables, tcx);
367+
mir_const
368+
.try_eval_target_usize(tables.tcx, ParamEnv::empty())
369+
.ok_or_else(|| Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
370+
}
371+
fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error> {
364372
let mut tables = self.0.borrow_mut();
365373
let tcx = tables.tcx;
366374
let mir_const = cnst.internal(&mut *tables, tcx);
@@ -369,7 +377,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
369377
.ok_or_else(|| Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
370378
}
371379

372-
fn try_new_const_zst(&self, ty: Ty) -> Result<Const, Error> {
380+
fn try_new_const_zst(&self, ty: Ty) -> Result<MirConst, Error> {
373381
let mut tables = self.0.borrow_mut();
374382
let tcx = tables.tcx;
375383
let ty_internal = ty.internal(&mut *tables, tcx);
@@ -390,25 +398,47 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
390398
)));
391399
}
392400

393-
Ok(ty::Const::zero_sized(tables.tcx, ty_internal).stable(&mut *tables))
401+
Ok(mir::Const::Ty(ty_internal, ty::Const::zero_sized(tables.tcx, ty_internal))
402+
.stable(&mut *tables))
394403
}
395404

396-
fn new_const_str(&self, value: &str) -> Const {
405+
fn new_const_str(&self, value: &str) -> MirConst {
397406
let mut tables = self.0.borrow_mut();
398407
let tcx = tables.tcx;
399408
let ty = ty::Ty::new_static_str(tcx);
400409
let bytes = value.as_bytes();
401410
let val_tree = ty::ValTree::from_raw_bytes(tcx, bytes);
402411

403-
ty::Const::new_value(tcx, val_tree, ty).stable(&mut *tables)
412+
mir::Const::Ty(ty, ty::Const::new_value(tcx, val_tree, ty)).stable(&mut *tables)
404413
}
405414

406-
fn new_const_bool(&self, value: bool) -> Const {
415+
fn new_const_bool(&self, value: bool) -> MirConst {
407416
let mut tables = self.0.borrow_mut();
408-
ty::Const::from_bool(tables.tcx, value).stable(&mut *tables)
417+
mir::Const::Ty(tables.tcx.types.bool, ty::Const::from_bool(tables.tcx, value))
418+
.stable(&mut *tables)
409419
}
410420

411-
fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<Const, Error> {
421+
fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error> {
422+
let mut tables = self.0.borrow_mut();
423+
let tcx = tables.tcx;
424+
let ty = ty::Ty::new_uint(tcx, uint_ty.internal(&mut *tables, tcx));
425+
let size = tables.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap().size;
426+
427+
// We don't use Const::from_bits since it doesn't have any error checking.
428+
let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {
429+
Error::new(format!("Value overflow: cannot convert `{value}` to `{ty}`."))
430+
})?;
431+
Ok(mir::Const::Ty(
432+
ty,
433+
ty::Const::new_value(tables.tcx, ValTree::from_scalar_int(scalar), ty),
434+
)
435+
.stable(&mut *tables))
436+
}
437+
fn try_new_ty_const_uint(
438+
&self,
439+
value: u128,
440+
uint_ty: UintTy,
441+
) -> Result<stable_mir::ty::TyConst, Error> {
412442
let mut tables = self.0.borrow_mut();
413443
let tcx = tables.tcx;
414444
let ty = ty::Ty::new_uint(tcx, uint_ty.internal(&mut *tables, tcx));
@@ -453,7 +483,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
453483
.stable(&mut *tables)
454484
}
455485

456-
fn const_pretty(&self, cnst: &stable_mir::ty::Const) -> String {
486+
fn mir_const_pretty(&self, cnst: &stable_mir::ty::MirConst) -> String {
457487
let mut tables = self.0.borrow_mut();
458488
let tcx = tables.tcx;
459489
cnst.internal(&mut *tables, tcx).to_string()
@@ -474,6 +504,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
474504
tables.types[ty].kind().stable(&mut *tables)
475505
}
476506

507+
fn ty_const_pretty(&self, ct: stable_mir::ty::TyConstId) -> String {
508+
let tables = self.0.borrow_mut();
509+
tables.ty_consts[ct].to_string()
510+
}
511+
477512
fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> stable_mir::ty::Ty {
478513
let mut tables = self.0.borrow_mut();
479514
let tcx = tables.tcx;

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_middle::mir::interpret::alloc_range;
66
use rustc_middle::mir::mono::MonoItem;
77
use stable_mir::mir::alloc::GlobalAlloc;
88
use stable_mir::mir::{ConstOperand, Statement, UserTypeProjection, VarDebugInfoFragment};
9-
use stable_mir::ty::{Allocation, Const, ConstantKind};
9+
use stable_mir::ty::{Allocation, ConstantKind, MirConst};
1010
use stable_mir::{opaque, Error};
1111

1212
use crate::rustc_smir::{alloc, Stable, Tables};
@@ -724,11 +724,16 @@ impl<'tcx> Stable<'tcx> for mir::interpret::GlobalAlloc<'tcx> {
724724
}
725725

726726
impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
727-
type T = stable_mir::ty::Const;
727+
type T = stable_mir::ty::MirConst;
728728

729729
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
730+
let id = tables.intern_mir_const(tables.tcx.lift(*self).unwrap());
730731
match *self {
731-
mir::Const::Ty(_, c) => c.stable(tables),
732+
mir::Const::Ty(ty, c) => MirConst::new(
733+
stable_mir::ty::ConstantKind::Ty(c.stable(tables)),
734+
ty.stable(tables),
735+
id,
736+
),
732737
mir::Const::Unevaluated(unev_const, ty) => {
733738
let kind =
734739
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
@@ -737,21 +742,18 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
737742
promoted: unev_const.promoted.map(|u| u.as_u32()),
738743
});
739744
let ty = ty.stable(tables);
740-
let id = tables.intern_const(tables.tcx.lift(*self).unwrap());
741-
Const::new(kind, ty, id)
745+
MirConst::new(kind, ty, id)
742746
}
743747
mir::Const::Val(mir::ConstValue::ZeroSized, ty) => {
744748
let ty = ty.stable(tables);
745-
let id = tables.intern_const(tables.tcx.lift(*self).unwrap());
746-
Const::new(ConstantKind::ZeroSized, ty, id)
749+
MirConst::new(ConstantKind::ZeroSized, ty, id)
747750
}
748751
mir::Const::Val(val, ty) => {
749752
let ty = tables.tcx.lift(ty).unwrap();
750753
let val = tables.tcx.lift(val).unwrap();
751754
let kind = ConstantKind::Allocated(alloc::new_allocation(ty, val, tables));
752755
let ty = ty.stable(tables);
753-
let id = tables.intern_const(tables.tcx.lift(*self).unwrap());
754-
Const::new(kind, ty, id)
756+
MirConst::new(kind, ty, id)
755757
}
756758
}
757759
}

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

+14-20
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
use rustc_middle::ty::Ty;
44
use rustc_middle::{mir, ty};
55
use stable_mir::ty::{
6-
AdtKind, Const, ConstantKind, FloatTy, GenericArgs, GenericParamDef, IntTy, Region, RigidTy,
7-
TyKind, UintTy,
6+
AdtKind, FloatTy, GenericArgs, GenericParamDef, IntTy, Region, RigidTy, TyKind, UintTy,
87
};
98

109
use crate::rustc_smir::{alloc, Stable, Tables};
@@ -411,7 +410,7 @@ impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
411410
}
412411

413412
impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
414-
type T = stable_mir::ty::Const;
413+
type T = stable_mir::ty::TyConst;
415414

416415
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
417416
let kind = match self.kind() {
@@ -426,32 +425,27 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
426425
let ty = tables.tcx.lift(ty).unwrap();
427426
let const_val = tables.tcx.valtree_to_const_val((ty, val));
428427
if matches!(const_val, mir::ConstValue::ZeroSized) {
429-
ConstantKind::ZeroSized
428+
stable_mir::ty::TyConstKind::ZSTValue(ty.stable(tables))
430429
} else {
431-
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
432-
ty, const_val, tables,
433-
))
430+
stable_mir::ty::TyConstKind::Value(
431+
ty.stable(tables),
432+
alloc::new_allocation(ty, const_val, tables),
433+
)
434434
}
435435
}
436-
ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
436+
ty::ParamCt(param) => stable_mir::ty::TyConstKind::Param(param.stable(tables)),
437+
ty::Unevaluated(uv) => stable_mir::ty::TyConstKind::Unevaluated(
438+
tables.const_def(uv.def),
439+
uv.args.stable(tables),
440+
),
437441
ty::ErrorCt(_) => unreachable!(),
438442
ty::InferCt(_) => unreachable!(),
439443
ty::BoundCt(_, _) => unimplemented!(),
440444
ty::PlaceholderCt(_) => unimplemented!(),
441-
ty::Unevaluated(uv) => {
442-
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
443-
def: tables.const_def(uv.def),
444-
args: uv.args.stable(tables),
445-
promoted: None,
446-
})
447-
}
448445
ty::ExprCt(_) => unimplemented!(),
449446
};
450-
// THISPR
451-
// let ty = self.ty().stable(tables);
452-
// let id = tables.intern_const(mir::Const::Ty(tables.tcx.lift(*self).unwrap()));
453-
// Const::new(kind, ty, id)
454-
todo!()
447+
let id = tables.intern_ty_const(tables.tcx.lift(*self).unwrap());
448+
stable_mir::ty::TyConst::new(kind, id)
455449
}
456450
}
457451

compiler/rustc_smir/src/rustc_smir/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
1414
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1515
use stable_mir::abi::Layout;
1616
use stable_mir::mir::mono::InstanceDef;
17-
use stable_mir::ty::{ConstId, Span};
17+
use stable_mir::ty::{MirConstId, Span, TyConstId};
1818
use stable_mir::{CtorKind, ItemKind};
1919
use std::ops::RangeInclusive;
2020
use tracing::debug;
@@ -33,7 +33,8 @@ pub struct Tables<'tcx> {
3333
pub(crate) spans: IndexMap<rustc_span::Span, Span>,
3434
pub(crate) types: IndexMap<Ty<'tcx>, stable_mir::ty::Ty>,
3535
pub(crate) instances: IndexMap<ty::Instance<'tcx>, InstanceDef>,
36-
pub(crate) constants: IndexMap<mir::Const<'tcx>, ConstId>,
36+
pub(crate) ty_consts: IndexMap<ty::Const<'tcx>, TyConstId>,
37+
pub(crate) mir_consts: IndexMap<mir::Const<'tcx>, MirConstId>,
3738
pub(crate) layouts: IndexMap<rustc_target::abi::Layout<'tcx>, Layout>,
3839
}
3940

@@ -42,8 +43,12 @@ impl<'tcx> Tables<'tcx> {
4243
self.types.create_or_fetch(ty)
4344
}
4445

45-
pub(crate) fn intern_const(&mut self, constant: mir::Const<'tcx>) -> ConstId {
46-
self.constants.create_or_fetch(constant)
46+
pub(crate) fn intern_ty_const(&mut self, ct: ty::Const<'tcx>) -> TyConstId {
47+
self.ty_consts.create_or_fetch(ct)
48+
}
49+
50+
pub(crate) fn intern_mir_const(&mut self, constant: mir::Const<'tcx>) -> MirConstId {
51+
self.mir_consts.create_or_fetch(constant)
4752
}
4853

4954
pub(crate) fn has_body(&self, instance: Instance<'tcx>) -> bool {

0 commit comments

Comments
 (0)