Skip to content

Remove Result from the hardcoded @[must_use] set #371

Closed

Description

@ehartford

Remove Result from the hardcoded @[must_use] set

Summary

Spec v7.0+ removed Result from the @[must_use] exhaustiveness rule: discarding or partially matching a Result carries no obligation (§10.1). The compiler still hardcodes Result (alongside Task) as must_use, so statement-position partial matches on Result are rejected and users are forced into _ => {} ceremony — exactly the ritual §10.1 and AGENTS.md forbid ("a dropped Result does nothing, so a must-use Result diagnostic is forbidden"). This removes the hardcoding; Task and user @[must_use] types keep the rule.

Spec

§10.1 Result and Option: "Discarding a Result or Option has no side effect. The compiler does not require ceremony such as let _ = expr merely to acknowledge that discard." §9.7 Pattern Matching (exhaustiveness): statement-position match allows partial matches; only must-use types require exhaustiveness there — and Result is no longer one of them. Expected-legal example:

match db.execute("..."):
    Ok(n) => log(n)
// legal: unmatched Err arms are a no-op (§10.1)

Current state

  • src/SemaDecl.w:116-122: after decl collection, // Hardcode Result and Task as must_use types inserts both Result and Task symbols into must_use_types.
  • src/SemaCheck.w:6070-6081 (check_match exhaustiveness gate): statement-position matches normally allow partial coverage, but must_use_types.contains(type_sym) forces require_exhaustive = 1 (comment at 6077-6078), producing "non-exhaustive match: missing variant" for Ok-only Result matches.
  • test/behavior/must_use_match.w currently asserts the old behavior, including a section commented "Result (hardcoded must_use) requires exhaustive match".
  • User @[must_use] attribute types and Task flow into the same must_use_types set and must keep working (a dropped Task cancels it — its discard matters, so its rule is legitimate).
  • Statement-position discard of a Result (db.execute(...) as an expression statement) already compiles without ceremony (campaign 10.1.1.3-5, verified manually) — this issue is only about the partial-match leg.

Implementation plan

  1. src/SemaDecl.w:116-122: delete the sym_result lookup/insertion; keep sym_task. (The comment becomes "Hardcode Task as must_use".)
  2. test/behavior/must_use_match.w: replace the Result section with the inverse expectation — a statement-position match on a Result covering only Ok must compile and run; keep the @[must_use] enum Status exhaustiveness sections and the wildcard-satisfies case unchanged.
  3. Grep the test suite and stdlib for _ => {} / _ => arms that exist only to satisfy the old Result rule and simplify where they obscure a test's point (grep -rn "_ =>" test/ | grep -i result); keep this cleanup minimal and in the same commit only when the file is already being touched — do not batch unrelated edits.
  4. Confirm there is no second enforcement site: grep -n must_use_types src/ (expected: the SemaDecl insert, the attribute-collection insert for user types, and the SemaCheck gate at 6079).
  5. Sema-only change; no rt/Link/seed hazard. Standard gates.

Edge cases that must keep failing/working:

  • Expression-position match on Result still requires exhaustiveness (it produces a value).
  • @[must_use] user enums still require exhaustive statement matches; wildcard still counts as catch-all.
  • Task statement-position handling unchanged (§14.7 disposition rules are separate machinery).

Test plan

  • test/behavior/behav_result_partial_match.w (expect-stdout ok):
    fn fallible(n: i32) -> Result[i32, str]: if n > 0: Ok(n) else: Err("neg")
    fn main:
        match fallible(1):
            Ok(v) => assert(v == 1)
        match fallible(-1):
            Ok(_) => assert(false, "unreachable")
        print("ok")
    
    (second match's Err is silently skipped — the §10.1 no-op guarantee).
  • Update test/behavior/must_use_match.w per step 2 (Result partial match compiles; @[must_use] Status still enforced — keep its existing negative coverage or pair it with a test/compile_errors/ case asserting "non-exhaustive match" for the user must-use type).
  • test/compile_errors/match_expr_position_result.w: expression-position let x = match r: Ok(v) => v (no Err arm) → //! expect-error: non-exhaustive.
  • Overlap: discard-statement coverage lives in Tests: result discard coverage (§10) #521; do not duplicate here.

Acceptance criteria

  • Statement-position partial match on Result compiles with no diagnostic and unmatched variants are a no-op at runtime
  • Expression-position Result matches still require exhaustiveness
  • Task and user @[must_use] types still require exhaustive statement-position matches
  • test/behavior/must_use_match.w updated to the new contract
  • with build && with build :fixpoint && with build :test pass

Dependencies

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 10.1.1.4 (no-ceremony rule; see also #521 for 10.1.1.3-5 test coverage). Spec reference: §9.7 Pattern Matching (exhaustiveness), §10.1 Result and Option — spec v7.0.

Activity

  1. ehartford commented on Jun 11, 2026

    @ehartford
    ContributorAuthor

    Implemented in 10e871a. Result is no longer hardcoded as must-use; partial statement-position Result matches are legal no-ops, while user @[must_use] types and Task still require exhaustive statement-position matches. Expression-position Result matches still check generic enum exhaustiveness, and for-comprehensions now diagnose mixed Option/Result carrier chains directly. Verification passed: build, fixpoint, full test suite, and test-green.

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