Repository navigation
Description
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?.fieldmaps non-optional fields withmap.expr?.fieldflattens optional fields withand_then.expr?.method(args)desugars toexpr.and_then(v => v.method(args))when the method returnsOptionorResult.
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-4164defines optional chaining and the method-return flattening rule.test/spec/spec_ss10_3_optional_chaining_type_aware_desugaring.w:6-25already covers non-optional field wrapping, optional field flattening,Nonepropagation for optional fields, and non-optional method return wrapping vialen().src/Parser.w:3966recognizes?.;src/Parser.w:4435buildsNK_OPTIONAL_CHAIN.src/Ast.w:1509documentsNK_OPTIONAL_CHAINasd0=expr,d1=member,d2=extra_start.src/SemaCheck.w:5296-5359computes optional-chain result types. The method path callsoptional_chain_method_raw_result_type, then returns the raw wrapper type unchanged when the method already returnsOptionorResult; otherwise it wraps inOption.src/MirLower.w:7566-7607lowers optional-chain methods. It calls the method on the unwrapped payload and assigns the raw result directly whenraw_ret_ty == result_ty; otherwise it wraps the value inSome.- The coverage gap from
out/req-campaign/g05.tsvis specific: "Option-returning method flatten untested."
Implementation plan
- Add a spec test, preferably
test/spec/spec_ss10_3_optional_chaining_method_flatten.w, next to the existing §10.3 tests. - Cover an
Option[T]receiver whose method returnsOption[U]. Assert bothSomeandNonereceiver paths, plus the method-returning-Nonepath, and assign the result toOption[U]soOption[Option[U]]cannot pass typechecking. - Cover a
Result[T, E]receiver whose method returnsResult[U, E]. Assert bothOkandErrreceiver paths, plus the method-returning-Errpath, and assign the result toResult[U, E]so nested results cannot pass typechecking. - Include an argument-bearing method call so
optional_chain_arg_start/optional_chain_arg_countandlower_call_argare exercised, not only zero-argument method lookup. - If any test fails, fix the root cause in
Sema.optional_chain_method_result_type,Sema.optional_chain_method_raw_result_type, orMirBuilder.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]returnsSome("ab")for a present receiver and valid length.- Same expression returns
Nonewhen the receiver isNone. - Same expression returns
Nonewhen the method returnsNone. Result[Parser, ParseError]?.parse_digit(n) -> Result[i32, ParseError]returnsOk(value)for a present receiver and valid input.- Same expression preserves the original receiver
Err. - Same expression returns the method's own
Errwhen the receiver isOkbut the method rejects the input.
- Optional negative compile test
test/compile_errors/optional_chain_method_flatten_type.w: assignp?.prefix(2)toOption[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
OptionandResult - The test proves the result type is flattened by type annotation
-
None/Errreceiver 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 :testpass
Dependencies
None.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 10.3.1.5.
Activity
ehartford commentedon Jul 3, 2026
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.)
test: coverage-sweep batch 2 (#483 #484 #522 #493 #494 #514 #515 #532)