You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Switch-default d2==0 means abort() in C but bb0 in LLVM (unenforced convention) #984
For TK_SWITCH_INT, the C backend treats default d2 == 0 as 'no default → abort()' (src/CCodegen.w:8170,8185) while the LLVM backend treats d2 as a plain BB index with if d2 >= 0 (src/CodegenDispatch.w:15538), i.e. d2==0 is a real default-bb0. All current producers pass nonzero defaults so the backends agree today — but nothing enforces the convention, and one default=bb0 lowering silently diverges the backends (C aborts where LLVM branches).
Repair boundary
File with CCGEN-001's fix or separately: a single documented MIR contract for the switch default (sentinel vs index) enforced at construction or validated by both backends.
Review: reproduced (latent, as the report says; established from source)
Verdict. The two backends read the TK_SWITCH_INT default payload d2 under different conventions, and nothing in MIR construction, the MIR printer, or the validators pins either one. That is exactly the report's claim. It is latent today: every switchInt producer in MirLower.w passes a freshly allocated block as the default, bb0 is the entry block and is never re-targeted, and a dump of several test programs shows no otherwise: bb0. So there is no runtime divergence to run; the defect is the missing contract, which direct source evidence establishes. A third reading I found while checking: for a negative d2 the LLVM backend branches to its unreachable block while the C backend would emit goto bb-1, so the sentinel space disagrees in both directions.
with build of 5809ac1 in a detached worktree, self-reports with v0.15.2.1 (used only for --dump-mir)
platform
macOS Darwin 25.6.0, arm64
Evidence
C backend, src/CCodegen.w:8157-8180emit_switch_term: if d2 != 0: goto bb{d2} else: abort(), in both the single-target and the table path. Zero is a sentinel meaning "no default".
LLVM backend, src/CodegenDispatch.w:15403-15408: default_bb = mir_default_unreachable_bb_value(); if d2 >= 0: default_bb = mir_bb_values[d2]. Zero is bb0; negative is the sentinel.
MIR printer, src/Mir.w:172-203: prints otherwise: bb{d2} unconditionally, so a dump would render the C sentinel as a real branch to bb0.
Producers: all 12 terminate(TermKind.TK_SWITCH_INT, ..., <block>, 0) sites in src/MirLower.w (lines 4523 to 6992) pass a real block (else_bb, exit_bb, fail_bb, skip_bb, rhs_bb, end_bb), never a sentinel.
grep -c "otherwise: bb0]" over --dump-mir of behav_labeled_tail_goto.w, spec_ss14_11_collection_await.w, and behav_std_build_api.w: 0, 0, 0.
Spec / decisions
Not a language-spec matter. AGENTS.md's single-source-of-truth rule for ABI (D6, FnAbi) is the closest principle: a per-backend re-derivation of a MIR encoding is the pattern that produced the T*/T** divergence. No decision-log entry documents the switch-default encoding.
Related PRs
None since 450733e touch emit_switch_term or the LLVM switch lowering's default handling (b9e0e65 touched other terminators). #982's review already proposes a terminator-target validator; this is the same fix's second acceptance criterion.
Would I champion a fix? Yes, folded into #982's validator work.
Acceptance criteria:
One documented encoding in src/MirCore.w next to TermKind: either "d2 is always a valid block index" (then the C backend drops the != 0 sentinel and abort() path) or "d2 < 0 means no default" (then the C backend tests < 0 and emits abort() or __builtin_unreachable(), and the LLVM backend's existing >= 0 test is the same rule).
validate_mir_body rejects a switchInt whose d2 is outside [0, block_count) unless it equals the documented sentinel; --validate-all reports it.
The MIR printer renders the sentinel distinctly (e.g. otherwise: unreachable) so dumps cannot show a phantom bb0 branch.
A MIR validation test (src/MirValidationTests.w) covers default = 0, default = -1, and default = block_count.
Untested
No runtime probe is possible without hand-built MIR; #989 (hostile-MIR testing) is the prerequisite for a negative test that actually executes both backends on a bb0 default.
Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced in source; no program reaches it today. The C backend still treats a switch default of 0 as "no default" (emit_switch_term, src/CCodegen.w:9179-9190: if d2 != 0: goto bb{d2} else abort();), while the LLVM backend treats any d2 >= 0 as a block index (src/CodegenDispatch.w:16086-16089). No validator states the convention.
post-release: latent, and it belongs with the emit-C campaign (#955, #982).
Fixed in #3255 (6a905fd). Root cause: MirCore's structural validator accepted a switch default of 0, which LLVM read as bb0 and emit-C as abort(). validate_mir_body now requires the default in 1..bb_count and CCodegen.emit_switch_term always emits the jump, so the convention is enforced in one place. Negative case: test/internals/mir_structural_validator_test.w.
Activity
ehartford commentedon Sep 16, 2026
Review: reproduced (latent, as the report says; established from source)
Verdict. The two backends read the
TK_SWITCH_INTdefault payloadd2under different conventions, and nothing in MIR construction, the MIR printer, or the validators pins either one. That is exactly the report's claim. It is latent today: everyswitchIntproducer inMirLower.wpasses a freshly allocated block as the default,bb0is the entry block and is never re-targeted, and a dump of several test programs shows nootherwise: bb0. So there is no runtime divergence to run; the defect is the missing contract, which direct source evidence establishes. A third reading I found while checking: for a negatived2the LLVM backend branches to its unreachable block while the C backend would emitgoto bb-1, so the sentinel space disagrees in both directions.Tested revision and compiler
main@ 5809ac1 (2026-09-14)with buildof 5809ac1 in a detached worktree, self-reportswith v0.15.2.1(used only for--dump-mir)Evidence
src/CCodegen.w:8157-8180emit_switch_term:if d2 != 0: goto bb{d2} else: abort(), in both the single-target and the table path. Zero is a sentinel meaning "no default".src/CodegenDispatch.w:15403-15408:default_bb = mir_default_unreachable_bb_value(); if d2 >= 0: default_bb = mir_bb_values[d2]. Zero isbb0; negative is the sentinel.src/Mir.w:172-203: printsotherwise: bb{d2}unconditionally, so a dump would render the C sentinel as a real branch tobb0.terminate(TermKind.TK_SWITCH_INT, ..., <block>, 0)sites insrc/MirLower.w(lines 4523 to 6992) pass a real block (else_bb,exit_bb,fail_bb,skip_bb,rhs_bb,end_bb), never a sentinel.validate_mir_body(src/MirCore.w:2256) checks array lengths only; no check relatesd2toblock_countor to a documented sentinel (the same gap as emit-c emits empty stub labels for out-of-range BB references instead of failing #982).grep -c "otherwise: bb0]"over--dump-mirofbehav_labeled_tail_goto.w,spec_ss14_11_collection_await.w, andbehav_std_build_api.w: 0, 0, 0.Spec / decisions
Not a language-spec matter. AGENTS.md's single-source-of-truth rule for ABI (D6,
FnAbi) is the closest principle: a per-backend re-derivation of a MIR encoding is the pattern that produced theT*/T**divergence. No decision-log entry documents the switch-default encoding.Related PRs
None since 450733e touch
emit_switch_termor the LLVM switch lowering's default handling (b9e0e65 touched other terminators). #982's review already proposes a terminator-target validator; this is the same fix's second acceptance criterion.Would I champion a fix? Yes, folded into #982's validator work.
Acceptance criteria:
src/MirCore.wnext toTermKind: either "d2is always a valid block index" (then the C backend drops the!= 0sentinel andabort()path) or "d2 < 0means no default" (then the C backend tests< 0and emitsabort()or__builtin_unreachable(), and the LLVM backend's existing>= 0test is the same rule).validate_mir_bodyrejects aswitchIntwhosed2is outside[0, block_count)unless it equals the documented sentinel;--validate-allreports it.otherwise: unreachable) so dumps cannot show a phantombb0branch.src/MirValidationTests.w) covers default = 0, default = -1, and default = block_count.Untested
No runtime probe is possible without hand-built MIR; #989 (hostile-MIR testing) is the prerequisite for a negative test that actually executes both backends on a
bb0default.ehartford commentedon Oct 25, 2026
Triage 2026-10-25,
with v0.15.3.0(main a261b65): reproduced in source; no program reaches it today. The C backend still treats a switch default of 0 as "no default" (emit_switch_term,src/CCodegen.w:9179-9190:if d2 != 0: goto bb{d2}elseabort();), while the LLVM backend treats anyd2 >= 0as a block index (src/CodegenDispatch.w:16086-16089). No validator states the convention.post-release: latent, and it belongs with the emit-C campaign (#955, #982).
Issues batch F: #1071 #984 #989 #2269 (#3255)
ehartford commentedon Nov 11, 2026
Fixed in #3255 (6a905fd). Root cause: MirCore's structural validator accepted a switch default of 0, which LLVM read as bb0 and emit-C as abort(). validate_mir_body now requires the default in 1..bb_count and CCodegen.emit_switch_term always emits the jump, so the convention is enforced in one place. Negative case: test/internals/mir_structural_validator_test.w.