Skip to content

Commit 42a0e57

Browse files
committed
crucible-mir: Support constant trait objects
This bumps the `mir-json` submodule to bring in the changes from GaloisInc/mir-json#240. It also adds `crucible-mir` support for parsing and translating constant trait object values. (See GaloisInc/mir-json#237 for the motivation.) Because these MIR JSON changes require bumping the schema version, we must also do the same on the `crucible-mir` side.
1 parent 45b4bcf commit 42a0e57

File tree

10 files changed

+48
-3
lines changed

10 files changed

+48
-3
lines changed

crucible-mir/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# next
22

3+
This release supports [version
4+
9](https://github.com/GaloisInc/mir-json/blob/master/SCHEMA_CHANGELOG.md#9) of
5+
`mir-json`'s schema.
6+
7+
* Support translating constant trait object values.
8+
39
# 0.6 -- 2026-01-29
410

511
This release supports [version

crucible-mir/src/Mir/JSON.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,12 @@ instance FromJSON ConstVal where
620620
Just (String "fn_ptr") ->
621621
ConstFnPtr <$> v .: "def_id"
622622

623+
Just (String "trait_object") ->
624+
ConstTraitObject
625+
<$> v .: "def_id"
626+
<*> v .: "trait_id"
627+
<*> v .: "vtable"
628+
623629
o -> do
624630
fail $ "parseJSON - bad rendered constant kind: " ++ show o
625631

crucible-mir/src/Mir/Mir.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,11 @@ data ConstVal =
641641
| ConstEnum Int [ConstVal]
642642
| ConstUnion
643643
| ConstFnPtr DefId
644+
-- | A reference to a trait object (e.g., @&dyn Trait@). The 'DefId' is the
645+
-- allocation backing the reference, the 'TraitName' is the name of the
646+
-- trait object's principal trait, and the 'VtableName' is the name of the
647+
-- trait object's vtable.
648+
| ConstTraitObject DefId TraitName VtableName
644649
deriving (Show,Eq, Ord, Generic)
645650

646651
data AggregateKind =

crucible-mir/src/Mir/PP.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ instance Pretty ConstVal where
394394
pretty ConstUnion = pretty "union"
395395
pretty (ConstSliceRef a _len) = pretty "&" <> pr_id a
396396
pretty (ConstFnPtr i) = pretty "fn_ptr" <> brackets (pretty i)
397+
pretty (ConstTraitObject a trait vtable) =
398+
pretty "&trait_object" <> list (map pr_id [a, trait, vtable])
397399

398400
instance Pretty AggregateKind where
399401
pretty (AKArray t) = brackets (pretty t)

crucible-mir/src/Mir/ParseTranslate.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import Debug.Trace
4848
-- If you update the supported mir-json schema version below, make sure to also
4949
-- update the crux-mir README accordingly.
5050
supportedSchemaVersion :: Word64
51-
supportedSchemaVersion = 8
51+
supportedSchemaVersion = 9
5252

5353
-- | Parse a MIR JSON file to a 'Collection'. If parsing fails, attempt to give
5454
-- a more informative error message if the MIR JSON schema version is

crucible-mir/src/Mir/Trans.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ transConstVal ty _ ConstZST = initialValue ty >>= \case
227227
Just x -> return x
228228
Nothing -> mirFail $
229229
"failed to evaluate ZST constant of type " ++ show ty ++ " (initialValue failed)"
230+
transConstVal _ty (Some DynRefRepr) (ConstTraitObject defId traitId vtableId) = do
231+
traitObjectPlace <- staticPlace defId
232+
traitObjectAddr <- addrOfPlace traitObjectPlace
233+
mkTraitObject traitId vtableId traitObjectAddr
230234
transConstVal _ty (Some MirReferenceRepr) (ConstRawPtr i) =
231235
MirExp MirReferenceRepr <$> integerToMirRef (R.App $ usizeLit i)
232236
transConstVal ty@(M.TyAdt aname _ _) tpr (ConstStruct fields) = do

crux-mir/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# next
22

3+
This release supports [version
4+
9](https://github.com/GaloisInc/mir-json/blob/master/SCHEMA_CHANGELOG.md#9) of
5+
`mir-json`'s schema.
6+
37
# 0.12 -- 2026-01-29
48

59
This release supports [version

crux-mir/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Next, navigate to the `crucible/dependencies/mir-json` directory. Install
4949
[the `mir-json` README][mir-json-readme].
5050

5151
Currently, `crux-mir` supports [version
52-
8](https://github.com/GaloisInc/mir-json/blob/master/SCHEMA_CHANGELOG.md#8) of
52+
9](https://github.com/GaloisInc/mir-json/blob/master/SCHEMA_CHANGELOG.md#9) of
5353
`mir-json`'s schema. Note that the schema versions produced by `mir-json` can
5454
change over time as dictated by internal requirements and upstream changes. To
5555
help smooth this over:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub trait Trait {
2+
fn method(&self) -> u32;
3+
}
4+
5+
impl Trait for u32 {
6+
fn method(&self) -> u32 { *self }
7+
}
8+
9+
const X: &dyn Trait = &0u32;
10+
11+
#[cfg_attr(crux, crux::test)]
12+
pub fn crux_test() -> u32 {
13+
X.method()
14+
}
15+
16+
pub fn main() {
17+
println!("{:?}", crux_test());
18+
}

0 commit comments

Comments
 (0)