Skip to content

Conversation

@bioball
Copy link
Member

@bioball bioball commented Dec 23, 2025

This adds power assertions to Pkl!

This implements the SPICE described in apple/pkl-evolution#29

This follows the power assertions style of reporting also found in
Groovy, Kotlin, and others.

  • Literal values are not emitted in the diagram
  • Stdlib constructors of literals like List(1, 2) are also considered
    literals

Power assertions are added to:

  • Failing type constraints
  • Failing test facts

Power assertions are implemented as a truffle instrument to observe execution.
When an assertion fails, the instrument is created and the assertion is run again to observe facts.
This incurs runtime overhead to collect facts, but has no impact on code in the non-error case.

Samples:

module test
  facts
    ✘ math
       myList.fold(0, (a, b) -> a + b) == 5 (/Users/danielchao/code/apple/pkl/.dan-scripts/test.pkl:6)
       |      |                        |
       |      6                        false
       List(1, 2, 3)

0.0% tests pass [1/1 failed], 0.0% asserts pass [1/1 failed]
–– Pkl Error ––
Type constraint `this == this.capitalize()` violated.
Value: "fooey"

    this == this.capitalize()
    |    |  |    |
    |    |  |    "Fooey"
    |    |  "fooey"
    |    false
    "fooey"

1 | typealias CapitalizedString = String(this == this.capitalize())
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^
at test#foo (/Users/danielchao/code/apple/pkl/.dan-scripts/test.pkl:1)

3 | foo: CapitalizedString = "fooey"
                             ^^^^^^^
at test#foo (/Users/danielchao/code/apple/pkl/.dan-scripts/test.pkl:3)

bioball added a commit that referenced this pull request Jan 6, 2026
This adds syntax highlighting of Pkl code!

It adds highlighting for:

* Stack frames within error messages
* CLI REPL (highlights as you type, highlights error output)
* Power assertions (coming in #1384)

This uses the lexer for highlighting. It will highlight strings,
numbers, keywords, but doesn't understand how to highlight nodes like
types, function params, etc.
The reason for this is because a single line of code by itself may not
be grammatically valid.
Copy link
Contributor

@HT154 HT154 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!! Just a few minor things.

Copy link
Contributor

@stackoverflow stackoverflow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!
Just some small things and clarifications.

} catch (ex: Exception) {
errWriter.appendLine("Error evaluating module ${moduleUri.path}:")
errWriter.write(ex.message ?: "")
errWriter.write(ex.stackTraceToString())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intended to be here, or is it a debug leftover?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional, but this is a bugfix unrelated to this feature. I'll pull this into a separate PR

Copy link
Contributor

@stackoverflow stackoverflow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

bioball and others added 10 commits January 20, 2026 14:11
This annotates failing assertions in type constraints, and in tests
with the values produced during code execution.

This follows the power assertions style of reporting also found in
Groovy, Kotlin, and others.

* Literal values are not emitted in the value graph
* Stdlib constructors of literals like `List(1, 2)` are also considered
  literals
@bioball bioball merged commit 03a7676 into apple:main Jan 21, 2026
17 checks passed
@bioball bioball deleted the power-asserts branch January 21, 2026 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants