@@ -1265,15 +1265,78 @@ the `Value` sort.
12651265Conversion is especially possible for the case of _ Slices_ (of dynamic length) and _ Arrays_ (of static length),
12661266which have the same representation ` Value::Range ` .
12671267
1268+ When the cast crosses transparent wrappers (for example newtypes that just forward field ` 0 ` ), the pointer's
1269+ ` Place ` must be realigned. ` #alignTransparentPlace ` rewrites the projection list until the source and target
1270+ expose the same inner value:
1271+ - if the source unwraps more than the target, append an explicit ` field(0) ` so the target still sees that field;
1272+ - if the target unwraps more, strip any redundant tail projections with ` #popTransparentTailTo ` , leaving the
1273+ canonical prefix shared by both sides.
1274+
12681275``` k
12691276 rule <k> #cast(PtrLocal(OFFSET, PLACE, MUT, META), castKindPtrToPtr, TY_SOURCE, TY_TARGET)
12701277 =>
1271- PtrLocal(OFFSET, PLACE, MUT, #convertMetadata(META, lookupTy(TY_TARGET)))
1278+ PtrLocal(
1279+ OFFSET,
1280+ #alignTransparentPlace(
1281+ PLACE,
1282+ #lookupMaybeTy(pointeeTy(lookupTy(TY_SOURCE))),
1283+ #lookupMaybeTy(pointeeTy(lookupTy(TY_TARGET)))
1284+ ),
1285+ MUT,
1286+ #convertMetadata(META, lookupTy(TY_TARGET))
1287+ )
12721288 ...
12731289 </k>
12741290 requires #typesCompatible(lookupTy(TY_SOURCE), lookupTy(TY_TARGET))
12751291 [preserves-definedness] // valid map lookups checked
12761292
1293+ syntax Place ::= #alignTransparentPlace ( Place , TypeInfo , TypeInfo ) [function, total]
1294+ syntax ProjectionElems ::= #popTransparentTailTo ( ProjectionElems , TypeInfo ) [function, total]
1295+
1296+ rule #alignTransparentPlace(place(LOCAL, PROJS), typeInfoStructType(_, _, FIELD_TY .Tys, LAYOUT) #as SOURCE, TARGET)
1297+ => #alignTransparentPlace(
1298+ place(
1299+ LOCAL,
1300+ appendP(PROJS, projectionElemField(fieldIdx(0), FIELD_TY) .ProjectionElems)
1301+ ),
1302+ lookupTy(FIELD_TY),
1303+ TARGET
1304+ )
1305+ requires #transparentDepth(SOURCE) >Int #transparentDepth(TARGET)
1306+ andBool #zeroFieldOffset(LAYOUT)
1307+
1308+ rule #alignTransparentPlace(
1309+ place(LOCAL, PROJS),
1310+ SOURCE,
1311+ typeInfoStructType(_, _, FIELD_TY .Tys, LAYOUT) #as TARGET
1312+ )
1313+ => #alignTransparentPlace(
1314+ place(LOCAL, #popTransparentTailTo(PROJS, lookupTy(FIELD_TY))),
1315+ SOURCE,
1316+ lookupTy(FIELD_TY)
1317+ )
1318+ requires #transparentDepth(SOURCE) <Int #transparentDepth(TARGET)
1319+ andBool #zeroFieldOffset(LAYOUT)
1320+ andBool PROJS =/=K #popTransparentTailTo(PROJS, lookupTy(FIELD_TY))
1321+
1322+ rule #alignTransparentPlace(PLACE, _, _) => PLACE [owise]
1323+
1324+ rule #popTransparentTailTo(
1325+ projectionElemField(fieldIdx(0), FIELD_TY) .ProjectionElems,
1326+ TARGET
1327+ )
1328+ => .ProjectionElems
1329+ requires lookupTy(FIELD_TY) ==K TARGET
1330+
1331+ rule #popTransparentTailTo(
1332+ X:ProjectionElem REST:ProjectionElems,
1333+ TARGET
1334+ )
1335+ => X #popTransparentTailTo(REST, TARGET)
1336+ requires REST =/=K .ProjectionElems
1337+
1338+ rule #popTransparentTailTo(PROJS, _) => PROJS [owise]
1339+
12771340 syntax Metadata ::= #convertMetadata ( Metadata , TypeInfo ) [function, total]
12781341 // -------------------------------------------------------------------------------------
12791342```
0 commit comments