Skip to content

Fix #1295 (char/byte literal match pattern) and #1299 (named struct pattern in let) - #1312

Merged
ehartford merged 2 commits into
mainfrom
fix-1295-1299-patterns
Sep 22, 2026
Merged

ehartford merged 2 commits into
mainfrom
fix-1295-1299-patterns

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1295. Fixes #1299.

Two parser defects found by the #1150 examples rewrite, one commit each.

#1295: a char or byte literal is a match pattern

§22 makes 'X' / b'X' an integer literal value and §29 lists CHAR_LIT under LITERAL_PAT, but parse_pattern only knew TK_INT_LIT, and is_arm_token (the match-arm start predicate) did not list TK_CHAR_LIT, so the arm list ended silently before '{' => and the line fell through to the statement parser ("expected expression" at =>).

  • char_literal_value(text) is now the one decoder for 'X' / b'X' and their escapes; parse_char_literal and the pattern parser both call it (the byte-chain esc == 110 ... is gone).
  • parse_pattern treats TK_CHAR_LIT like TK_INT_LIT: bare (NK_PAT_INT) and as either endpoint of a range ('a'..='z', b'0'..=b'9').
  • is_arm_token and the for/parameter pattern-start predicates admit TK_CHAR_LIT.

#1299: a named struct pattern parses in let

let Req { name, email } = req hit "let binding requires initializer": parse_let_binding routed a destructuring let only when the pattern began with (, { or [, so an uppercase name followed by { fell into the plain-binding path. STRUCT_PAT := [ TYPE ] '{' ... '}' (§29); "all pattern forms are available in let" (§9.7).

An uppercase name followed by { now builds the pattern with parse_struct_pattern — the same NK_PAT_STRUCT node a match arm builds, so Sema's Drop gate (#1291) and MirLower see one form — and the = value [else: body] tail is shared with the anonymous forms via parse_let_pattern_rest.

Tests

Verification (iterate tier)

with check src/main.w ok; WITH=$PWD/src/main src/main build :dev green from the committed HEAD; the three new tests plus all of test/spec/spec_ss09_*, test/behavior/behav_*pattern*, behav_*match*, behav_drop_destructure_rule.w and test/compile_errors/err_drop_destructure_* pass under stage1 (34 files). The two pre_d_build_runner tests the *match* glob also catches (behav_capability_token_mismatch.w, behav_file_prefix_map_objects_match.w) need an out/stage or out/release compiler that a :dev-only worktree does not have; the former passes on the untouched main tree. Battery not run: the branch waits for the batch.

Eric Hartford added 2 commits September 22, 2026 11:12
§22 makes a char/byte literal an integer literal value and §29 lists
CHAR_LIT under LITERAL_PAT, but parse_pattern only knew TK_INT_LIT and
is_arm_token (the match-arm start predicate) did not list TK_CHAR_LIT, so
the arm list ended silently before `'{' =>` and the line fell through to
the statement parser ("expected expression" at `=>`).

- char_literal_value(text) is the one decoder for 'X' / b'X' and its
  escapes; parse_char_literal and the pattern parser both call it.
- parse_pattern treats TK_CHAR_LIT like TK_INT_LIT: bare (NK_PAT_INT) and
  as either endpoint of a range ('a'..='z', b'0'..=b'9').
- is_arm_token and the for/parameter pattern-start predicates admit
  TK_CHAR_LIT.

test/behavior/behav_match_char_literal_pattern.w covers bare, escaped,
ranged and constructor-nested (Some(b'{')) forms.
`let Req { name, email } = req` failed with "let binding requires
initializer": parse_let_binding routed a destructuring let only when
the pattern began with (, { or [, so an uppercase name followed by {
fell into the plain-binding path, which then found no `=`. §29's
STRUCT_PAT is `[ TYPE ] '{' ... '}'` and §9.7 says every pattern form
is available in let.

An uppercase name followed by `{` now builds the pattern with
parse_struct_pattern — the same NK_PAT_STRUCT node a match arm builds,
so Sema's Drop gate (#1291) and MirLower see one form — and the
`= value [else: body]` tail is shared with the anonymous forms via
parse_let_pattern_rest.

Tests: behav_let_named_struct_pattern.w (by value on a non-Drop struct,
view bindings over &req, rename, .., nested named patterns);
err_drop_destructure_let_named.w (the named form is refused on a Drop
type with the same fix-it as the anonymous form).
@ehartford
ehartford merged commit 8cbe8e6 into main Sep 22, 2026
@ehartford
ehartford deleted the fix-1295-1299-patterns branch September 22, 2026 16:25
ehartford added a commit that referenced this pull request Sep 22, 2026
…mpiler defects (#1338)

* examples: ephemerality-and-lowering stores the element directly, store(order[2], ...) (#1287 fixed in #1311)

The typed-binding detour around #1287 (Atomic intrinsics passed an element view's address as the value) is gone with its comment. Package test output unchanged on a release compiler built from ae609cd.

* examples: json-parser matches byte literals as patterns and returns &entry.value inline (#1295, #1297 fixed)

Byte arms are Some(b'{') patterns, not guards (#1295, fixed in #1312); json_get_field returns Some(&entry.value) from a for loop over the entries, and the kv_value helper is gone (#1297, fixed in #1327). Workaround comments removed. Demo and package test output unchanged on a release compiler built from ae609cd.

* examples: nebula's Status and Telemetry carry @[derive(Debug, Clone)] again (§11.8; #1288, #1289 fixed in #1311)

The parking comment is removed. Demo and package test output unchanged on a release compiler built from ae609cd.

* examples: service builds its change list and SET clause with the §7.2 builder (#1298 fixed in #1319)

describe_changes and PgUserRepo.update spell with Vec.new() as mut ...: with a trailing if; the var desugaring and its comments are gone. Demo and package test output unchanged on a release compiler built from ae609cd (neither function is reached by the demo; the same shape was run separately under --debug-alloc: correct result, zero leaks).

* examples: ecs cites D43 for DenseStorage.insert's -> Unit, not #1296

insert's tail if has an assignment arm (a value of type T) and a push arm (Unit): two meanings remain, so -> Unit is the spelling D43 requires, not a workaround. The compiler currently infers Unit without it because the assignment arm is a multi-statement block (#1337); the one-statement spellings of the same arm are rejected as D43 requires.

---------

Co-authored-by: Eric Hartford <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant