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