Skip to content

feat: reject variables declared with a PROGRAM type (E142)#1802

Draft
volsa wants to merge 1 commit into
masterfrom
vosa/program-instance-validation
Draft

feat: reject variables declared with a PROGRAM type (E142)#1802
volsa wants to merge 1 commit into
masterfrom
vosa/program-instance-validation

Conversation

@volsa

@volsa volsa commented Jul 10, 2026

Copy link
Copy Markdown
Member

What

Declaring a variable whose type is a PROGRAM is now a validation error (E142):

FUNCTION main : DINT
VAR
    myInstance : myProg;   // error[E142]
END_VAR
END_FUNCTION
error[E142]: Program `myProg` cannot be used as a variable type; programs are singletons — call `myProg` directly instead

Why

Programs are singletons — the compiler allocates exactly one global instance per program, and calling a program by name operates on that instance. A program-typed variable silently created a detached copy of the singleton's state: calls through it mutated the copy while the real program instance never advanced. IEC 61131-3 only allows program instantiation inside CONFIGURATION/RESOURCE, which are not supported.

Notes

  • Covers all variable blocks (VAR, VAR_INPUT, VAR_OUTPUT, VAR_IN_OUT, VAR_TEMP, VAR_GLOBAL), struct members, and arrays of a program type.
  • Direct calls (myProg(in1 := 5);) and member access (x := myProg.out1;) on the program name keep working — both are pinned with tests.
  • New error code E142 with documentation (plc explain E142).
  • No codegen changes.

🤖 Generated with Claude Code

Programs are singletons: the compiler allocates exactly one global
instance per program, and calls by name operate on that instance.
Declaring a variable of a program type created a detached copy of the
singleton's state, which calls would mutate while the real instance
never advances. IEC 61131-3 only allows program instantiation inside
CONFIGURATION/RESOURCE declarations, which are not supported.

Variable declarations (all VAR blocks, VAR_GLOBAL, struct members, and
arrays) whose effective type resolves to a PROGRAM POU now report E142.
Direct calls (`myProg(...)`) and member access (`myProg.out1`) on the
program name keep working.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@volsa volsa marked this pull request as draft July 10, 2026 10:24
@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown

Build Artifacts

🐧 Linux

Artifact Link Size
deb-x86_64 Download 38.4 MB
schema Download 0.0 MB
stdlib Download 32.3 MB
plc-x86_64 Download 43.5 MB
deb-aarch64 Download 30.8 MB
plc-aarch64 Download 43.3 MB

From workflow run

🪟 Windows

Artifact Link Size
stdlib.lib Download 4.0 MB
stdlib.dll Download 0.1 MB
plc.exe Download 38.3 MB

From workflow run

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