Repository navigation
feat: a constant's value is any expression built from constants - #173
Merged
Merged
Conversation
A class constant, an attribute argument, an enum case's value and a pattern's value take `? :`, an index, a template, `(int)`, `(float)` and `(string)`, an enum case, a plain case of an enum whose other cases carry data, a case's `value` and `name`, and a lambda that captures nothing. - The checker reports a call in a constant value with the call it runs, and says to use a static field, or `when` in a pattern. A lambda that captures a local or `this` is an error that names what it captures. - The bridge lowers a value PHP takes as a constant expression as it is, and any other value, such as a template or a data enum's plain case, to the static closure a computed parameter default already runs in. A field's initial value takes the same path. - The engine takes that closure in every constant expression of a .sharp file, not only in a parameter default. - A lambda constant reads as the function type it declares. - The "not supported yet" refusal of a data enum's case in a constant expression is gone. - docs/spec.md section 2 shows each form, the call error, and the values PHP# and plain PHP read, as examples sharp/bin/spec runs.
…constant A plain case of an enum whose other cases carry data now runs in a constant expression, so the slice's decision 12 says so instead of listing it among the refusals.
#166 kept "not supported yet" arms for the constant-value forms this branch builds. Each form now has one arm, this branch's: - is_undecided loses its arms for a case's `value` and `name` in a constant value, and for a constant typed as a function or a generic class. A constant takes any generic type, as `Class<Report>`, and its value decides what it holds. - never_written loses its call-in-a-constant arm. A call there reports "A constant's value is known before the code runs, and `…` runs code". - check_cast keeps #166's permanent refusals of `(unset)` and `(void)`, and drops its place, which nothing reads now. - The semantics rows of those arms are gone, and the spec self-test's refusal example is `break 2`, a form PHP# has not decided.
- A data enum's constant that holds its own plain case runs: the engine reads a computed constant on first fetch, so `Tier.DEFAULT` no longer overflows the stack. - A template that shows an object's `toString()` in a constant value, an enum case value, a pattern or an attribute is an error. - An index outside a constant list is a compile error that names the size, and where the size is unknown it throws OutOfRangeException as a method body does. - A value pattern or a `match` arm that names a function constant, and a lambda with parameters as a pattern value, give the `==` refusal that `is ==` gives. - A constant whose value reads back to it is a compile error that names the constants on the way. - A `match` on constants is a constant value. Any other expression in a constant value, `new` among them, runs code, and the "not supported yet" fallback is gone. An attribute argument keeps `new`. - The constant place's note, spec section 2, and the message for a call in a pattern inside a lambda an attribute argument holds.
- An index into an empty constant list, and a cycle through three constants, each get a row. - The constant-list check is one condition, with no early returns. - `++` and `--` in a constant value run code, as `x++` does, and in an attribute argument they get the attribute's error. - The constant place's note gets a row, for `this` in a constant value.
A constant that reads `Code.A.value` while `case A`'s value reads that constant compiled, and never gets a value when it runs. Reading a backed case reads its value, so the cycle is the same compile error that names the reads on the way, as "`Code.A` refers back to itself through `Keys.KEY`." - EnumCaseMetadata::read_constants records the constants and cases a backed case's value reads, from the walker read_constants uses. - The cycle check follows a case's reads as it follows a constant's, and reports a case whose value reads back to it.
heyJordanParker
added a commit
that referenced
this pull request
Oct 18, 2026
#173 makes a constant's value any expression built from constants, and reports one that runs code with report_call_in_constant. This branch reports every form outside the grammar as a typo. The conflicts in the checker resolve to both: - check_cast and check_conditional take #173's signatures, with this branch's typo error for every cast but `(int)`, `(float)` and `(string)`, and for `?:`. - report_unsupported keeps unexpected_part, then reports any other node in a constant value through report_call_in_constant. That function now takes a span, so `this` in a constant value, a constant access node, says it runs code instead of failing as a typo. - broken_rule drops both sides' arms: #173's call and property read in a constant, and this branch's PHP forms. - an_undecided_form_is_not_supported_yet keeps the intersection type, the one form left there.
heyJordanParker
added a commit
that referenced
this pull request
Oct 18, 2026
#173 changed the `refusal` example to `while (true) { break 2; }`, but `break 2` now fails as a typo does, so the line no longer shows a "not supported yet" refusal. An intersection type is still not supported yet, so `public void pair(Order & Line both) { }` shows it.
heyJordanParker
added a commit
that referenced
this pull request
Oct 18, 2026
…bers
is_undecided keeps the branch's one interface arm, a property in another form
than { get; }, { get; set; } and => expr;, and drops the constant arms #173
retired.
heyJordanParker
added a commit
that referenced
this pull request
Oct 18, 2026
Priced's constants read an index, a ? :, a match, a template with a cast, a case's name and a lambda, as a class constant's do since #173, in every mode and with preloading.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A PHP# constant's value is now any expression built from constants. The same rule holds in every place PHP# takes a constant expression: a class constant, an attribute argument, an enum case's value and a pattern's value. A parameter default stays any expression, as spec section 7 says.
The forms
RATEreports: "A constant's value is known before the code runs, andRates.current()runs code: use a static field." In a pattern, the message ends "compare with it inwhen." A lambda that captures a local orthisreports "The lambda capturesrate, and a lambda in a constant value captures nothing."How it runs
The bridge keeps a value PHP takes as a constant expression as it is. Any other value, such as a template or a data enum's plain case, runs in the static closure a computed parameter default already runs in,
(static function () { return …; })(). The engine now takes that closure in every constant expression of a.sharpfile. A field's initial value takes the same path, soprivate Tier tier = Tier.Free;runs too.Plain PHP reads the same values.
Billing::PLANSis['free', 'pro'],Billing::DEFAULT_PLAN === Plan::Free, andBilling::DEFAULT_TIER === Tier\Free::instance().Where a place cannot take a form
intorstring.==before a ternary, a cast, a template orPlan.Free.value, since a pattern reads a bare value at comparison precedence.Billing.PLANS[0]andTier.Freeneed none.newof constants…".The merge with #166
#166 kept "not supported yet" arms for these forms. The merge deletes each one and its rows, so every form has one arm: a case's
valueandnamein a constant, a constant typed as a function or a generic class, and the call-in-a-constant message.(unset)and(void)keep #166's permanent refusals. The spec self-test's refusal example is nowbreak 2, since the constant ternary it used compiles.Tests
Zend/tests/sharp/constant_values.phptandconstant_values_preload.phptrun every value, the plain PHP reads, each place's forms and the field initial values, in every modesharp/bin/test --opcache --repeat --passesruns. The preload test reads every value during preload, the lambda among them, and again in each request. On master every form fails to compile, each as "not supported yet" or "non-constant expression".docs/spec.mdsection 2 shows the rule as three examplessharp/bin/specruns: 238 checked, 0 fail.No harness file changed.