Skip to content

error ... from: transitive ? conversion (§10.9) #433

Closed

Description

@ehartford

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, DbError

Generates wrapper variants (AppError.Io(IoError), etc.) and From implementations. ? uses From for 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 in check_unary UOP_TRY (src/SemaCheck.w:4316-4322, emits the "? cannot convert error type ... " diagnostic at 4321) and the MIR fail-path wrap in lower_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 via type_extra — deterministic, fixpoint-safe.
  • error ... from parsing/desugaring: src/Parser.w:2465-2492 (variant per source error, named by stripping the Error suffix, payload = the source error type).

Implementation plan

  1. Sema chain search (src/SemaCheck.w): add Sema.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-from errors) 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.
  2. 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.
  3. Rewire callers: keep error_conversion_variant as the one-hop fast path or fold it into the chain API; update:
    • check_unary UOP_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 of enclosing_ty and assign_enum_variant_to_place(temp, enclosing_ty, variant_sym, [cur_op], span), making the temp the next cur_op. The final operand feeds the existing syms.err wrap of the function's return type.
  4. 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.
  5. Sema+MirLower only; no rt/Link/seed hazard. Standard gates; pay attention to with build :fixpoint because 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-stdout ok):
    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.w unchanged and green.
  • Mixed: Top from Mid, OtherError where the chain must route through Mid only.
  • test/compile_errors/error_from_ambiguous.w: diamond (TopError from MidA, MidB, both from LowError) then low()? 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

Traceability

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

Activity

  1. ehartford commented on Jun 14, 2026

    @ehartford
    ContributorAuthor

    Implemented in e31f769. Added deterministic transitive error ... from conversion 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.

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