Skip to content

Commit ff1a18f

Browse files
authored
Added mutability to PtrType and RefType TypeInfo (#127)
An error was seen with `PtrToPtr` casts in mir-semantics when there is an array of arrays being cast to change mutability. The current model incorrectly assumes it wants to point to an interior element, but this case needs to be distinguished from the case that is purely changing mutability. Therefore the mutability of the pointer needs to be accessible in the `TypeInfo`
1 parent cab07e2 commit ff1a18f

30 files changed

Lines changed: 335 additions & 9 deletions

src/mk_graph/index.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,30 @@ pub struct TypeEntry {
5151
#[derive(Clone)]
5252
pub enum TypeKind {
5353
Primitive,
54-
Struct { fields: Vec<FieldInfo> },
55-
Enum { variants: Vec<VariantInfo> },
56-
Union { fields: Vec<FieldInfo> },
57-
Array { elem_ty: Ty, len: Option<u64> },
58-
Tuple { fields: Vec<Ty> },
59-
Ptr { pointee: Ty },
60-
Ref { pointee: Ty },
54+
Struct {
55+
fields: Vec<FieldInfo>,
56+
},
57+
Enum {
58+
variants: Vec<VariantInfo>,
59+
},
60+
Union {
61+
fields: Vec<FieldInfo>,
62+
},
63+
Array {
64+
elem_ty: Ty,
65+
len: Option<u64>,
66+
},
67+
Tuple {
68+
fields: Vec<Ty>,
69+
},
70+
Ptr {
71+
pointee: Ty,
72+
mutability: stable_mir::mir::Mutability,
73+
},
74+
Ref {
75+
pointee: Ty,
76+
mutability: stable_mir::mir::Mutability,
77+
},
6178
Function,
6279
Void,
6380
}
@@ -365,25 +382,29 @@ impl TypeEntry {
365382
TypeMetadata::PtrType {
366383
pointee_type,
367384
layout,
385+
mutability,
368386
} => {
369387
let layout_info = layout.as_ref().map(LayoutInfo::from_shape);
370388
(
371389
format!("{}", ty),
372390
TypeKind::Ptr {
373391
pointee: *pointee_type,
392+
mutability: *mutability,
374393
},
375394
layout_info,
376395
)
377396
}
378397
TypeMetadata::RefType {
379398
pointee_type,
380399
layout,
400+
mutability,
381401
} => {
382402
let layout_info = layout.as_ref().map(LayoutInfo::from_shape);
383403
(
384404
format!("{}", ty),
385405
TypeKind::Ref {
386406
pointee: *pointee_type,
407+
mutability: *mutability,
387408
},
388409
layout_info,
389410
)

src/printer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,10 +1338,12 @@ pub enum TypeMetadata {
13381338
PtrType {
13391339
pointee_type: stable_mir::ty::Ty,
13401340
layout: Option<LayoutShape>,
1341+
mutability: stable_mir::mir::Mutability,
13411342
},
13421343
RefType {
13431344
pointee_type: stable_mir::ty::Ty,
13441345
layout: Option<LayoutShape>,
1346+
mutability: stable_mir::mir::Mutability,
13451347
},
13461348
TupleType {
13471349
types: Vec<stable_mir::ty::Ty>,
@@ -1459,18 +1461,20 @@ fn mk_type_metadata(
14591461
},
14601462
)),
14611463
// for raw pointers and references store the pointee type
1462-
T(RawPtr(pointee_type, _)) => Some((
1464+
T(RawPtr(pointee_type, mutability)) => Some((
14631465
k,
14641466
PtrType {
14651467
pointee_type,
14661468
layout,
1469+
mutability,
14671470
},
14681471
)),
1469-
T(Ref(_, pointee_type, _)) => Some((
1472+
T(Ref(_, pointee_type, mutability)) => Some((
14701473
k,
14711474
RefType {
14721475
pointee_type,
14731476
layout,
1477+
mutability,
14741478
},
14751479
)),
14761480
// for tuples the element types are provided

tests/integration/programs/assert_eq.smir.json.expected

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4934,6 +4934,7 @@
49344934
}
49354935
}
49364936
},
4937+
"mutability": "Mut",
49374938
"pointee_type": "elided"
49384939
}
49394940
}
@@ -4966,6 +4967,7 @@
49664967
}
49674968
}
49684969
},
4970+
"mutability": "Mut",
49694971
"pointee_type": "elided"
49704972
}
49714973
}
@@ -4998,6 +5000,7 @@
49985000
}
49995001
}
50005002
},
5003+
"mutability": "Not",
50015004
"pointee_type": "elided"
50025005
}
50035006
}
@@ -5030,6 +5033,7 @@
50305033
}
50315034
}
50325035
},
5036+
"mutability": "Not",
50335037
"pointee_type": "elided"
50345038
}
50355039
}
@@ -5062,6 +5066,7 @@
50625066
}
50635067
}
50645068
},
5069+
"mutability": "Not",
50655070
"pointee_type": "elided"
50665071
}
50675072
}
@@ -5094,6 +5099,7 @@
50945099
}
50955100
}
50965101
},
5102+
"mutability": "Mut",
50975103
"pointee_type": "elided"
50985104
}
50995105
}
@@ -5126,6 +5132,7 @@
51265132
}
51275133
}
51285134
},
5135+
"mutability": "Mut",
51295136
"pointee_type": "elided"
51305137
}
51315138
}
@@ -5158,6 +5165,7 @@
51585165
}
51595166
}
51605167
},
5168+
"mutability": "Not",
51615169
"pointee_type": "elided"
51625170
}
51635171
}
@@ -5190,6 +5198,7 @@
51905198
}
51915199
}
51925200
},
5201+
"mutability": "Not",
51935202
"pointee_type": "elided"
51945203
}
51955204
}
@@ -5222,6 +5231,7 @@
52225231
}
52235232
}
52245233
},
5234+
"mutability": "Not",
52255235
"pointee_type": "elided"
52265236
}
52275237
}
@@ -5254,6 +5264,7 @@
52545264
}
52555265
}
52565266
},
5267+
"mutability": "Not",
52575268
"pointee_type": "elided"
52585269
}
52595270
}
@@ -5286,6 +5297,7 @@
52865297
}
52875298
}
52885299
},
5300+
"mutability": "Not",
52895301
"pointee_type": "elided"
52905302
}
52915303
}
@@ -5318,6 +5330,7 @@
53185330
}
53195331
}
53205332
},
5333+
"mutability": "Not",
53215334
"pointee_type": "elided"
53225335
}
53235336
}
@@ -5377,6 +5390,7 @@
53775390
}
53785391
}
53795392
},
5393+
"mutability": "Not",
53805394
"pointee_type": "elided"
53815395
}
53825396
}
@@ -5436,6 +5450,7 @@
54365450
}
54375451
}
54385452
},
5453+
"mutability": "Not",
54395454
"pointee_type": "elided"
54405455
}
54415456
}
@@ -5495,6 +5510,7 @@
54955510
}
54965511
}
54975512
},
5513+
"mutability": "Not",
54985514
"pointee_type": "elided"
54995515
}
55005516
}
@@ -5554,6 +5570,7 @@
55545570
}
55555571
}
55565572
},
5573+
"mutability": "Not",
55575574
"pointee_type": "elided"
55585575
}
55595576
}
@@ -5610,6 +5627,7 @@
56105627
}
56115628
}
56125629
},
5630+
"mutability": "Mut",
56135631
"pointee_type": "elided"
56145632
}
56155633
}
@@ -5666,6 +5684,7 @@
56665684
}
56675685
}
56685686
},
5687+
"mutability": "Not",
56695688
"pointee_type": "elided"
56705689
}
56715690
}
@@ -5722,6 +5741,7 @@
57225741
}
57235742
}
57245743
},
5744+
"mutability": "Not",
57255745
"pointee_type": "elided"
57265746
}
57275747
}

tests/integration/programs/binop.smir.json.expected

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9794,6 +9794,7 @@
97949794
}
97959795
}
97969796
},
9797+
"mutability": "Mut",
97979798
"pointee_type": "elided"
97989799
}
97999800
}
@@ -9826,6 +9827,7 @@
98269827
}
98279828
}
98289829
},
9830+
"mutability": "Not",
98299831
"pointee_type": "elided"
98309832
}
98319833
}
@@ -9858,6 +9860,7 @@
98589860
}
98599861
}
98609862
},
9863+
"mutability": "Not",
98619864
"pointee_type": "elided"
98629865
}
98639866
}
@@ -9890,6 +9893,7 @@
98909893
}
98919894
}
98929895
},
9896+
"mutability": "Mut",
98939897
"pointee_type": "elided"
98949898
}
98959899
}
@@ -9922,6 +9926,7 @@
99229926
}
99239927
}
99249928
},
9929+
"mutability": "Not",
99259930
"pointee_type": "elided"
99269931
}
99279932
}
@@ -9954,6 +9959,7 @@
99549959
}
99559960
}
99569961
},
9962+
"mutability": "Not",
99579963
"pointee_type": "elided"
99589964
}
99599965
}
@@ -9986,6 +9992,7 @@
99869992
}
99879993
}
99889994
},
9995+
"mutability": "Not",
99899996
"pointee_type": "elided"
99909997
}
99919998
}
@@ -10045,6 +10052,7 @@
1004510052
}
1004610053
}
1004710054
},
10055+
"mutability": "Not",
1004810056
"pointee_type": "elided"
1004910057
}
1005010058
}
@@ -10101,6 +10109,7 @@
1010110109
}
1010210110
}
1010310111
},
10112+
"mutability": "Not",
1010410113
"pointee_type": "elided"
1010510114
}
1010610115
}

0 commit comments

Comments
 (0)