Repository navigation
Description
error ... from: transitive ? conversion (§10.9)
Summary
error Top from Mid / error Mid from Low declarations generate wrapper variants and one-hop ? conversion, but a two-hop chain (Result[_, Low] propagated with ? from a function returning Result[_, Top]) is rejected with "? cannot convert error type 'LowError' to 'TopError'". Spec §10.9 says chained conversion works via transitivity. This implements the transitive search and the nested re-wrapping.
Spec
§10.9 Error Conversion with from:
error AppError from IoError, ParseError, DbErrorGenerates wrapper variants (
AppError.Io(IoError), etc.) andFromimplementations.?usesFromfor automatic conversion. Chained conversion works via transitivity.
So with error MidError from LowError and error TopError from MidError, a LowError propagated by ? in a function returning Result[_, TopError] must become TopError.Mid(MidError.Low(low)).
Current state
- One-hop works and is tested (
test/behavior/behav_error_decl.w; campaign 10.9.1.1/10.9.1.2 ok). v0.15.1 rejects two-hop conversion (10.9.1.3 absent). - The conversion search is
Sema.error_conversion_variant(src/SemaCheck.w:6355-6375): it scans only the direct single-payload variants of the target enum for a payload type compatible with the source — no recursion. - Callers: the
?type check incheck_unaryUOP_TRY (src/SemaCheck.w:4316-4322, emits the "? cannot convert error type ... " diagnostic at 4321) and the MIR fail-path wrap inlower_question_mark(src/MirLower.w:6515-6534), which wraps the source payload in exactly one enum variant (assign_enum_variant_to_place(..., conversion_variant, ...)). - Variant payload introspection used by the search:
enum_variant_payload_types(src/SemaCheck.w:6377-6416), declaration-ordered viatype_extra— deterministic, fixpoint-safe. error ... fromparsing/desugaring:src/Parser.w:2465-2492(variant per source error, named by stripping theErrorsuffix, payload = the source error type).
Implementation plan
- Sema chain search (
src/SemaCheck.w): addSema.error_conversion_chain(target_err_ty, source_err_ty) -> Vec[i32]returning an interleaved chain[enclosing_ty1, variant_sym1, enclosing_ty2, variant_sym2, ...]outermost-first (empty Vec + a found-flag, or a sentinel first element, for "identical types, no wrap"). Algorithm: breadth-first over the target enum's single-payload variants (reuse the scan shape of 6361-6374):- direct payload compatible with source → chain
[(target, v)]; - else, for each single-payload variant whose payload resolves to an enum type, enqueue that payload type with the extended prefix;
- maintain a visited set of resolved TypeIds (cycle guard for mutually-
fromerrors) and a depth cap (8 is generous); - BFS guarantees the shortest chain wins; iterate variants in declaration order (never map order) so the result is deterministic for fixpoint.
- direct payload compatible with source → chain
- Ambiguity: if two distinct chains of the same minimal length reach the source type, emit a sema error:
ambiguous error conversion from 'Low' to 'Top': via variants 'A' and 'B'(name the first divergent variants in declaration order). One-hop behavior cannot regress: a length-1 chain is found before any length-2 chain. - Rewire callers: keep
error_conversion_variantas the one-hop fast path or fold it into the chain API; update:check_unaryUOP_TRY (src/SemaCheck.w:4316-4322): accept when a chain exists; keep the existing "? cannot convert error type 'X' to 'Y'" message when none does.lower_question_mark(src/MirLower.w:6515-6534): replace the single wrap with an innermost-first loop: start from the source Err payload operand, then for each(enclosing_ty, variant_sym)from the chain end to its start, allocate a temp ofenclosing_tyandassign_enum_variant_to_place(temp, enclosing_ty, variant_sym, [cur_op], span), making the temp the nextcur_op. The final operand feeds the existingsyms.errwrap of the function's return type.
- Intermediate types in MIR: the chain must carry the intermediate enum TypeIds (that is why step 1 returns (type, variant) pairs, not bare variant syms) — MirLower must not re-derive them.
- Sema+MirLower only; no rt/Link/seed hazard. Standard gates; pay attention to
with build :fixpointbecause the search order is new code (declaration-order iteration only).
Edge cases: source type identical to target (chain empty — existing types_compatible early-out at 6358 stays first); diamond graphs (Top from MidA, MidB; MidA from Low; MidB from Low → ambiguous, step 2); self-referential error A from A (cycle guard); generic error enums as intermediates (enum_variant_payload_types already resolves generic payloads, 6396-6415); chains where an intermediate variant has ≥2 payloads (not a wrapper — skip, only single-payload variants participate, same rule as today).
Test plan
test/behavior/behav_error_from_transitive.w(expect-stdoutok):error LowError = Bad(code: i32) error MidError from LowError error TopError from MidError fn low() -> Result[i32, LowError]: Err(.Bad(7)) fn top() -> Result[i32, TopError]: low()? // match Err(TopError.Mid(MidError.Low(LowError.Bad(code)))) => assert code == 7- Three-hop variant of the same shape (depth robustness).
- One-hop regression:
behav_error_decl.wunchanged and green. - Mixed:
Top from Mid, OtherErrorwhere the chain must route throughMidonly. test/compile_errors/error_from_ambiguous.w: diamond (TopError from MidA, MidB, bothfrom LowError) thenlow()?in a-> Result[_, TopError]fn →//! expect-error: ambiguous error conversion.test/compile_errors/error_from_unrelated.w: unrelated error with no path still gets "? cannot convert error type" (existing message preserved).
Acceptance criteria
- Two-hop and three-hop
?conversion produce correctly nested wrapper variants observable by pattern matching - One-hop conversion and its diagnostics unchanged
- Ambiguous diamond conversions rejected with a named-variants diagnostic; resolution is declaration-order deterministic
- with build && with build :fixpoint && with build :test pass
Dependencies
- Overlaps Tests: error decls coverage (§10) #498 (error-decl test coverage). Related: Add the Error trait; error declarations implement it (§10.6, §10.8) #434 (Error trait on the same decls), Implement user Try trait for ? (§10.2, §11.7) #408 (user Try
?dispatch sits beside this conversion logic incheck_unary/lower_question_mark).
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 10.9.1.3.
Activity
ehartford commentedon Jun 14, 2026
Implemented in e31f769. Added deterministic transitive
error ... fromconversion search for?, MIR lowering that wraps error payloads through the full conversion chain, and positive/negative coverage for two-hop, three-hop, mixed sibling, one-hop regression, and equal-length ambiguous conversion diagnostics. Verification passed: build, fixpoint, full test suite, and test-green.