Skip to content

D103: a value converts to Option[T] where one is demanded (§4.9a) - #2224

Merged
ehartford merged 1 commit into
mainfrom
d103-value-to-option
Oct 6, 2026
Merged

ehartford merged 1 commit into
mainfrom
d103-value-to-option

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Implements §4.9a (D103, PR #2221): where a concrete Option[T] is demanded and the expression has type T, it is Some(expression) — once, at the demand, never in inference; demand propagates into arms, tails and the payload (a named fn takes extern "C" fn, a literal takes i64); an operator's operands are not demand sites.

Also: the typed MIR validator now refuses an enum payload operand that is a scalar of another width/signedness/kind than the variant's payload (it was silent over Option[i64].Some(i32)), and lib/std/regex.w drops its Some(...) ceremony.

Fixes #2217.

Draft until the battery is green.

Where a concrete `Option[T]` is demanded and the expression has type `T`,
Sema records the node in `value_to_option_nodes` once, at the demand
(`check_expr_with_expected`), and MIR builds the `Some` around the lowered
value. The node keeps its own type; the record is the conversion. `None`
and an existing Option pass unchanged; `Option[Option[T]]` from a bare `T`
is refused (once); a generic `Option[T]` demand never fires (no
inference); an operator's operands are marked in `operator_operand_nodes`
at `check_binary_operator` and never convert, so `opt == 3` stays an error.

The demand propagates into the payload: `option_demand_payload` peels a
concrete `Option[T]` to `T` for an untyped literal's context type and for
a named fn's expected callable type, so `g(twice)` takes the
`extern "C" fn` type and `let x: Option[i64] = -7` is an i64 literal.
A value Sema admitted by lossless widening (`i32` into `Option[i64]`) is
cast to the payload before the aggregate.

The typed MIR validator was silent over that aggregate (an `i32` operand
stored into an `i64` payload read back right by luck):
`mir_validate_aggregate_scalar_mismatch` now refuses a scalar payload
operand of another width, signedness or kind, with a hand-built MIR test.

`lib/std/regex.w` drops the `Some(...)` ceremony around its allocator
callbacks. Fixes #2217.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on 2fb6385 (seed-driven, with run tools/battery.w): gate rc=0 513s, uat rc=0 17s, battery-checks rc=0 851s.

@ehartford
ehartford marked this pull request as ready for review October 6, 2026 23:25
@ehartford
ehartford merged commit 086bd94 into main Oct 6, 2026
@ehartford
ehartford deleted the d103-value-to-option branch October 6, 2026 23:25
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.

Implicit Some: a value converts to Option[T] where one is expected (decide before stable)

1 participant