Skip to content

D86 (#1864): assert/require/check are compiler-known forms; the message is evaluated only on failure - #2030

Merged
ehartford merged 4 commits into
fix-2014from
impl-1864
Oct 3, 2026
Merged

ehartford merged 4 commits into
fix-2014from
impl-1864

Conversation

@ehartford

@ehartford ehartford commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Implements D86 (spec §18.2, #1864): "assert, require, check and their std.testing forms are compiler-known forms, not functions. Each evaluates its message operand only when its condition is false. The message operand follows the rules of the right operand of ??: its effects do not run when the condition holds, and a move inside it is a conditional move."

Design

  • Sema owns the fact (D65). check_call records each call whose resolved callee is assert/require/check declared in std/builtins.w or std/testing.w in Sema.precondition_form_calls (call node → form). A user's own fn check is an ordinary function.
  • Sema move state. The operands after the condition are checked like an if arm that does not return: the scope and field-move/poison state is saved after the condition and restored after the call. A move inside the message is a conditional move on a path that does not return, so the value is still owned after a passing form.
  • Not a function. Naming a form as a value (let f = require, run(check), testing.assert) is refused. A function value would evaluate the message before the call.
  • MIR (MirLower.w lower_precondition_form). Lower the condition, then switch on it. The failing path is a ??-style lazy arm: it lowers the message and the location default (still the call site's src()), calls the declaration with false, flushes its pending resets, and ends unreachable. The passing path continues. c |> require(msg) lowers the same way with the piped value as the condition. Emit-C reads the same MIR.
  • Comptime evaluator (eval_precondition_form): same order. Note: comptime fns cannot call the forms today ("comptime can only call comptime functions"), so this path only matters where the evaluator runs ordinary code.
  • std declarations keep their parameters and bodies and now document the form contract: the compiler calls the declaration only on a false condition, and the declaration does not return.

Two adjacent fixes, found by the fixtures:

  • ?? with a diverging default (§18.2 names ??'s right operand as the model). Sema now restores move state when the default is Never, as it does for a diverging if arm. Before, o ?? panic(f"{consume(r)}") followed by a use of r was refused.
  • ?? on a Copy subject dropped the subject on the default path. When the default diverges, --validate-all then reported a leak (o ?? panic("x")). The default path no longer drops a Copy subject.

Filed rather than fixed: #2024, a pipeline stage call omits the callee's default arguments (invalid LLVM).

Tests (each new test failed on the base compiler first)

  • test/behavior/precondition_lazy.w: //! known-issue: #1864 dropped; covers all three builtins forms.
  • test/behavior/precondition_lazy_std_testing.w: testing.assert/require/check and an imported require.
  • test/behavior/precondition_lazy_pipeline.w: the piped condition. The message would index out of range if evaluated. The base compiler produced invalid LLVM here.
  • test/behavior/precondition_message_runs_on_failure{,_check,_assert,_std_testing}.w: on failure the message runs once, after the condition, and the form panics with it (exit 134, stderr).
  • test/behavior/precondition_message_conditional_move.w: a message that consumes a Drop value, in every form. The value is usable afterwards and dropped once. Also an owned str passed to testing.check.
  • test/debug_alloc/da_precondition_message_conditional_move.w: the same moves under the debug allocator, leak count=0.
  • test/emit_c/precondition_lazy.w: the C backend. The emitted C calls the message only in the failing block, which ends in the unreachable panic.
  • test/compile_errors/err_1864_{require_as_value,check_passed_as_fn,testing_assert_as_value}.w: the refusals.
  • test/behavior/coalesce_diverging_default_conditional_move.w and test/phase/validate_coalesce_diverging_default_copy.w: the two ?? fixes.

--validate-all: ok and analyze audit:all: violations=0 on the precondition fixtures.

Gate: src/main build :gate green; :deep-debug-tool-tests green; :drop-audit 254 cells, 0 regressions (see comment).

@ehartford

Copy link
Copy Markdown
Contributor Author

Iterate tier on 1958e43 (pinned seed driving): src/main build :gate rc=0, all targets green (fixed list; this worktree has no battery measurements), wall 259 s. :deep-debug-tool-tests green. :drop-audit: 254 cells, 0 non-PASS, 0 regressions vs baseline. All 15 new and touched fixtures pass on stage1, and the debug-alloc fixture reports leak count=0. The battery has not been run; this stays a draft until it is.

Eric Hartford added 3 commits October 3, 2026 04:02
…ge is evaluated only on failure

Sema records each call of the std.builtins / std.testing forms
(precondition_form_calls) and checks the operands after the condition as
an if-arm that does not return: what they move or invalidate is
untouched after the form. MirLower branches on the condition and lowers
the message, location and the declaration's call only on the failing
path, ending it unreachable. The comptime evaluator does the same. The
forms are refused as values.
…path is a lazy arm; a diverging ?? default keeps what it would move

`c |> require(msg)` lowers as the form with `c` as its condition (the
pipeline path also dropped the location default: #2024 is the general
case). The failing path uses ??'s lazy-arm frame. Sema: a ?? fallback
that does not return leaves the move state at the join untouched, as a
diverging if arm does; D86 names ??'s right operand as the model.
The default path dropped the subject whatever its type. For a Copy
subject that drop is a no-op, but it made the local owned storage to the
ownership validator, which reported it Init at return (a leak) on the
success path whenever the default diverges: `o ?? panic(..)` failed
--validate-all. Found by D86's ?? fixture (#1864).
@ehartford
ehartford changed the base branch from main to fix-2014 October 3, 2026 11:27
@ehartford
ehartford added this pull request to stack #2032 October 3, 2026 11:27
…ype is its address (#1864 exposed)

D86 made assert a branch on its condition, so two values that were poison
or undef on main became UB the optimizer folded to `brk #1` in main
(behav_rotate, behav_int_to_pointer_cast_sign_extends exit 133):

- CodegenDispatch.w, ROTATE_LEFT/RIGHT lowered `(x << n) | (x >> (W - n))`;
  n of 0 or W shifts by W, poison (`shl i32 x, 32` in the IR). It is now
  llvm.fshl/fshr(x, x, n), the count modulo the width as
  comptime_rotate_bits takes it.
- CodegenDispatch.w mir_const_value, CK_INT_EXACT at a pointer Sema type
  (the module const `-1 as extern "C" fn(...)` folded by MirLower) returned
  `undef` silently (`icmp eq i64 undef, -1`); CK_INT at a pointer type was an
  i32. Both are the address now (const_int_address), and an exact constant
  with no lowering is a reported BUG, not an undef.
- CodegenTraits.w, a module binding of function-pointer type with value 0
  was emitted `@STATIC = internal constant i32 0` and read as eight bytes;
  every binding whose type lowers to a pointer gets its address constant.
@ehartford
ehartford marked this pull request as ready for review October 3, 2026 15:13
@ehartford
ehartford merged commit 69739fb into main Oct 3, 2026
@ehartford
ehartford deleted the impl-1864 branch October 3, 2026 15:13
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.

MIR: a pipeline stage call omits the callee's default arguments (invalid LLVM: wrong argument count)

1 participant