Skip to content

Commit 2ff62fd

Browse files
committed
clean up, use opaque types
1 parent 206bfc4 commit 2ff62fd

File tree

2 files changed

+53
-42
lines changed

2 files changed

+53
-42
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+30-31
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
1010
use crate::rustc_internal::{self, opaque};
11-
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection};
11+
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
1212
use crate::stable_mir::ty::{FloatTy, IntTy, Movability, RigidTy, TyKind, UintTy};
1313
use crate::stable_mir::{self, Context};
1414
use rustc_hir as hir;
@@ -130,16 +130,11 @@ impl<'tcx> Stable<'tcx> for mir::Statement<'tcx> {
130130
}
131131
PlaceMention(place) => stable_mir::mir::Statement::PlaceMention(place.stable(tables)),
132132
AscribeUserType(place_projection, variance) => {
133-
stable_mir::mir::Statement::AscribeUserType(
134-
(
135-
place_projection.as_ref().0.stable(tables),
136-
UserTypeProjection {
137-
base: place_projection.as_ref().1.base.stable(tables),
138-
projection: format!("{:?}", place_projection.as_ref().1.projs),
139-
},
140-
),
141-
variance.stable(tables),
142-
)
133+
stable_mir::mir::Statement::AscribeUserType {
134+
place: place_projection.as_ref().0.stable(tables),
135+
projections: place_projection.as_ref().1.stable(tables),
136+
variance: variance.stable(tables),
137+
}
143138
}
144139
Coverage(coverage) => stable_mir::mir::Statement::Coverage(stable_mir::mir::Coverage {
145140
kind: coverage.kind.stable(tables),
@@ -398,13 +393,11 @@ impl<'tcx> Stable<'tcx> for mir::FakeReadCause {
398393
use mir::FakeReadCause::*;
399394
match self {
400395
ForMatchGuard => stable_mir::mir::FakeReadCause::ForMatchGuard,
401-
ForMatchedPlace(local_def_id) => stable_mir::mir::FakeReadCause::ForMatchedPlace(
402-
local_def_id.map(|id| id.to_def_id().index.index()),
403-
),
396+
ForMatchedPlace(local_def_id) => {
397+
stable_mir::mir::FakeReadCause::ForMatchedPlace(opaque(local_def_id))
398+
}
404399
ForGuardBinding => stable_mir::mir::FakeReadCause::ForGuardBinding,
405-
ForLet(local_def_id) => stable_mir::mir::FakeReadCause::ForLet(
406-
local_def_id.map(|id| id.to_def_id().index.index()),
407-
),
400+
ForLet(local_def_id) => stable_mir::mir::FakeReadCause::ForLet(opaque(local_def_id)),
408401
ForIndex => stable_mir::mir::FakeReadCause::ForIndex,
409402
}
410403
}
@@ -447,22 +440,30 @@ impl<'tcx> Stable<'tcx> for mir::coverage::CoverageKind {
447440
CoverageKind::Counter { function_source_hash, id } => {
448441
stable_mir::mir::CoverageKind::Counter {
449442
function_source_hash: *function_source_hash as usize,
450-
id: id.as_usize(),
443+
id: opaque(id),
451444
}
452445
}
453446
CoverageKind::Expression { id, lhs, op, rhs } => {
454447
stable_mir::mir::CoverageKind::Expression {
455-
id: id.as_usize(),
456-
lhs: lhs.as_usize(),
448+
id: opaque(id),
449+
lhs: opaque(lhs),
457450
op: op.stable(tables),
458-
rhs: rhs.as_usize(),
451+
rhs: opaque(rhs),
459452
}
460453
}
461454
CoverageKind::Unreachable => stable_mir::mir::CoverageKind::Unreachable,
462455
}
463456
}
464457
}
465458

459+
impl<'tcx> Stable<'tcx> for mir::UserTypeProjection {
460+
type T = stable_mir::mir::UserTypeProjection;
461+
462+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
463+
UserTypeProjection { base: self.base.as_usize(), projection: format!("{:?}", self.projs) }
464+
}
465+
}
466+
466467
impl<'tcx> Stable<'tcx> for mir::coverage::Op {
467468
type T = stable_mir::mir::Op;
468469

@@ -476,14 +477,14 @@ impl<'tcx> Stable<'tcx> for mir::coverage::Op {
476477
}
477478

478479
impl<'tcx> Stable<'tcx> for mir::Local {
479-
type T = usize;
480+
type T = stable_mir::mir::Local;
480481
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
481482
self.as_usize()
482483
}
483484
}
484485

485486
impl<'tcx> Stable<'tcx> for rustc_target::abi::VariantIdx {
486-
type T = usize;
487+
type T = VariantIdx;
487488
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
488489
self.as_usize()
489490
}
@@ -525,14 +526,12 @@ impl<'tcx> Stable<'tcx> for CodeRegion {
525526
type T = stable_mir::mir::CodeRegion;
526527

527528
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
528-
match self {
529-
_ => stable_mir::mir::CodeRegion {
530-
file_name: self.file_name.as_u32() as usize,
531-
start_line: self.start_line as usize,
532-
start_col: self.start_col as usize,
533-
end_line: self.end_line as usize,
534-
end_col: self.end_col as usize,
535-
},
529+
stable_mir::mir::CodeRegion {
530+
file_name: self.file_name.as_str().to_string(),
531+
start_line: self.start_line as usize,
532+
start_col: self.start_col as usize,
533+
end_line: self.end_line as usize,
534+
end_col: self.end_col as usize,
536535
}
537536
}
538537
}

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

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::rustc_internal::Opaque;
12
use crate::stable_mir::ty::{
23
AdtDef, ClosureDef, Const, GeneratorDef, GenericArgs, Movability, Region,
34
};
@@ -133,15 +134,18 @@ pub enum AsyncGeneratorKind {
133134
Fn,
134135
}
135136

136-
// FIXME: We are using `usize` for now but we should be using DefIds and find what
137-
// what those are refering to and then use appropirate ty_defs for them (i.e AdtDef)
137+
pub(crate) type LocalDefId = Opaque;
138+
pub(crate) type CounterValueReference = Opaque;
139+
pub(crate) type InjectedExpressionId = Opaque;
140+
pub(crate) type ExpressionOperandId = Opaque;
141+
138142
/// The FakeReadCause describes the type of pattern why a FakeRead statement exists.
139143
#[derive(Clone, Debug)]
140144
pub enum FakeReadCause {
141145
ForMatchGuard,
142-
ForMatchedPlace(Option<usize>),
146+
ForMatchedPlace(LocalDefId),
143147
ForGuardBinding,
144-
ForLet(Option<usize>),
148+
ForLet(LocalDefId),
145149
ForIndex,
146150
}
147151

@@ -170,14 +174,22 @@ pub enum Op {
170174

171175
#[derive(Clone, Debug)]
172176
pub enum CoverageKind {
173-
Counter { function_source_hash: usize, id: usize },
174-
Expression { id: usize, lhs: usize, op: Op, rhs: usize },
177+
Counter {
178+
function_source_hash: usize,
179+
id: CounterValueReference,
180+
},
181+
Expression {
182+
id: InjectedExpressionId,
183+
lhs: ExpressionOperandId,
184+
op: Op,
185+
rhs: ExpressionOperandId,
186+
},
175187
Unreachable,
176188
}
177189

178-
#[derive(Clone, Copy, Debug)]
190+
#[derive(Clone, Debug)]
179191
pub struct CodeRegion {
180-
pub file_name: usize,
192+
pub file_name: String,
181193
pub start_line: usize,
182194
pub start_col: usize,
183195
pub end_line: usize,
@@ -213,7 +225,7 @@ pub enum Statement {
213225
StorageDead(Local),
214226
Retag(RetagKind, Place),
215227
PlaceMention(Place),
216-
AscribeUserType((Place, UserTypeProjection), Variance),
228+
AscribeUserType { place: Place, projections: UserTypeProjection, variance: Variance },
217229
Coverage(Coverage),
218230
Intrinsic(NonDivergingIntrinsic),
219231
ConstEvalCounter,
@@ -362,12 +374,12 @@ pub struct UserTypeProjection {
362374
pub projection: String,
363375
}
364376

365-
type Local = usize;
377+
pub type Local = usize;
366378

367379
type FieldIdx = usize;
368380

369381
/// The source-order index of a variant in a type.
370-
type VariantIdx = usize;
382+
pub type VariantIdx = usize;
371383

372384
type UserTypeAnnotationIndex = usize;
373385

0 commit comments

Comments
 (0)