Skip to content

Tests: result discard coverage (§10) #521

Closed

Description

@ehartford

Summary

Add tests that pin the §10.1 no-ceremony discard rule for Result and Option. With must not force let _ = expr when a discarded Result or Option has no side effect; an expression statement is already an explicit choice to ignore the value. This issue is coverage for expression-statement discard and should stay separate from #371, which removes the remaining Result partial-match must_use behavior.

Spec

§10.1 Result and Option says errors are values and that discarding Result or Option has no side effect. The compiler "does not require ceremony such as let _ = expr merely to acknowledge that discard"; users should propagate, match, or bind the value when handling matters, otherwise an expression statement is enough (docs/with-specification.md:4105-4124).

Examples this issue should lock down:

fn fallible(ok: bool) -> Result[i32, str]:
    if ok: Ok(1) else: Err("no")

fn maybe(ok: bool) -> Option[i32]:
    if ok: Some(1) else: None

fn main:
    fallible(false)  // legal discard
    maybe(false)     // legal discard

Current state

  • docs/with-specification.md:4107-4124 explicitly forbids requiring let _ = expr for Result/Option discard.
  • src/SemaCheck.w:4368-4388 checks block statements in statement position and emits an unused-value error only for discarded non-scoped Task values (E0801: unused Task value). It does not emit an expression-statement discard error for Result or Option.
  • src/SemaDecl.w:116-122 still inserts both Result and Task into must_use_types; Remove Result from the hardcoded @[must_use] set #371 owns removing Result from that set. This affects statement-position partial match, not bare expression-statement discard.
  • src/SemaCheck.w:6069-6081 uses must_use_types to force exhaustive statement-position matches for Result today; again, that is Remove Result from the hardcoded @[must_use] set #371, not this issue.
  • out/req-campaign/g05.tsv records the specific coverage gap for 10.1.1.3, 10.1.1.4, and 10.1.1.5: discard was verified manually, no ceremony is required, expression-statement discard works, but there is no committed test.
  • out/issue-enrich/new/371.md already documents and plans the partial-match cleanup. This issue should cross-reference it and avoid duplicating that implementation.

Implementation plan

  1. Add a behavior test, preferably test/behavior/behav_result_option_discard.w, that calls functions returning Result and Option as bare expression statements. Include both success and failure/none paths so the test proves the ignored value itself does not change control flow.
  2. In the same test, include equivalent let _ = ... examples as allowed style, but keep the primary assertion that let _ = is optional, not required.
  3. Add a negative guard for Task discard in test/compile_errors/unused_task_value.w if current coverage is missing. The point is to show the distinction: Result/Option discard is harmless; dropped Task has cancellation semantics and remains an error.
  4. Do not change src/SemaDecl.w for this issue unless Remove Result from the hardcoded @[must_use] set #371 is being implemented at the same time. If Result partial-match failures block this test, the test was scoped incorrectly; use expression statements, not partial matches.
  5. Update any coverage manifest or spec test index used by the test harness if required by the local test layout.

Test plan

  • test/behavior/behav_result_option_discard.w:
    • fallible(true) and fallible(false) as expression statements both compile and run.
    • maybe(true) and maybe(false) as expression statements both compile and run.
    • Side-effect counters around those calls prove the calls executed even though their wrappers were ignored.
    • let _ = fallible(false) remains legal style but is not necessary.
  • test/compile_errors/unused_task_value.w or an existing Task-discard test:
    • A bare async task expression still produces E0801: unused Task value from src/SemaCheck.w:4384-4387.
  • Remove Result from the hardcoded @[must_use] set #371 test coverage:
    • Statement-position partial match on Result becomes legal there; do not duplicate it here except as a cross-reference.

Acceptance criteria

  • Bare expression-statement discard of Result is tested and accepted
  • Bare expression-statement discard of Option is tested and accepted
  • let _ = remains accepted but is not required by any diagnostic
  • Task discard remains rejected, preserving the semantic distinction
  • Remove Result from the hardcoded @[must_use] set #371 remains the owner for Result partial-match must_use cleanup
  • with build && with build :fixpoint && with build :test pass

Dependencies

Related to #371. This issue can add expression-statement discard coverage before #371, but full §10.1 no-ceremony compliance also needs #371's Result partial-match cleanup.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 10.1.1.3, 10.1.1.4, 10.1.1.5.

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Closing — bare Result/Option expression-statement discard pinned by behav_result_option_discard (6c59303): side effects run, no ceremony. Task negative already covered by err_unused_task. §10.1.

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