Skip to content

Tests: default op coverage (§10) #493

Closed

Description

@ehartford

Tests: default op coverage (§10)

Summary

The ?? default operator is implemented, but four of its spec'd behaviors are only "verified manually" (campaign survey): RHS laziness, the break/continue early-exit forms, the labeled forms, and the match desugaring. Only the basic value-default and the ?? return form are in the suite. This adds the missing positive coverage so regressions in lower_double_question are caught.

Spec

§10.4 Default Operator (??):

  • "expr ?? default desugars to expr.unwrap_or(default). The right-hand side is lazily evaluated (only computed if left is None)." (10.4.1.3, 10.4.1.7 — the normative desugar to the match form is given explicitly)
  • "Early exit form: ?? can be followed by return, break, or continue for early exit on None." (10.4.1.4)
  • "The break and continue forms may include labels (§13.5a): let token = lexer.peek() ?? break 'scan" (10.4.1.5)

Current state

  • Implementation: MirBuilder.lower_double_question (src/MirLower.w:6568ff), dispatched from the binary-op lowering at src/MirLower.w:7782. Campaign verdicts 10.4.1.1/.2 ok; .3/.4/.5/.7 are ok|gap — behavior verified manually, untested.
  • Existing test: test/spec/spec_ss10_4_default_operator.w (20 lines) covers: basic default, chained a ?? b ?? c ?? 0, and ?? return Err(...) only. No laziness test, no break/continue, no labels, no desugar-equivalence check.

Implementation plan

Test-only issue; no compiler changes expected. If any listed form fails, that is a new compiler bug: file it with the repro and do not work around it in the test (AGENTS.md).

  1. Extend test/spec/spec_ss10_4_default_operator.w (or add test/spec/spec_ss10_4_default_operator_forms.w if it grows past readability) with the cases in the test plan.
  2. For laziness, use a helper whose evaluation is detectable without globals: fn boom() -> i32: assert(false, "?? rhs evaluated eagerly") — Some(5) ?? boom() must not trip it; and a counter variant (var local + closure or Vec push) proving the RHS is evaluated exactly once on None.
  3. Mirror the §10.4 desugaring example as an equivalence test: same input through ?? and through the explicit match form; assert equal results.

Test plan

All positive cases in test/spec/spec_ss10_4_default_operator*.w (assert-based, //! expect-stdout: ok convention of sibling tests):

  • Laziness (10.4.1.3): Some(5) ?? boom() returns 5 without evaluating boom; (None: Option[i32]) ?? lazy() evaluates lazy exactly once.
  • Early-exit return (10.4.1.4): existing ?? return Err(...) kept; add a plain-value ?? return 0 form in a non-Result fn.
  • Early-exit break (10.4.1.4): while true: loop popping from a Vec-backed stack: let item = stack.pop() ?? break; assert items seen and loop exited.
  • Early-exit continue (10.4.1.4): iterate Vec[Option[i32]] with let v = item ?? continue; assert only Some payloads accumulated.
  • Labeled forms (10.4.1.5): nested loops with 'outer: label: ?? break 'outer exits both levels; ?? continue 'outer advances the outer loop — assert iteration counts.
  • Desugar equivalence (10.4.1.7): find_user(id) ?? return Err(.NotFound) behaves identically to the spec's expanded match ... Some(v) => v / None => return ... form for both Some and None inputs.
  • Chaining regression: keep a ?? b ?? c ?? 0 (10.4.1.1).
  • Negative (compile error): test/compile_errors/default_op_type_mismatch.w — Some(1) ?? "str" (RHS type incompatible with payload) → //! expect-error: with the actual diagnostic text observed (pin after running once; do not guess the message).

Acceptance criteria

  • Requirements 10.4.1.3, 10.4.1.4, 10.4.1.5, 10.4.1.7 each have at least one passing automated test
  • Any behavior found broken is filed as its own compiler bug with spec reference, not patched around in tests
  • with build && with build :fixpoint && with build :test pass (with build :test-green updated)

Dependencies

None blocking. Related: #515/#521 (sibling §10 coverage issues); ?? on chains interacts with ?. (covered in #515's chain+default case, already present in spec_ss10_3_optional_chaining.w).

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 10.4.1.3, 10.4.1.4, 10.4.1.5, 10.4.1.7.

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Closing — ?? laziness (RHS unevaluated when LHS is Some), control-flow RHS (break), and payload-type-mismatch rejection pinned (spec_ss10_4_default_op_control_flow + err_default_op_type_mismatch, ec03406). §10.4. (Inline Some(x) as the ?? operand hits codegen bug #634, worked around via a bound Option local.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions