Skip to content

Commit 41364c7

Browse files
authored
Rollup merge of #114320 - ouz-a:smir_statements, r=oli-obk
Cover statements for stable_mir Added missing statements to stable_mir, used opaque types for few types that are only used for diagnostic. cc rust-lang/project-stable-mir#16 r? `@oli-obk`
2 parents 00ad3cc + 2ff62fd commit 41364c7

File tree

2 files changed

+279
-15
lines changed

2 files changed

+279
-15
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+176-13
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
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, VariantIdx};
1112
use crate::stable_mir::ty::{FloatTy, IntTy, Movability, RigidTy, TyKind, UintTy};
1213
use crate::stable_mir::{self, Context};
1314
use rustc_hir as hir;
14-
use rustc_middle::mir;
15-
use rustc_middle::ty::{self, Ty, TyCtxt};
15+
use rustc_middle::mir::coverage::CodeRegion;
16+
use rustc_middle::mir::{self};
17+
use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
1618
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1719
use rustc_target::abi::FieldIdx;
1820
use tracing::debug;
@@ -110,17 +112,38 @@ impl<'tcx> Stable<'tcx> for mir::Statement<'tcx> {
110112
Assign(assign) => {
111113
stable_mir::mir::Statement::Assign(assign.0.stable(tables), assign.1.stable(tables))
112114
}
113-
FakeRead(_) => todo!(),
114-
SetDiscriminant { .. } => todo!(),
115-
Deinit(_) => todo!(),
116-
StorageLive(_) => todo!(),
117-
StorageDead(_) => todo!(),
118-
Retag(_, _) => todo!(),
119-
PlaceMention(_) => todo!(),
120-
AscribeUserType(_, _) => todo!(),
121-
Coverage(_) => todo!(),
122-
Intrinsic(_) => todo!(),
123-
ConstEvalCounter => todo!(),
115+
FakeRead(fake_read_place) => stable_mir::mir::Statement::FakeRead(
116+
fake_read_place.0.stable(tables),
117+
fake_read_place.1.stable(tables),
118+
),
119+
SetDiscriminant { place: plc, variant_index: idx } => {
120+
stable_mir::mir::Statement::SetDiscriminant {
121+
place: plc.as_ref().stable(tables),
122+
variant_index: idx.stable(tables),
123+
}
124+
}
125+
Deinit(place) => stable_mir::mir::Statement::Deinit(place.stable(tables)),
126+
StorageLive(place) => stable_mir::mir::Statement::StorageLive(place.stable(tables)),
127+
StorageDead(place) => stable_mir::mir::Statement::StorageDead(place.stable(tables)),
128+
Retag(retag, place) => {
129+
stable_mir::mir::Statement::Retag(retag.stable(tables), place.stable(tables))
130+
}
131+
PlaceMention(place) => stable_mir::mir::Statement::PlaceMention(place.stable(tables)),
132+
AscribeUserType(place_projection, variance) => {
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+
}
138+
}
139+
Coverage(coverage) => stable_mir::mir::Statement::Coverage(stable_mir::mir::Coverage {
140+
kind: coverage.kind.stable(tables),
141+
code_region: coverage.code_region.as_ref().map(|reg| reg.stable(tables)),
142+
}),
143+
Intrinsic(intrinstic) => {
144+
stable_mir::mir::Statement::Intrinsic(intrinstic.stable(tables))
145+
}
146+
ConstEvalCounter => stable_mir::mir::Statement::ConstEvalCounter,
124147
Nop => stable_mir::mir::Statement::Nop,
125148
}
126149
}
@@ -364,6 +387,22 @@ impl<'tcx> Stable<'tcx> for rustc_hir::Unsafety {
364387
}
365388
}
366389

390+
impl<'tcx> Stable<'tcx> for mir::FakeReadCause {
391+
type T = stable_mir::mir::FakeReadCause;
392+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
393+
use mir::FakeReadCause::*;
394+
match self {
395+
ForMatchGuard => stable_mir::mir::FakeReadCause::ForMatchGuard,
396+
ForMatchedPlace(local_def_id) => {
397+
stable_mir::mir::FakeReadCause::ForMatchedPlace(opaque(local_def_id))
398+
}
399+
ForGuardBinding => stable_mir::mir::FakeReadCause::ForGuardBinding,
400+
ForLet(local_def_id) => stable_mir::mir::FakeReadCause::ForLet(opaque(local_def_id)),
401+
ForIndex => stable_mir::mir::FakeReadCause::ForIndex,
402+
}
403+
}
404+
}
405+
367406
impl<'tcx> Stable<'tcx> for FieldIdx {
368407
type T = usize;
369408
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
@@ -393,6 +432,110 @@ impl<'tcx> Stable<'tcx> for mir::Place<'tcx> {
393432
}
394433
}
395434

435+
impl<'tcx> Stable<'tcx> for mir::coverage::CoverageKind {
436+
type T = stable_mir::mir::CoverageKind;
437+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
438+
use rustc_middle::mir::coverage::CoverageKind;
439+
match self {
440+
CoverageKind::Counter { function_source_hash, id } => {
441+
stable_mir::mir::CoverageKind::Counter {
442+
function_source_hash: *function_source_hash as usize,
443+
id: opaque(id),
444+
}
445+
}
446+
CoverageKind::Expression { id, lhs, op, rhs } => {
447+
stable_mir::mir::CoverageKind::Expression {
448+
id: opaque(id),
449+
lhs: opaque(lhs),
450+
op: op.stable(tables),
451+
rhs: opaque(rhs),
452+
}
453+
}
454+
CoverageKind::Unreachable => stable_mir::mir::CoverageKind::Unreachable,
455+
}
456+
}
457+
}
458+
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+
467+
impl<'tcx> Stable<'tcx> for mir::coverage::Op {
468+
type T = stable_mir::mir::Op;
469+
470+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
471+
use rustc_middle::mir::coverage::Op::*;
472+
match self {
473+
Subtract => stable_mir::mir::Op::Subtract,
474+
Add => stable_mir::mir::Op::Add,
475+
}
476+
}
477+
}
478+
479+
impl<'tcx> Stable<'tcx> for mir::Local {
480+
type T = stable_mir::mir::Local;
481+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
482+
self.as_usize()
483+
}
484+
}
485+
486+
impl<'tcx> Stable<'tcx> for rustc_target::abi::VariantIdx {
487+
type T = VariantIdx;
488+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
489+
self.as_usize()
490+
}
491+
}
492+
493+
impl<'tcx> Stable<'tcx> for Variance {
494+
type T = stable_mir::mir::Variance;
495+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
496+
match self {
497+
Variance::Bivariant => stable_mir::mir::Variance::Bivariant,
498+
Variance::Contravariant => stable_mir::mir::Variance::Contravariant,
499+
Variance::Covariant => stable_mir::mir::Variance::Covariant,
500+
Variance::Invariant => stable_mir::mir::Variance::Invariant,
501+
}
502+
}
503+
}
504+
505+
impl<'tcx> Stable<'tcx> for mir::RetagKind {
506+
type T = stable_mir::mir::RetagKind;
507+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
508+
use rustc_middle::mir::RetagKind;
509+
match self {
510+
RetagKind::FnEntry => stable_mir::mir::RetagKind::FnEntry,
511+
RetagKind::TwoPhase => stable_mir::mir::RetagKind::TwoPhase,
512+
RetagKind::Raw => stable_mir::mir::RetagKind::Raw,
513+
RetagKind::Default => stable_mir::mir::RetagKind::Default,
514+
}
515+
}
516+
}
517+
518+
impl<'tcx> Stable<'tcx> for rustc_middle::ty::UserTypeAnnotationIndex {
519+
type T = usize;
520+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
521+
self.as_usize()
522+
}
523+
}
524+
525+
impl<'tcx> Stable<'tcx> for CodeRegion {
526+
type T = stable_mir::mir::CodeRegion;
527+
528+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
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,
535+
}
536+
}
537+
}
538+
396539
impl<'tcx> Stable<'tcx> for mir::UnwindAction {
397540
type T = stable_mir::mir::UnwindAction;
398541
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
@@ -406,6 +549,26 @@ impl<'tcx> Stable<'tcx> for mir::UnwindAction {
406549
}
407550
}
408551

552+
impl<'tcx> Stable<'tcx> for mir::NonDivergingIntrinsic<'tcx> {
553+
type T = stable_mir::mir::NonDivergingIntrinsic;
554+
555+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
556+
use rustc_middle::mir::NonDivergingIntrinsic;
557+
match self {
558+
NonDivergingIntrinsic::Assume(op) => {
559+
stable_mir::mir::NonDivergingIntrinsic::Assume(op.stable(tables))
560+
}
561+
NonDivergingIntrinsic::CopyNonOverlapping(copy_non_overlapping) => {
562+
stable_mir::mir::NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping {
563+
src: copy_non_overlapping.src.stable(tables),
564+
dst: copy_non_overlapping.dst.stable(tables),
565+
count: copy_non_overlapping.count.stable(tables),
566+
})
567+
}
568+
}
569+
}
570+
}
571+
409572
impl<'tcx> Stable<'tcx> for mir::AssertMessage<'tcx> {
410573
type T = stable_mir::mir::AssertMessage;
411574
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {

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

+103-2
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,9 +134,101 @@ pub enum AsyncGeneratorKind {
133134
Fn,
134135
}
135136

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+
142+
/// The FakeReadCause describes the type of pattern why a FakeRead statement exists.
143+
#[derive(Clone, Debug)]
144+
pub enum FakeReadCause {
145+
ForMatchGuard,
146+
ForMatchedPlace(LocalDefId),
147+
ForGuardBinding,
148+
ForLet(LocalDefId),
149+
ForIndex,
150+
}
151+
152+
/// Describes what kind of retag is to be performed
153+
#[derive(Clone, Debug)]
154+
pub enum RetagKind {
155+
FnEntry,
156+
TwoPhase,
157+
Raw,
158+
Default,
159+
}
160+
161+
#[derive(Clone, Debug)]
162+
pub enum Variance {
163+
Covariant,
164+
Invariant,
165+
Contravariant,
166+
Bivariant,
167+
}
168+
169+
#[derive(Clone, Debug)]
170+
pub enum Op {
171+
Subtract,
172+
Add,
173+
}
174+
175+
#[derive(Clone, Debug)]
176+
pub enum CoverageKind {
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+
},
187+
Unreachable,
188+
}
189+
190+
#[derive(Clone, Debug)]
191+
pub struct CodeRegion {
192+
pub file_name: String,
193+
pub start_line: usize,
194+
pub start_col: usize,
195+
pub end_line: usize,
196+
pub end_col: usize,
197+
}
198+
199+
#[derive(Clone, Debug)]
200+
pub struct Coverage {
201+
pub kind: CoverageKind,
202+
pub code_region: Option<CodeRegion>,
203+
}
204+
205+
#[derive(Clone, Debug)]
206+
pub struct CopyNonOverlapping {
207+
pub src: Operand,
208+
pub dst: Operand,
209+
pub count: Operand,
210+
}
211+
212+
#[derive(Clone, Debug)]
213+
pub enum NonDivergingIntrinsic {
214+
Assume(Operand),
215+
CopyNonOverlapping(CopyNonOverlapping),
216+
}
217+
136218
#[derive(Clone, Debug)]
137219
pub enum Statement {
138220
Assign(Place, Rvalue),
221+
FakeRead(FakeReadCause, Place),
222+
SetDiscriminant { place: Place, variant_index: VariantIdx },
223+
Deinit(Place),
224+
StorageLive(Local),
225+
StorageDead(Local),
226+
Retag(RetagKind, Place),
227+
PlaceMention(Place),
228+
AscribeUserType { place: Place, projections: UserTypeProjection, variance: Variance },
229+
Coverage(Coverage),
230+
Intrinsic(NonDivergingIntrinsic),
231+
ConstEvalCounter,
139232
Nop,
140233
}
141234

@@ -271,14 +364,22 @@ pub enum Operand {
271364

272365
#[derive(Clone, Debug)]
273366
pub struct Place {
274-
pub local: usize,
367+
pub local: Local,
275368
pub projection: String,
276369
}
277370

371+
#[derive(Clone, Debug)]
372+
pub struct UserTypeProjection {
373+
pub base: UserTypeAnnotationIndex,
374+
pub projection: String,
375+
}
376+
377+
pub type Local = usize;
378+
278379
type FieldIdx = usize;
279380

280381
/// The source-order index of a variant in a type.
281-
type VariantIdx = usize;
382+
pub type VariantIdx = usize;
282383

283384
type UserTypeAnnotationIndex = usize;
284385

0 commit comments

Comments
 (0)