Skip to content

Commit 290daf9

Browse files
authored
Rollup merge of #117417 - celinval:smir-visitor, r=oli-obk
Add a stable MIR visitor This change also adds a few utility functions as well and extend most `mir` and `ty` ADTs to implement `PartialEq` and `Eq`. Fixes rust-lang/project-stable-mir#32 r? `@oli-obk`
2 parents 83990ba + af7472e commit 290daf9

File tree

8 files changed

+662
-85
lines changed

8 files changed

+662
-85
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
216216
tables.create_def_id(def_id)
217217
}
218218

219+
fn instance_mangled_name(&self, def: InstanceDef) -> String {
220+
let tables = self.0.borrow_mut();
221+
let instance = tables.instances[def];
222+
tables.tcx.symbol_name(instance).name.to_string()
223+
}
224+
219225
fn mono_instance(&self, item: stable_mir::CrateItem) -> stable_mir::mir::mono::Instance {
220226
let mut tables = self.0.borrow_mut();
221227
let def_id = tables[item.0];

compiler/stable_mir/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ pub type DefKind = Opaque;
103103
pub type Filename = Opaque;
104104

105105
/// Holds information about an item in the crate.
106-
/// For now, it only stores the item DefId. Use functions inside `rustc_internal` module to
107-
/// use this item.
108106
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
109107
pub struct CrateItem(pub DefId);
110108

@@ -224,6 +222,9 @@ pub trait Context {
224222
/// Get the instance.
225223
fn instance_def_id(&self, instance: InstanceDef) -> DefId;
226224

225+
/// Get the instance mangled name.
226+
fn instance_mangled_name(&self, instance: InstanceDef) -> String;
227+
227228
/// Convert a non-generic crate item into an instance.
228229
/// This function will panic if the item is generic.
229230
fn mono_instance(&self, item: CrateItem) -> Instance;
@@ -259,7 +260,7 @@ pub fn with<R>(f: impl FnOnce(&dyn Context) -> R) -> R {
259260
}
260261

261262
/// A type that provides internal information but that can still be used for debug purpose.
262-
#[derive(Clone)]
263+
#[derive(Clone, Eq, PartialEq)]
263264
pub struct Opaque(String);
264265

265266
impl std::fmt::Display for Opaque {

compiler/stable_mir/src/mir.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
mod body;
22
pub mod mono;
3+
pub mod visit;
34

45
pub use body::*;
6+
pub use visit::MirVisitor;

compiler/stable_mir/src/mir/body.rs

+38-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::ty::{AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region};
1+
use crate::ty::{AdtDef, ClosureDef, Const, CoroutineDef, GenericArgs, Movability, Region, Ty};
22
use crate::Opaque;
3-
use crate::{ty::Ty, Span};
3+
use crate::Span;
44

55
/// The SMIR representation of a single function.
66
#[derive(Clone, Debug)]
@@ -12,10 +12,10 @@ pub struct Body {
1212
// The first local is the return value pointer, followed by `arg_count`
1313
// locals for the function arguments, followed by any user-declared
1414
// variables and temporaries.
15-
locals: LocalDecls,
15+
pub(super) locals: LocalDecls,
1616

1717
// The number of arguments this function takes.
18-
arg_count: usize,
18+
pub(super) arg_count: usize,
1919
}
2020

2121
impl Body {
@@ -35,7 +35,7 @@ impl Body {
3535

3636
/// Return local that holds this function's return value.
3737
pub fn ret_local(&self) -> &LocalDecl {
38-
&self.locals[0]
38+
&self.locals[RETURN_LOCAL]
3939
}
4040

4141
/// Locals in `self` that correspond to this function's arguments.
@@ -60,7 +60,7 @@ impl Body {
6060

6161
type LocalDecls = Vec<LocalDecl>;
6262

63-
#[derive(Clone, Debug)]
63+
#[derive(Clone, Debug, Eq, PartialEq)]
6464
pub struct LocalDecl {
6565
pub ty: Ty,
6666
pub span: Span,
@@ -72,13 +72,13 @@ pub struct BasicBlock {
7272
pub terminator: Terminator,
7373
}
7474

75-
#[derive(Clone, Debug)]
75+
#[derive(Clone, Debug, Eq, PartialEq)]
7676
pub struct Terminator {
7777
pub kind: TerminatorKind,
7878
pub span: Span,
7979
}
8080

81-
#[derive(Clone, Debug)]
81+
#[derive(Clone, Debug, Eq, PartialEq)]
8282
pub enum TerminatorKind {
8383
Goto {
8484
target: usize,
@@ -122,7 +122,7 @@ pub enum TerminatorKind {
122122
},
123123
}
124124

125-
#[derive(Clone, Debug)]
125+
#[derive(Clone, Debug, Eq, PartialEq)]
126126
pub struct InlineAsmOperand {
127127
pub in_value: Option<Operand>,
128128
pub out_place: Option<Place>,
@@ -131,15 +131,15 @@ pub struct InlineAsmOperand {
131131
pub raw_rpr: String,
132132
}
133133

134-
#[derive(Clone, Debug)]
134+
#[derive(Clone, Debug, Eq, PartialEq)]
135135
pub enum UnwindAction {
136136
Continue,
137137
Unreachable,
138138
Terminate,
139139
Cleanup(usize),
140140
}
141141

142-
#[derive(Clone, Debug)]
142+
#[derive(Clone, Debug, Eq, PartialEq)]
143143
pub enum AssertMessage {
144144
BoundsCheck { len: Operand, index: Operand },
145145
Overflow(BinOp, Operand, Operand),
@@ -151,7 +151,7 @@ pub enum AssertMessage {
151151
MisalignedPointerDereference { required: Operand, found: Operand },
152152
}
153153

154-
#[derive(Clone, Debug)]
154+
#[derive(Clone, Debug, Eq, PartialEq)]
155155
pub enum BinOp {
156156
Add,
157157
AddUnchecked,
@@ -177,20 +177,20 @@ pub enum BinOp {
177177
Offset,
178178
}
179179

180-
#[derive(Clone, Debug)]
180+
#[derive(Clone, Debug, Eq, PartialEq)]
181181
pub enum UnOp {
182182
Not,
183183
Neg,
184184
}
185185

186-
#[derive(Clone, Debug)]
186+
#[derive(Clone, Debug, Eq, PartialEq)]
187187
pub enum CoroutineKind {
188188
Async(CoroutineSource),
189189
Coroutine,
190190
Gen(CoroutineSource),
191191
}
192192

193-
#[derive(Clone, Debug)]
193+
#[derive(Clone, Debug, Eq, PartialEq)]
194194
pub enum CoroutineSource {
195195
Block,
196196
Closure,
@@ -204,7 +204,7 @@ pub(crate) type LocalDefId = Opaque;
204204
pub(crate) type Coverage = Opaque;
205205

206206
/// The FakeReadCause describes the type of pattern why a FakeRead statement exists.
207-
#[derive(Clone, Debug)]
207+
#[derive(Clone, Debug, Eq, PartialEq)]
208208
pub enum FakeReadCause {
209209
ForMatchGuard,
210210
ForMatchedPlace(LocalDefId),
@@ -214,42 +214,42 @@ pub enum FakeReadCause {
214214
}
215215

216216
/// Describes what kind of retag is to be performed
217-
#[derive(Clone, Debug)]
217+
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
218218
pub enum RetagKind {
219219
FnEntry,
220220
TwoPhase,
221221
Raw,
222222
Default,
223223
}
224224

225-
#[derive(Clone, Debug)]
225+
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
226226
pub enum Variance {
227227
Covariant,
228228
Invariant,
229229
Contravariant,
230230
Bivariant,
231231
}
232232

233-
#[derive(Clone, Debug)]
233+
#[derive(Clone, Debug, Eq, PartialEq)]
234234
pub struct CopyNonOverlapping {
235235
pub src: Operand,
236236
pub dst: Operand,
237237
pub count: Operand,
238238
}
239239

240-
#[derive(Clone, Debug)]
240+
#[derive(Clone, Debug, Eq, PartialEq)]
241241
pub enum NonDivergingIntrinsic {
242242
Assume(Operand),
243243
CopyNonOverlapping(CopyNonOverlapping),
244244
}
245245

246-
#[derive(Clone, Debug)]
246+
#[derive(Clone, Debug, Eq, PartialEq)]
247247
pub struct Statement {
248248
pub kind: StatementKind,
249249
pub span: Span,
250250
}
251251

252-
#[derive(Clone, Debug)]
252+
#[derive(Clone, Debug, Eq, PartialEq)]
253253
pub enum StatementKind {
254254
Assign(Place, Rvalue),
255255
FakeRead(FakeReadCause, Place),
@@ -266,7 +266,7 @@ pub enum StatementKind {
266266
Nop,
267267
}
268268

269-
#[derive(Clone, Debug)]
269+
#[derive(Clone, Debug, Eq, PartialEq)]
270270
pub enum Rvalue {
271271
/// Creates a pointer with the indicated mutability to the place.
272272
///
@@ -378,7 +378,7 @@ pub enum Rvalue {
378378
Use(Operand),
379379
}
380380

381-
#[derive(Clone, Debug)]
381+
#[derive(Clone, Debug, Eq, PartialEq)]
382382
pub enum AggregateKind {
383383
Array(Ty),
384384
Tuple,
@@ -387,49 +387,51 @@ pub enum AggregateKind {
387387
Coroutine(CoroutineDef, GenericArgs, Movability),
388388
}
389389

390-
#[derive(Clone, Debug)]
390+
#[derive(Clone, Debug, Eq, PartialEq)]
391391
pub enum Operand {
392392
Copy(Place),
393393
Move(Place),
394394
Constant(Constant),
395395
}
396396

397-
#[derive(Clone, Debug)]
397+
#[derive(Clone, Debug, Eq, PartialEq)]
398398
pub struct Place {
399399
pub local: Local,
400400
/// projection out of a place (access a field, deref a pointer, etc)
401401
pub projection: String,
402402
}
403403

404-
#[derive(Clone, Debug)]
404+
#[derive(Clone, Debug, Eq, PartialEq)]
405405
pub struct UserTypeProjection {
406406
pub base: UserTypeAnnotationIndex,
407407
pub projection: String,
408408
}
409409

410410
pub type Local = usize;
411411

412+
pub const RETURN_LOCAL: Local = 0;
413+
412414
type FieldIdx = usize;
413415

414416
/// The source-order index of a variant in a type.
415417
pub type VariantIdx = usize;
416418

417419
type UserTypeAnnotationIndex = usize;
418420

419-
#[derive(Clone, Debug)]
421+
#[derive(Clone, Debug, Eq, PartialEq)]
420422
pub struct Constant {
421423
pub span: Span,
422424
pub user_ty: Option<UserTypeAnnotationIndex>,
423425
pub literal: Const,
424426
}
425427

426-
#[derive(Clone, Debug)]
428+
#[derive(Clone, Debug, Eq, PartialEq)]
427429
pub struct SwitchTarget {
428430
pub value: u128,
429431
pub target: usize,
430432
}
431433

432-
#[derive(Clone, Debug)]
434+
#[derive(Clone, Debug, Eq, PartialEq)]
433435
pub enum BorrowKind {
434436
/// Data must be immutable and is aliasable.
435437
Shared,
@@ -446,26 +448,26 @@ pub enum BorrowKind {
446448
},
447449
}
448450

449-
#[derive(Clone, Debug)]
451+
#[derive(Clone, Debug, Eq, PartialEq)]
450452
pub enum MutBorrowKind {
451453
Default,
452454
TwoPhaseBorrow,
453455
ClosureCapture,
454456
}
455457

456-
#[derive(Clone, Debug)]
458+
#[derive(Clone, Debug, PartialEq, Eq)]
457459
pub enum Mutability {
458460
Not,
459461
Mut,
460462
}
461463

462-
#[derive(Copy, Clone, Debug)]
464+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
463465
pub enum Safety {
464466
Unsafe,
465467
Normal,
466468
}
467469

468-
#[derive(Clone, Debug)]
470+
#[derive(Clone, Debug, Eq, PartialEq)]
469471
pub enum PointerCoercion {
470472
/// Go from a fn-item type to a fn-pointer type.
471473
ReifyFnPointer,
@@ -492,7 +494,7 @@ pub enum PointerCoercion {
492494
Unsize,
493495
}
494496

495-
#[derive(Clone, Debug)]
497+
#[derive(Clone, Debug, Eq, PartialEq)]
496498
pub enum CastKind {
497499
PointerExposeAddress,
498500
PointerFromExposedAddress,
@@ -507,7 +509,7 @@ pub enum CastKind {
507509
Transmute,
508510
}
509511

510-
#[derive(Clone, Debug)]
512+
#[derive(Clone, Debug, Eq, PartialEq)]
511513
pub enum NullOp {
512514
/// Returns the size of a value of that type.
513515
SizeOf,

compiler/stable_mir/src/mir/mono.rs

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ impl Instance {
4242
with(|context| context.instance_ty(self.def))
4343
}
4444

45+
pub fn mangled_name(&self) -> String {
46+
with(|context| context.instance_mangled_name(self.def))
47+
}
48+
4549
/// Resolve an instance starting from a function definition and generic arguments.
4650
pub fn resolve(def: FnDef, args: &GenericArgs) -> Result<Instance, crate::Error> {
4751
with(|context| {

0 commit comments

Comments
 (0)