Skip to content

feat: accept declare(strict_types=1) and declare directives#459

Open
chadmandoo wants to merge 2 commits into
illegalstudio:mainfrom
chadmandoo:upstream-pr/declare-strict-types
Open

feat: accept declare(strict_types=1) and declare directives#459
chadmandoo wants to merge 2 commits into
illegalstudio:mainfrom
chadmandoo:upstream-pr/declare-strict-types

Conversation

@chadmandoo

Copy link
Copy Markdown
Contributor

What

Accepts declare(strict_types=1); (and declare(...) directives generally) as a top-of-file statement instead of erroring. Every strict-typed PHP file opens with it, so this unblocks compiling any codebase that uses declare(strict_types=1).

How

Parses the declare(directive=value) form as a directive statement rather than treating directive=value as an assignment expression (which produced "Invalid assignment target"). strict_types is accepted and does not alter runtime behavior in the compiled output (the compiler is already strict), so byte-parity with PHP is preserved.

Verification

Byte-parity vs PHP 8.5 for files opening with declare(strict_types=1); + a regression test.

The `declare` keyword was unrecognized, so `declare(strict_types=1);` — the
first line of most modern PHP files — failed to parse ("Invalid assignment
target"), which blocks compiling essentially any strict-typed codebase.

Lex `declare` as a new token and parse `declare(name=value, ...)` in both the
statement form (`declare(strict_types=1);`) and the block form
(`declare(ticks=1) { ... }`). elephc compiles an always-strict subset, so the
directives are parsed for syntactic validity and discarded: the statement form
lowers to an empty `Synthetic` block (a no-op) and the block form to a
`Synthetic` wrapper around its body. Reusing the existing `Synthetic` node means
no analysis or lowering pass needs to change.

Adds lexer, parser, codegen, and error tests, plus a docs section in
docs/php/control-structures.md.

(cherry picked from commit 88e78ca)
(cherry picked from commit f256454)
@nahime0

nahime0 commented Jul 7, 2026

Copy link
Copy Markdown
Member

This will be discussed in the next core team meeting. It touches an important area about static / dynamic behaviour
@Guikingone

@nahime0 nahime0 self-requested a review July 7, 2026 12:43
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.

2 participants