Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions kmir/src/kmir/kdist/mir-semantics/rt/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@ A `Deref` projection in the projections list changes the target of the write ope
rule <k> #traverseProjection(_, VAL, .ProjectionElems, _) ~> #readProjection(false) => VAL ... </k>
rule <k> #traverseProjection(_, VAL, .ProjectionElems, _) ~> (#readProjection(true) => #writeMoved ~> VAL) ... </k>

// interior mutability permits projected writeback
rule <k> #traverseProjection(toLocal(I), _ORIGINAL, .ProjectionElems, CONTEXTS)
~> #writeProjection(NEW)
=> #forceSetLocal(local(I), #buildUpdate(NEW, CONTEXTS))
...
</k>
<locals> LOCALS </locals>
requires CONTEXTS =/=K .Contexts
andBool 0 <=Int I andBool I <Int size(LOCALS)
andBool isTypedLocal(LOCALS[I])
andBool #allowsInteriorMutation(lookupTy(tyOfLocal({LOCALS[I]}:>TypedLocal)))
[priority(40), preserves-definedness]

rule <k> #traverseProjection(toLocal(I), _ORIGINAL, .ProjectionElems, CONTEXTS)
~> #writeProjection(NEW)
=> #setLocalValue(place(local(I), .ProjectionElems), #buildUpdate(NEW, CONTEXTS))
Expand Down
17 changes: 17 additions & 0 deletions kmir/src/kmir/kdist/mir-semantics/rt/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ requires "value.md"

module RT-TYPES
imports BOOL
imports INT
imports STRING
imports MAP
imports K-EQUAL

Expand Down Expand Up @@ -101,6 +103,21 @@ To make this function total, an optional `MaybeTy` is used.
rule elemTy( _ ) => TyUnknown [owise]
```

## Interior Mutability

Rust models interior mutability with `UnsafeCell`. When the runtime determines whether a projected write can observe mutation through a shared reference, it relies on `#allowsInteriorMutation` to detect that marker inside Stable MIR type metadata.

```k
syntax Bool ::= #allowsInteriorMutation(TypeInfo) [function, total]
// ------------------------------------------------------
// Stable MIR serializes struct names as either `mirString` or a plain `String`.
rule #allowsInteriorMutation(typeInfoStructType(mirString(NAME), _, _, _))
=> findString(NAME, "UnsafeCell", 0) =/=Int -1
rule #allowsInteriorMutation(typeInfoStructType(NAME:String, _, _, _))
=> findString(NAME, "UnsafeCell", 0) =/=Int -1
rule #allowsInteriorMutation(_) => false [owise]
```

## Static and Dynamic Metadata for Types

References to data on the heap or stack may require metadata, most commonly the size of slices, which is not statically known.
Expand Down