Skip to content

Tests: optional chaining coverage (§10) #515

Closed

Description

@ehartford

Summary

Add focused coverage for the §10.3 optional-chaining method case where the chained method itself returns an Option or Result. Existing tests cover field flattening and non-optional method wrapping, but not the desugaring rule that says expr?.method(args) must use and_then when the method already returns a wrapper. This is a coverage issue, not a feature implementation issue unless the new test exposes a regression.

Spec

§10.3 Optional Chaining says ?. accesses a field or method on an Option or Result, returning None/Err when the receiver is absent. The desugaring is type-aware:

  • expr?.field maps non-optional fields with map.
  • expr?.field flattens optional fields with and_then.
  • expr?.method(args) desugars to expr.and_then(v => v.method(args)) when the method returns Option or Result.

The missing requirement is 10.3.1.5: expr?.method(args) must not produce Option[Option[T]] or Result[Result[T, E], E] when the method already returns the wrapper.

Expected examples:

type Parser { text: str }

fn Parser.prefix(self: &Self, n: usize) -> Option[str]:
    if self.text.len() >= n: Some(self.text[0..n]) else: None

let p: Option[Parser] = Some(Parser { text: "abcd" })
let got: Option[str] = p?.prefix(2)

Current state

  • docs/with-specification.md:4143-4164 defines optional chaining and the method-return flattening rule.
  • test/spec/spec_ss10_3_optional_chaining_type_aware_desugaring.w:6-25 already covers non-optional field wrapping, optional field flattening, None propagation for optional fields, and non-optional method return wrapping via len().
  • src/Parser.w:3966 recognizes ?.; src/Parser.w:4435 builds NK_OPTIONAL_CHAIN.
  • src/Ast.w:1509 documents NK_OPTIONAL_CHAIN as d0=expr, d1=member, d2=extra_start.
  • src/SemaCheck.w:5296-5359 computes optional-chain result types. The method path calls optional_chain_method_raw_result_type, then returns the raw wrapper type unchanged when the method already returns Option or Result; otherwise it wraps in Option.
  • src/MirLower.w:7566-7607 lowers optional-chain methods. It calls the method on the unwrapped payload and assigns the raw result directly when raw_ret_ty == result_ty; otherwise it wraps the value in Some.
  • The coverage gap from out/req-campaign/g05.tsv is specific: "Option-returning method flatten untested."

Implementation plan

  1. Add a spec test, preferably test/spec/spec_ss10_3_optional_chaining_method_flatten.w, next to the existing §10.3 tests.
  2. Cover an Option[T] receiver whose method returns Option[U]. Assert both Some and None receiver paths, plus the method-returning-None path, and assign the result to Option[U] so Option[Option[U]] cannot pass typechecking.
  3. Cover a Result[T, E] receiver whose method returns Result[U, E]. Assert both Ok and Err receiver paths, plus the method-returning-Err path, and assign the result to Result[U, E] so nested results cannot pass typechecking.
  4. Include an argument-bearing method call so optional_chain_arg_start/optional_chain_arg_count and lower_call_arg are exercised, not only zero-argument method lookup.
  5. If any test fails, fix the root cause in Sema.optional_chain_method_result_type, Sema.optional_chain_method_raw_result_type, or MirBuilder.lower_optional_chain_method; do not alter the test to match nested-wrapper behavior.

Test plan

  • New positive test test/spec/spec_ss10_3_optional_chaining_method_flatten.w:
    • Option[Parser]?.prefix(n) -> Option[str] returns Some("ab") for a present receiver and valid length.
    • Same expression returns None when the receiver is None.
    • Same expression returns None when the method returns None.
    • Result[Parser, ParseError]?.parse_digit(n) -> Result[i32, ParseError] returns Ok(value) for a present receiver and valid input.
    • Same expression preserves the original receiver Err.
    • Same expression returns the method's own Err when the receiver is Ok but the method rejects the input.
  • Optional negative compile test test/compile_errors/optional_chain_method_flatten_type.w: assign p?.prefix(2) to Option[Option[str]] and expect a type mismatch, if the existing compile-error harness can express the expected diagnostic robustly.

Acceptance criteria

  • §10.3 method-return flattening has positive coverage for both Option and Result
  • The test proves the result type is flattened by type annotation
  • None/Err receiver short-circuit behavior and method-returning-empty behavior are both covered
  • No production code changes are made unless the new coverage exposes a real bug
  • with build && with build :fixpoint && with build :test pass

Dependencies

None.

Traceability

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

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Closing — optional-chaining flatten (opt?.method() where method returns Option, no Option[Option[T]]; None receiver short-circuits) pinned by spec_ss10_3_optional_chaining_flatten (ec03406). §10.3.1.5. (Assertions use match rather than == Some(...) to avoid codegen bug #634.)

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