Skip to content

fix(semantics): name the offending expression in unit expression errors - #2671

Open
HuiJun wants to merge 2 commits into
developfrom
fix/unit-expression-error-names-expression
Open

HuiJun wants to merge 2 commits into
developfrom
fix/unit-expression-error-names-expression

Conversation

@HuiJun

@HuiJun HuiJun commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

An expression in unit position that is not a unit was reported by its Go syntax-tree type. 5 [2] now names the expression as the model wrote it.

Before (develop):

$ bin/sysml -e '5 [2]'
sysml: evaluation failed: not a quantity expression: not a measurement unit expression: *ast.LiteralInteger
$ bin/sysml -e '5 ["m"]'
sysml: evaluation failed: not a quantity expression: not a measurement unit expression: *ast.LiteralString

After (this branch):

$ bin/sysml -e '5 [2]'
sysml: evaluation failed: not a quantity expression: not a measurement unit expression: the number 2
$ bin/sysml -e '5 ["m"]'
sysml: evaluation failed: not a quantity expression: not a measurement unit expression: the string "m"
$ bin/sysml -e '5 [1]'
✓ 5 [1]
  = 5 [1]

Root cause: the fall-through of both readers of a unit expression, UnitTermOfExpr (units.go) and UnitProductOfExprBy (unit_product.go), formatted the node with %T. Both now call one helper, describeNonUnitExpr, so the two cannot drift.

Written in unit position Before After
2, 2.5 *ast.LiteralInteger, *ast.LiteralReal the number 2, the number 2.5
"m" *ast.LiteralString the string "m"
true *ast.LiteralBool the Boolean true
null *ast.NullExpr null
* *ast.LiteralInfinity the unbounded value *
f(1) *ast.InvocationExpr an invocation of f
new A() *ast.ConstructorExpr a constructor of A
(1, 2) *ast.SequenceExpr a sequence
anything else, e.g. a.b *ast.FeatureChainExpr an expression that names no unit

The error still wraps ErrUnitExpr, so errors.Is callers are unaffected, and the accepted forms (5 [1], unit names, products, quotients, powers) are untouched.

How it was verified

Try it: make build-sysml && bin/sysml -e '5 [2]'

  • TestUnitExprErrorNamesTheExpression (new, internal/semantic/semantics/units_test.go) checks the message from both readers for each literal kind, an invocation, a sequence and a non-unit inside a product (m*2). It failed first on develop's code:

    --- FAIL: TestUnitExprErrorNamesTheExpression (0.00s)
        units_test.go:323: UnitProductOfExprBy("2") = not a measurement unit expression: *ast.LiteralInteger, want "not a measurement unit expression: the number 2"
        units_test.go:330: UnitTermOfExpr("2") = not a measurement unit expression: *ast.LiteralInteger, want "not a measurement unit expression: the number 2"
        ...
    

    and passes with the fix.

  • The CLI output above is from the binary built at this head; the rows of the table were each run the same way.

  • At this head: gofmt -l . (empty), go build ./... and go vet ./... in the root and tools modules, make lint, python3 scripts/changelog.py check, python3 scripts/check-doc-ids.py, and go test -count=1 on ./tests/hygiene, ./internal/semantic/semantics, ./tests/semantics/..., ./internal/frontend/repl, ./cmd/sysml and ./client/opensysml/....

  • Before merging develop: go test -race on ./internal/semantic/semantics, and go test -count=1 on ./internal/semantic/..., ./internal/exec/runtime, ./internal/frontend/engine, ./internal/frontend/protoconv and ./internal/frontend/symbolfacts.

  • No test, golden or document in the tree pinned the old text.

Not run locally: the full make test (race over every package) and the tools module test suite.

Checklist

  • make test and make lint pass locally (make lint passes; make test was run only for the packages listed above)
  • Tests added or updated for the change
  • Documentation extended where it already covers the surface (no document quotes this message)
  • Changelog entry added as changes/unreleased/<slug>.<section>.md, not as an edit to CHANGELOG.md
  • baselines regenerated and make docs-counts run if a gate count moved (none moved)
  • No internal work-item labels (waves, slices, F4, K5) in the body, docs, or changelog

🟢 Devin Review found nothing to fix · View findings · Follow Devin's work

Devin Review

devin-ai-integration Bot and others added 2 commits November 26, 2026 09:08
A non-unit in unit position was reported by its Go syntax-tree type
(`not a measurement unit expression: *ast.LiteralInteger`). Both readers
of a unit expression now describe it as written: `the number 2`.

Co-Authored-By: jason.han <hanhuijun@gmail.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@devin-ai-integration

devin-ai-integration Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

QA: PASS

Tested head c4e7cbf8ac against base develop 6f13c63949, both built with make build.

Recording (CLI, validate, REPL, then VS Code on both builds):

QA run

What passed

  • Errors name the expression. Through sysml -e, REPL %eval, a feature value (probe.badInt), %calc, %constraint and %action, every case that printed a Go type on develop now prints the PR's wording: the number 2 / 2.5 / 2e3, the string "m", the Boolean true, null, the unbounded value *, an invocation of f (also Paths::C when qualified), a constructor of A, a sequence, and a non-unit inside a product or quotient (m*2, m/"s"). Surrounding context (feature value probe.badInt: ..., the %calc hint about #(…)) is unchanged.
  • No error lost or gained. About 75 inputs were run on both builds and diffed: the only differences are the text after not a measurement unit expression:. Exit codes match.
  • Valid units unchanged. 5 [1], 5 [m] and 3 [km] + 2 [m] (3.002 [km]) evaluate the same, and a model using m/s, m/s**2 and kg*m/s**2 validates the same (those three were not evaluated); a neighbouring 1000.0 [kg] next to a bad attribute still reads back.
  • -validate and the editor are unchanged. Output is byte-identical on base and head for a model of bad unit expressions and one of valid ones; the VS Code Problems view lists the same 9 + 1 quantity-unit warnings with both sysml-lsp builds, and hover is the same.
  • Tests on head: go vet and go test -count=1 on ./internal/semantic/..., ./tests/semantics/..., ./internal/ir/lower/..., ./internal/frontend/repl, ./internal/frontend/symbolfacts, ./tests/hygiene; gofmt clean on the package.
  • Past fixes: the tests mapped to 193 inventory rows for units, quantities, semantics and diagnostics (59 packages) all pass on head, including 14 of the 15 Tim Weilkiens rows.

CLI base vs head

VS Code Problems view, base and head
Base Head
Base Head

Notes (not failures)

  • Expressions outside the listed kinds get the fallback an expression that names no unit, which does not say what was written: 5 [p.good], 5 [1 [m]], 5 [m [2]], a body expression. A chained call (5 [p.good.x(1)]) reads an invocation with no name. This matches the PR's table.
  • Same on base and head: -validate and the editor give no warning for 5 [m*2] or 5 [m/"s"], which then fail on evaluation, and they do warn on 5 [1], which evaluates.

Hosted CI

The first run on this head did no testing: every job was cancelled and the Build and test roll-up failed only because of that. A new run was in progress when this was posted (2 jobs passed, the rest pending), so there is no hosted result yet.

Not run

  • gRPC, MCP, Jupyter and the non-Go clients.
  • -race and the full make test; the tools module beyond the mapped rows.
  • 6 inventory rows with no mapped test (issue 594, PRs 551, 785, 983, 1335, 2311); PR 983 is the one Tim Weilkiens row among them.

This branch has not been deployed

No deployments
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.

1 participant