Skip to content

feat: a constant's value is any expression built from constants - #173

Merged
heyJordanParker merged 6 commits into
masterfrom
feat/constant-values
Oct 18, 2026
Merged

heyJordanParker merged 6 commits into
masterfrom
feat/constant-values

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

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

public const int GRACE_DAYS = Billing.TRIAL_DAYS > 7 ? 3 : 1;          // 3
public const string FIRST_PLAN = Billing.PLANS[0];                     // "free"
public const string TRIAL_LABEL = `Trial: ${Billing.TRIAL_DAYS} days`; // "Trial: 14 days"
public const int PRICE_CENTS = (int)(9.99 * 100);                      // 999
public const Plan DEFAULT_PLAN = Plan.Free;                            // the case
public const Tier DEFAULT_TIER = Tier.Free;                            // a plain case of an enum whose other cases carry data
public const string DEFAULT_KEY = Plan.Free.value;                     // "free"
public const Function<int(int)> DOUBLE = (int x) => x * 2;             // (Billing.DOUBLE)(4) is 8
public const Class<Plan> PLAN_KIND = typeof(Plan);                     // a constant takes any generic type
public const int RATE = Rates.current();                               // compile error
private static int rate = Rates.current();                             // compiles

RATE reports: "A constant's value is known before the code runs, and Rates.current() runs code: use a static field." In a pattern, the message ends "compare with it in when." A lambda that captures a local or this reports "The lambda captures rate, 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 .sharp file. A field's initial value takes the same path, so private Tier tier = Tier.Free; runs too.

Plain PHP reads the same values. Billing::PLANS is ['free', 'pro'], Billing::DEFAULT_PLAN === Plan::Free, and Billing::DEFAULT_TIER === Tier\Free::instance().

Where a place cannot take a form

  • An enum case's value takes no case object and no lambda, because its backing type is int or string.
  • A pattern's value needs == before a ternary, a cast, a template or Plan.Free.value, since a pattern reads a bare value at comparison precedence. Billing.PLANS[0] and Tier.Free need none.
  • A lambda as a pattern's value compiles but never matches, since each lambda is its own object.
  • An attribute argument keeps its own error for a call: "An attribute argument is a constant or a new of 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 value and name in 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 now break 2, since the constant ternary it used compiles.

Tests

Zend/tests/sharp/constant_values.phpt and constant_values_preload.phpt run every value, the plain PHP reads, each place's forms and the field initial values, in every mode sharp/bin/test --opcache --repeat --passes runs. 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.md section 2 shows the rule as three examples sharp/bin/spec runs: 238 checked, 0 fail.

No harness file changed.

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
heyJordanParker merged commit a70fc63 into master Oct 18, 2026
24 checks passed
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.
@heyJordanParker
heyJordanParker deleted the feat/constant-values branch November 3, 2026 22:08
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