Skip to content

comptime: first(), last() and rest() of a fixed array evaluate, and ?? over an Option (#2386) - #2652

Merged
ehartford merged 1 commit into
fix-2403from
fix-2386
Oct 25, 2026
Merged

ehartford merged 1 commit into
fix-2403from
fix-2386

Conversation

@ehartford

@ehartford ehartford commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Root cause: ComptimeEval's method dispatch (src/ComptimeEval.w, the receiver
switch before "method '…' is not comptime-evaluable yet") had no CV_ARRAY
case. D117's array/slice first/last/rest are compiler-declared, with no std
body for the evaluator to run, so comptime A.first() was refused. The
issue's own spelling, A.first() ?? &-1, also met the evaluator's binary
operator with no OP_DEFAULT case ("operator requires integer comptime
values").

eval_array_view_method_call: first/last give Some(the element's own arena
slot) or None; rest is the tail's (start + 1, count - 1) view. A const is
evaluated before Sema types its expressions, so the Option[&T] comes from
the element's own type when the call node has none, and an untyped array's
tail is typed from its element, so an empty tail still knows what first()
yields. eval_binary evaluates opt ?? fallback lazily: the payload of a
Some, else the fallback.

Test: test/behavior/behav_2386_comptime_array_first_last_rest.w
(expect "1 3 2 3 -1"). The installed compiler refuses first.

Closes #2386

…??` over an Option (#2386)

Root cause: ComptimeEval's method dispatch (src/ComptimeEval.w, the receiver
switch before "method '…' is not comptime-evaluable yet") had no CV_ARRAY
case. D117's array/slice first/last/rest are compiler-declared, with no std
body for the evaluator to run, so `comptime A.first()` was refused. The
issue's own spelling, `A.first() ?? &-1`, also met the evaluator's binary
operator with no OP_DEFAULT case ("operator requires integer comptime
values").

eval_array_view_method_call: first/last give Some(the element's own arena
slot) or None; rest is the tail's (start + 1, count - 1) view. A const is
evaluated before Sema types its expressions, so the Option[&T] comes from
the element's own type when the call node has none, and an untyped array's
tail is typed from its element, so an empty tail still knows what first()
yields. eval_binary evaluates `opt ?? fallback` lazily: the payload of a
Some, else the fallback.

Test: test/behavior/behav_2386_comptime_array_first_last_rest.w
(expect "1 3 2 3 -1"). The installed compiler refuses `first`.
@ehartford
ehartford added this pull request to stack #2658 October 25, 2026 21:47
@ehartford ehartford changed the title fix 2386 comptime: first(), last() and rest() of a fixed array evaluate, and ?? over an Option (#2386) Oct 25, 2026
@ehartford
ehartford marked this pull request as ready for review October 25, 2026 21:59
@ehartford
ehartford merged commit 73f19a7 into main Oct 25, 2026
@ehartford
ehartford deleted the fix-2386 branch October 25, 2026 21:59
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.

comptime: first()/last()/rest() on fixed arrays and slices are not comptime-evaluable

1 participant