Skip to content

D71 language rulings: @[flags] without repr, from_int on unit enums, closure -> T, :? coverage and rows - #1787

Merged
ehartford merged 5 commits into
mainfrom
batch2-rulings-lang
Sep 27, 2026
Merged

ehartford merged 5 commits into
mainfrom
batch2-rulings-lang

Conversation

@ehartford

@ehartford ehartford commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Stack layer 1 of 6 (stack #1793). Implements D71 #1482 #1497 #1508 #1564 (+ the #1776 :? rows). #1587 (str slices) is blocked on &str becoming the {ptr,len} value the spec defines (its own campaign). Each change has a test shown failing before. Draft until the stack's one battery is green.

Eric Hartford added 5 commits September 27, 2026 06:41
An @[flags] enum with no representation type doubles in its default
integer representation; the attribute is never ignored. The parser set
the flags bit only beside an explicit repr, so a backing-less @[flags]
enum counted 0 1 2.

parse_enum_named_decl now sets TDK_FLAG_FLAGS on every discriminant
enum, and a backing-less @[flags] enum is one in the inferred i32:
the block form with payloads (tags 1 2 4, as `enum E: i32:` would be)
and the braced form, which never inferred a representation. One helper,
add_backingless_enum_extras, writes both extras formats.

A generic enum with a payload variant cannot be a discriminant enum yet
(#1768: its payload type reaches MIR unresolved), so @[flags] on one is
refused by name instead of ignored. Found on the way: an explicit
`= N` on a backing-less payload enum is still dropped (#1769).

Fixes #1482
`from_int` exists only on an enum whose variants are all unit variants;
no value of the enum exists for a payload variant's discriminant alone,
so on an enum with a payload variant a `from_int` call is a compile
error naming that variant. On a discriminant enum with payloads it
compiled and returned the repr's Option (`Msg.from_int(7)` was
`Some(7)`); on any other payload enum it was "unknown method".

check_method_call_parts's from_int branch asks enum_first_payload_variant
(new, next to enum_has_variant) first and reports the variant; the
repr's-Option branch is gone, so from_int is always Option[Type]. A
`from_int` the program declares on such an enum is its own static
method and still resolves.

Fixes #1497
A closure may state its result type as a function does, `(x: i32) -> str
=> f"{x}"`; the annotation is checked like a declared return type and is
never dropped. Parser.parse_fat_arrow_paren_closure and parse_closure
parsed the `-> T` and discarded it, so `(x: i32) -> str => x` compiled
and returned the i32, and `(x: i32) -> i64 => x` returned an i32.

The parser keeps the type node in an AstPool side table
(set_closure_ret_type / closure_ret_type, copied by astpool_clone_deep
and the comptime tree clone like the other closure marks); Resolve walks
it and render prints it. Sema.check_closure checks the body against it
and makes it the closure's result type, ahead of the partial-application
and context results.

A context expecting a closure that returns another type is an error at
the annotation. The check is the closure's own (Sema.types_identical, an
exact structural comparison): types_compatible takes any fn type for any
other (#1772), so the closure would otherwise reach the context returning
the wrong representation.

Fixes #1508
§15.4.7 now reads: `:?` is available for the table's types, their
compositions and any `impl Debug`; a value with no Debug form (a
function value, a closure, `dyn Trait`, a range, a `va_list`) is a
compile error under `:?` naming its type, never a placeholder.

D61's implementation already refuses each of those by name, at every
depth; the new tests pin the closure, `dyn Trait`, range, `va_list`
and enum-payload cases beside the existing fn-value test. One case
disagreed: a HashMap or BTreeMap whose key or value has no form was
reported from the std formatter's own `{v:?}`, at
<embedded-std>/std/collections.w, a line the program never wrote.
ensure_debug_formatter now asks debug_fmt_has_form of K and V before
binding the std `debug_form` and reports the one without a form at the
`:?` that reached the map.

Refs #1564 (the raw-pointer / Box / Rc Debug rows wait on Eric)
Eric's rows for §15.4.7 (docs PR #1776):
- `HashSet[T]` prints `{elem, elem}`, elements ordered by their Debug
  text. It printed its generated struct form, `HashSet { ptr: 4339450384 }`.
- `Box[T]`, `Rc[T]`, `Arc[T]` print the value they hold with `:?`. Box
  already did; Rc and Arc printed `Rc { ptr: ... }`.
- A raw pointer prints its address in hexadecimal. It printed decimal.

Sema: debug_fmt_transparent_owner (Box, or std's Rc/Arc by
type_symbol_is_std_rc_owner) replaces the Box-only checks in
ensure_debug_formatter, debug_fmt_has_form and debug_fmt_components, and
binds each one's as_ref through the new debug_fmt_bind_aux. A HashSet
is a synthesized formatter whose component is T and whose aux method is
std's new HashSet.debug_form_of; Sema ensures the element view and
Vec[str] types MIR needs.

MirLower.lower_debug_form reads the transparent owner from Sema, and
the new lower_debug_set walks the set's table as a map's traversal does
(MAP_CAPACITY / MAP_SLOT_OCCUPIED / MAP_KEY_AT, each element viewed in
place), pushes each element's `:?` text onto a Vec[str] and calls
debug_form_of (lower_debug_borrowing_call2), which orders them with the
HashMap form's debug_entry_order. The set has no traversal of its own:
`for x in set` is unspecified and fails in MIR lowering today.

Codegen.gen_debug_format and emit-C's debug_format_expr format a raw
pointer as `{:#x}` through with_fmt_int_spec. emit-C still refuses a
HashSet's form loudly, as it refuses a HashMap traversal.

Fixes #1564
@ehartford
ehartford added this pull request to stack #1793 September 27, 2026 11:01
@ehartford ehartford changed the title batch2 rulings lang D71 language rulings: @[flags] without repr, from_int on unit enums, closure -> T, :? coverage and rows Sep 27, 2026
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on the stack top 57410c94 (docs-csharplang-shape, folds #1787→#1792): with run tools/battery.w — src/main build ok, :battery-checks (seed-driver, fixpoint, test-with-audits incl. drop/move audits, user-programs-safe, last-green) rc=0 in 1574 s. Pre-battery gate green on the same tip: full build, stage1 self-check of src/main.w, build.w with the fresh binary, abi-hash, unit-return-review, spec-inventory, examples-tests, c-migrator-basic-tests, deep-debug-tool-tests. Merge order: gh stack merge 1792 --yes --squash.

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