Skip to content

fix(parser): admit import and alias in while, loop, for and if bodies - #2670

Open
HuiJun wants to merge 3 commits into
developfrom
fix/action-body-import-alias
Open

HuiJun wants to merge 3 commits into
developfrom
fix/action-body-import-alias

Conversation

@HuiJun

@HuiJun HuiJun commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

A while, loop, for, if or else body rejected members that a plain action body accepts, and an action stopped when it ran a body holding an import or an alias. Closes #2668.

package Q { part def T; }
package P {
    action def A {
        attribute n : ScalarValues::Integer;
        while (n < 3) { private import Q::*; part v : T; }
    }
}

Before, sysml -validate on this model:

m8.sysml:5:25: error[syntax]: unknown action keyword: private
m8.sysml:5:33: error[syntax]: unknown action keyword: import
m8.sysml:5:40: error[syntax]: expected a body member

After, no syntax error is reported.

Root cause: these bodies read their statements through parseActionMember, whose keyword dispatch has no arm for a visibility prefix, import or alias, so each fell to the unknown action keyword default. The while body sent every member there; the loop, for and if bodies only sent what atDefUsageStart does not recognize, which is why they failed on an unprefixed alias only. parseActionMember now hands a visibility prefix, an import and an alias to parseBodyMember, next to where it already does so for the members only some body kind owns. One change covers all five bodies and leaves every existing golden AST unchanged.

Member action while loop for if / else
private import Q::*; / public import Q::*; ok was rejected ok ok ok
private alias TT for Q::T; ok was rejected ok ok ok
alias TT for Q::T; ok was rejected was rejected was rejected was rejected
import Q::*; import-visibility was unknown action keyword, now import-visibility same same same

Every cell is accepted now, the last row with its import-visibility diagnostic only.

Running such a body

Once these members parse, a body with no flow of its own lowered each of them as a step, so running the action stopped at the import or alias:

execute action: statement not executable: action node : `then` statement in a body is not executable

lowerBlock (internal/ir/lower/action_graph.go) now skips the members that state no step (statesNoStep), as a body with a flow already does. while w < 2 { private import Q::*; alias W for Q::T; assign w := w + 1; } runs to w = 2.

Out of scope

  • The model above now parses, but T is still reported unresolved in the while body: an import in a loop or branch body does not yet bring names into scope. That is semantic scoping work, not parsing, and is left to fix(semantics): reflect action node parameters #2617 and its follow-ups.
  • private assign n := n + 1; (a visibility-prefixed action node) is rejected with expected a body member in every action body, a plain one included. That was so before this change and is not touched here.

Specification basis

SysML.xtext: the body of a WhileLoopNode, ForLoopNode and IfNode is an ActionBodyParameter, whose body is ActionBodyItem*. ActionBodyItem includes NonBehaviorBodyItem, which includes Import and AliasMember. No compliance row moves.

How it was verified

  • Failing first: go test ./internal/syntax/parser -run TestLoopAndBranchBodiesAdmitImportAndAlias failed 13 of its 30 subtests before the fix (the cells marked above) and passes all 30 after it.
  • New golden AST tests/parser/testdata/parse/action_loop_branch_import_alias.sysml: 5 imports and 6 aliases across the five bodies, no ErrorNode. go test -run TestGolden -update ./tests/parser changed no existing golden.
  • On this head (after merging develop): go build ./... and go vet ./... in the root and tools modules, gofmt -l . (empty), make lint, make docs-counts, python3 scripts/changelog.py check, python3 scripts/check-doc-ids.py, go test -race ./internal/syntax/parser ./tests/parser, and go test ./internal/syntax/... ./tests/parser ./tests/hygiene ./internal/workspace/... ./internal/frontend/lsp/... ./client/opensysml/... (includes TestStdlibConformance and TestNegative).
  • Full go test -timeout 55m ./... passed (141 packages) on the commit before the develop merge; the merge brought in only client, LSP and workspace-model changes, whose packages were re-run above.
  • Failing first for the run: the new execution conformance case internal/exec/runtime/testdata/conformance/action_loop_branch_import_alias.sysml (an import and an alias in a while, loop, for, if and else body) failed with the error above before the lowering change and passes with it. go test -count=1 ./internal/ir/... ./internal/exec/runtime passes on this head, with go build ./..., go vet over both trees, gofmt -l, make lint, make docs-counts, python3 scripts/changelog.py check and go test ./tests/hygiene. The full suite was not re-run after this commit.
  • Not run locally: the full suite under -race (make test), and go test -C tools ./... beyond the make docs-counts packages.

Try it:

go test -count=1 -run 'TestLoopAndBranchBodiesAdmitImportAndAlias|TestGolden' ./internal/syntax/parser ./tests/parser
go test -count=1 -run 'TestExecutionConformance/action_loop_branch_import_alias' ./internal/exec/runtime

Checklist

  • make test and make lint pass locally (make lint passes; go test ./... passed without -race, see above)
  • Tests added or updated for the change
  • Documentation extended where it already covers the surface (none covers it)
  • 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 (compliance rows need nothing: the census is counted at docs build)
  • No internal work-item labels (waves, slices, F4, K5) in the body, docs, or changelog

Software Factory opened this PR for Jason
Implemented in session Fix import and alias parsing in action bodies
Review with Devin · Open in Devin Desktop


devin-ai-integration Bot and others added 2 commits November 26, 2026 05:56
…oop and branch bodies

A while, loop, for, if or else body reads its statements through
parseActionMember, whose keyword dispatch reported an import, an alias
or a visibility prefix as an unknown action keyword. These open a
NonBehaviorBodyItem, so parseActionMember now hands them to the body
member parser, as it does for the members only some body kind owns.

Refs #2668

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

A loop or branch body with no flow of its own lowered each member as a
step, so an import or an alias became an unsupported statement and the
run stopped with 'statement not executable'. Such a body now skips the
members that state no step, as a body with a flow already does.

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

This comment was marked as resolved.

@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 found 1 finding. Devin automatically fixed it.

Fixed by Devin (1)
  • Imports and aliases abort action runs — Confirmed: while w < 2 { private import Q::*; assign w := w + 1; } aborted with statement not executable. lowerBlock now skips members that state no step, as a body with a flow already does; a new execution conformance fixture fails without the fix. The commit is local and is pushed once the CI coordinator releases a slot.

View findings in Devin Review · Follow Devin's work

Devin Review

@devin-ai-integration

Copy link
Copy Markdown
Contributor

QA: PASS on 0ef51b212a (base develop at 83249b3fa2), two gaps noted

The head moved during the run (316bfcb80b → 0ef51b212a); everything below was rerun on 0ef51b212a.

QA run: CLI, VS Code and REPL on base and head

What passed

Surface Base Head
sysml -validate on import / alias in while, loop, for, if, else bodies unknown action keyword: private/import/alias no errors
-convert sysml and -convert api-json n/a (syntax errors) all imports and aliases written back; the converted file validates; alias Membership present in the JSON
Alias name resolution (alias X for Q::T; part v : X; in each body kind) syntax error resolves; hover on the usage gives part def T, go-to-definition reaches Q::T, rename of the alias edits both sites
VS Code + sysml-lsp 16 problems 0 problems; hover, F12, typing a new alias in a for body stays clean; malformed alias for; still reports expected a name
gRPC ParseSources 16 syntax diagnostics 0 diagnostics
Execution (%action test::run on the new conformance fixture) n/a completes with w=2, l=3, f=6, taken=1, skipped=3
PR's TestLoopAndBranchBodiesAdmitImportAndAlias 13 of 30 subtests fail 30 of 30 pass

Nothing else in parsing moved.

  • 297 generated models (every body kind × valid and malformed import/alias/visibility members, nested bodies, ordinary action bodies), each through -validate, -strict -validate, -convert sysml, -convert api-json: the only base/head differences are the intended ones (the members are admitted; malformed ones get the body-member parser's diagnostics instead of unknown action keyword).
  • All 4,496 .sysml / .kerml files in the tree through -validate and -convert sysml on base and head: byte-identical output (one line differs only by the checkout path).
  • TestStdlibConformance, ./internal/syntax/..., ./tests/parser (golden and negative), ./tests/resolve, ./internal/ir/lower, TestExecutionConformance|TestExecutionTrace|TestRuntimeRobustness, TestGRPCConformance: pass. go build ./..., go vet and gofmt -l on the touched packages: clean.

Past fixes in the touched areas (77 rows: parser/lexer/formatter, action bodies, import, alias; 15 of them from Tim Weilkiens's reports):

Hosted CI on 0ef51b212a: 20 passed, 0 failed, 0 pending, 13 skipped (the client, editor-plugin, PDF and derived-property jobs, skipped by the changed-areas filter).

Gaps

  1. An import in a loop or branch body brings no names into scope. private import Q::*; part v : T; inside a while, loop, for, if or else body reports unresolved reference: T on head, while the same two members in an ordinary action body (action s { … }) resolve. The PR description declares this out of scope, and for loop / for / if / else base behaves the same, so it is not a regression; but the imports this PR admits are not usable yet.

    Unqualified T unresolved after a body-local import

  2. -query does not see members of a loop or branch body. oslc.where=rdf:type="Membership" returns the alias of an ordinary action body but no elements matched for the same alias in a while / loop / for / if / else body. Same on base for other members (a part in a while body is not returned for PartUsage either), so it predates this PR.

For the record, the first head (316bfcb80b) parsed these bodies but failed to execute them (statement not executable: action node : \then` statement in a body is not executable); 0ef51b2` fixes that.

316bfcb80b: execution fails 0ef51b212a: expected values
Execution failure on the earlier head Final values on the current head

Not run

  • The full go test ./... and -race tiers locally (hosted CI covers them); only the suites listed above.
  • The VS Code extension's own test suite, and the Emacs, JupyterLab, SysON and Magic plugins.
  • LSP folding ranges (sysml-lsp answers not implemented on base and head).
  • No alias-specific completion was observed inside the bodies on either side; completion offers Q and n on both.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

QA: pass — 0ef51b2 against develop 83249b3 (race re-runs of four packages and the tools module still running, result to follow)

Exact head 0ef51b212a5f0e9b4b94b4160066100f4bfd0d12 (non-force descendant of 316bfcb, contains 83249b3), in a detached worktree; a second worktree at 83249b3fa2 for comparison. Nothing pushed.

Gates on the head

Check Result
go build ./..., go vet ./... (root and tools/), gofmt -l . clean, empty
go test -run TestStdlibConformance ./internal/workspace/libs ok
go test -run TestGolden ./tests/parser (no -update) ok
go test -run TestNegative ./tests/parser ./internal/syntax/parser ok
TestLoopAndBranchBodiesAdmitImportAndAlias, TestExecutionConformance/action_loop_branch_import_alias (also under policies) ok
make test (-race, training/pilot/spec-example corpora downloaded, OPENSYSML_REQUIRE_TRAINING_CORPUS/PILOT_CORPORA/SPEC_EXAMPLES=1) 136 packages ok, 5 not ok — all environmental, see below

make test packages that did not pass in the single all-packages run on an 8-core machine, none with a failing assertion:

  • internal/exec/runtime, tests/corpus, tests/export: panic: test timed out after 45m0s under full parallel load. internal/exec/runtime re-run alone under -race: ok, 786s. tests/corpus, tests/export: re-run alone in progress.
  • tests/codegen: signal: killed (the process was killed, no test failed). Re-run alone in progress.
  • tests/identity: TestPilotLibraryXMI — the machine sets OPENSYSML_REQUIRE_PILOT_LIBRARY_XMI=1 and the XMI had not been fetched. After ./scripts/download-pilot-library-xmi.sh: ok under -race.
  • go test -C tools -race ./... (second half of make test) did not start because the first half exited nonzero; running now.

Failing-first

  • Only the parser test file and the golden pair applied to 83249b3: TestLoopAndBranchBodiesAdmitImportAndAlias fails in the while/loop/for/if/else subtests, TestGolden fails with unknown action keyword: private|import|alias. Pass on the head.
  • Only the conformance fixture pair applied to 83249b3: TestExecutionConformance/action_loop_branch_import_alias fails with the undeclared syntax diagnostics. With the parser fix but action_graph.go as of 316bfcb: fails with ExecuteAction failed: ... statement not executable: action node : then statement in a body is not executable. Passes on the head.

Behavior, base vs head

Parsing (sysml -validate, plus ast.Dump / format.Source driven from a scratch program — the CLI has no -dump-ast and its -format is the report format):

  • 1,326 generated cases: private|public|protected import, import Q::T;, alias X for Q::T;, private alias, visibility-prefixed part/attribute/item/action members, nested bodies, in while, loop … until, for, if, else, else if, and plain action, state and calc bodies; plus malformed variants (private alone, import with no name, alias without for, at end of body and at EOF). No panic on either side. On the head every admitted member in a loop or branch body gives the same diagnostics as in a plain action body (unprefixed import keeps error[import-visibility]); malformed ones give expected a name, expected 'for' in alias, expected a body member.
  • Nothing extra admitted: what a plain action body rejects is still rejected in a loop or branch body (e.g. private assign m := m + 1; → expected a body member).
  • Plain action, state, calc and entry action bodies: AST and diagnostics identical on base and head.
  • Formatter: output of the fixture is idempotent and re-parses to the same AST; -convert sysml prints every admitted member back with its visibility. The committed .golden equals ast.Dump of the fixture byte for byte.
  • On the base, private import was already read in loop/for/if bodies; alias in all of them and import/visibility + import in while were the rejected ones. The head makes the five bodies uniform.

Execution (REPL %action test::run + %continue, and the conformance runner):

Body holding private import Q::*; alias X for Q::T; + assignments base head
while, loop … until, for, if, else, else if, nested while/if/for syntax errors, then statement not executable completes, counters as expected (e.g. committed fixture w=2 l=3 f=6 taken=1 skipped=3; nested n=2 m=6)
same with unprefixed import Q::T; syntax errors error[import-visibility] reported, run completes
private attribute pv : Integer = 5; in a while body, read by a later assignment n=2 m=10 n=2 m=10
private action sub {…} in a while body n=2 m=2 n=2 m=2
plain action body with private import, alias, private attribute, protected part completes, n=1 m=10 pv=5 identical

So on the head the import and alias are no-ops at run time and the other statements run normally; the plain body is unchanged.

Resolution (unchanged, not this PR's)

Grammar basis

ActionBodyItem lists Import and AliasMember first (SysML.xtext at the pin, inlined; the spec BNF routes them through NonBehaviorBodyItem), and ActionBodyParameter/loop and if bodies are '{' ActionBodyItem* '}', so admitting exactly these in loop and branch bodies matches. statesNoStep already listed *ast.Import/*ast.Alias; the head applies it in lowerBlock.

Observations (not failures of this PR)

  1. Pre-existing on 83249b3 too: a part or item usage in a loop or branch body, with or without a visibility prefix, aborts the run — while n < 2 { part pp : Q::P; assign n := n + 1; } → statement not executable: action node : part usage "pp" in a body is not executable. The same member in a plain action body runs.
  2. Nit: the new comment in behavior.go cites NonBehaviorBodyItem (SysML.xtext); that production name is in the specification's BNF, not in the pinned SysML.xtext, which inlines it into ActionBodyItem.

Not covered

LSP, gRPC clients and Jupyter surfaces were exercised only through the suite, not by hand. Race results for tests/codegen, tests/corpus, tests/export and the tools module follow in a second comment.

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.

Parser rejects import and alias in a while loop body, and an unprefixed alias in loop, for and if bodies

1 participant