Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crucible-mir: Improve pretty-printing of ZST constants #1312

Merged
merged 1 commit into from
Feb 20, 2025
Merged
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
31 changes: 29 additions & 2 deletions crucible-mir/src/Mir/PP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,27 @@ instance Pretty Operand where
pretty (Temp c) = pretty c

instance Pretty Constant where
pretty (Constant _a b) = pretty b
pretty (Constant a b) =
case (a, b) of
-- We include two special cases for ConstZST that leverage its type to
-- improve the pretty-printing:
--
-- - If the type is TyFnDef, then we pretty-print the function's DefId.
-- (This is crucial to ensure that pretty-printing function
-- applications reveals the name of the applied function.)
--
-- - Otherwise, we print <ZST: [ty]>, where [ty] is the pretty-printed
-- type.
--
-- There is also a case for ConstZST in the `Pretty ConstVal` instance,
-- but the output there (<ZST>) is much less informative, as it cannot
-- take the type of the ConstZST into account.
(TyFnDef defId, ConstZST) ->
pr_id defId
(_, ConstZST) ->
pretty "<ZST:" <+> pretty a <> pretty ">"
_ ->
pretty b

instance Pretty NullOp where
pretty SizeOf = pretty "sizeof"
Expand Down Expand Up @@ -323,7 +343,14 @@ instance Pretty ConstVal where
pretty (ConstFunction a) = pr_id a
pretty (ConstInitializer a) = pr_id a
pretty (ConstStaticRef a) = pretty "&" <> pr_id a
pretty ConstZST = pretty "<ZST>"
pretty ConstZST =
-- A ConstZST value represents a value with a zero-sized type, but we
-- cannot know what type it is by looking at the value in isolation. Note
-- that the `Pretty Constant` instance includes a special case for
-- ConstZST that value along with its type for clarity. In practice, most
-- ConstZSTs will be printed via the `Pretty Constant` instance, not the
-- code below.
pretty "<ZST>"
pretty (ConstRawPtr a) = pretty a
pretty (ConstStruct fs) = pretty "struct" <> list (map pretty fs)
pretty (ConstEnum v fs) = pretty "enum" <> list ((pretty "variant" <+> pretty v) : map pretty fs)
Expand Down
Loading