-
Notifications
You must be signed in to change notification settings - Fork 465
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
feat: lean CLI option to print messages as JSON #3939
Conversation
Mathlib CI status (docs):
|
src/Lean/Message.lean
Outdated
@[inline] def toMessage (msg : SerialMessage) : Message := | ||
{msg with data := msg.data} | ||
|
||
instance : Coe SerialMessage Message := ⟨SerialMessage.toMessage⟩ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used? Seems like a slightly strange direction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but this is potentially useful for Lean consumers of the JSON messages as they can report them back as normal messages. Since it sounds like we are unsure of this direction, I will remove the coercion.
src/Lean/Message.lean
Outdated
return {msg with data := ← msg.data.toString} | ||
|
||
protected def toString (msg : Message) (includeEndPos := false) : IO String := do | ||
-- Remark: We inline here to avoid a copy in the case were `msg` is shared |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this comment. A copy of what? Should we care, what use case are we talking about that could make this relevant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If msg
is not shared, Lean can just replace its data with its serialized version before applying toString
. However, If the msg
is shared, serializing it will require constructing (and allocating) and new BaseMessage
with the same fields but serialized data (i.e., the "copy"). This will then be immediately deconstructed by the toString
function. With the inline, we avoid this temporary allocation (as the serialization and destruction occur in the same function and thus Lean is smart enough to optimize it away).
Fixes a mixed up between the parameter and global variable for `json_output` the occurred during some name juggling in #3939.
Adds a
--json
option to thelean
CLI. When used, the Lean frontend will print messages as JSON objects using the defaultToJson
encoding for theMessage
structure. This allows consumers (such as Lake) to handle Lean output in a more intelligent, well-structured way.Message
has been refactored intoBaseMessage
,Message
, andSerialMessage
to enable derivingToJson
/FromJson
instances automatically forBaseMessage
/SerialMessage
.SerialMessage
is aMessage
with itsMessageData
eagerly serialized to aString
.