Auto-Recover Error Message Options

Issue #2822 — improving the error when array literal auto-recovery lowers a variable's capability

The problem

When the compiler auto-recovers an array literal (wrapping it in an implicit recover iso so it can be assigned to a val target), any non-sendable variable defined outside the synthetic recover has its capability silently lowered to tag. The subtype check then fails, and the error message reports the lowered capability without explaining why it changed.

The user sees Foo tag where they declared Foo ref, with no indication that auto-recovery caused the change.

Current error output

Error:
main.pony:36:22: array element not a subtype of specified array type
        let b = Bar([f])
                    ^
    Info:
    main.pony:19:32: array type: Foo box
      new create(children: ReadSeq[Foo box] val) =>
                                  ^
    main.pony:33:16: element type: Foo tag
            let f: Foo ref = Foo
                  ^
    main.pony:33:16: Foo tag is not a subtype of Foo box:
      tag is not a subcap of box
            let f: Foo ref = Foo
                  ^

Proposed error output

Error:
main.pony:36:22: array element not a subtype of specified array type
        let b = Bar([f])
                    ^
    Info:
    main.pony:19:32: array type: Foo box
      new create(children: ReadSeq[Foo box] val) =>
                                  ^
    main.pony:33:16: element type: Foo tag
            let f: Foo ref = Foo
                  ^
    main.pony:33:16: Foo tag is not a subtype of Foo box:
      tag is not a subcap of box
            let f: Foo ref = Foo
                  ^
    main.pony:33:16: f is Foo ref but is treated as Foo tag
      because the array literal is implicitly recovered

How it works today

The flow crosses three files:

  1. src/libponyc/expr/array.c:438–464 — expr_pre_array() detects that the array type would match the antecedent if capabilities were ignored. It wraps the literal in a synthetic TK_RECOVER node and processes it through the expr pass.
  2. src/libponyc/expr/reference.c:556–566 — expr_localref() sees the variable reference inside a recover where the definition is outside. It silently lowers the cap to tag via set_cap_and_ephemeral(type, TK_TAG, TK_NONE). No diagnostic is emitted.
  3. src/libponyc/expr/array.c:532–543 — expr_array() runs the subtype check. The element type is already tag. The error chain says “tag is not a subcap of box” with no explanation of why it became tag.

Options

A. Reconstruct at error time Low effort Low maintenance

At the error site in expr_array(), after the subtype check fails, check whether the element is a bare variable reference (TK_VARREF or TK_LETREF). If so, compare the cap on the variable's definition (the original declared type, still intact on the def node) with the cap on the reference (the lowered type). When they differ and we're inside a recover, add the explanatory info line.

Files changed 1 — array.c Lines added ~15 New infra None Coverage Bare variable references only. Does not cover complex expressions like [foo.bar()] or [consume x] where the element isn't a simple variable reference. Maintenance Near zero. The detection logic reads existing AST structure that has been stable for years (definition node type vs. reference node type). No new invariants to maintain. If the auto-recover mechanism itself changes, this code is adjacent to it in the same file.

Sketch

// In expr_array(), after the is_subtype failure and before errorframe_report:
if((ast_id(ele) == TK_VARREF || ast_id(ele) == TK_LETREF)
  && opt->check.frame->recover != NULL)
{
  ast_t* def = (ast_t*)ast_data(ele);
  ast_t* def_type = ast_type(def);
  if(def_type != NULL && !is_typecheck_error(def_type))
  {
    token_id def_cap = cap_single(def_type);
    token_id ref_cap = cap_single(c_type);
    if(def_cap != ref_cap)
    {
      ast_error_frame(&frame, ele,
        "%s is %s but is treated as %s because the array "
        "literal is implicitly recovered",
        ast_nice_name(ast_child(def)),
        ast_print_type(def_type, opt->strtab),
        ast_print_type(c_type, opt->strtab));
    }
  }
}
B. Flag the synthetic recover Medium effort Low maintenance

Add an AST_FLAG_AUTO_RECOVER flag. Set it on the synthetic TK_RECOVER node created in expr_pre_array(). In expr_localref(), when lowering a cap inside a flagged recover, set a companion flag on the variable reference node. At the error site in expr_array(), check the flag and add the explanation.

Files changed 3 — ast.h, array.c, reference.c Lines added ~25 New infra 2 AST flag bits (13 of 32 bits remain after this) Coverage Any element expression, not just bare variable references — the flag propagates through the reference node regardless of surrounding expression structure. Maintenance Low. The flag is set in one place and read in one place. The main risk is AST flag bit exhaustion — the enum is a 32-bit int and 19 bits are already used. Each new flag gets us closer to needing a wider type.
C. Store original type before lowering High effort Medium maintenance

Before set_cap_and_ephemeral() in expr_localref(), stash the original type on the reference AST node (e.g., as an extra child or via a side table). At the error site, retrieve it and include both the original and lowered types in the message.

Files changed 3+ — reference.c, array.c, possibly ast.h or a new header Lines added ~40–60 New infra Type stashing mechanism — either extra AST children (changes the node shape, must be handled by every AST walker) or a side hash table (must be allocated and freed with the pass context). Coverage Complete. Any expression whose type was lowered, any reason for the lowering. Could also serve future improvements for explicit recover blocks. Maintenance Medium. Extra AST children change the shape that every pass and visitor expects. A side table needs lifecycle management. Either adds an invariant that future changes must respect.

Comparison

Option Files Lines New infra Coverage Maintenance
A. Reconstruct 1 ~15 None Bare refs only Near zero
B. Flag 3 ~25 2 flag bits All expressions Low
C. Stash type 3+ ~40–60 Stashing mechanism Complete Medium
Recommendation. Option A covers the case from the issue (and likely the vast majority of real-world occurrences — array elements are almost always bare variable references or constructor calls), touches one file, adds no new infrastructure, and has effectively no ongoing maintenance cost. Option B is the natural next step if broader coverage proves necessary.

What each option does not cover

All three options address the array literal auto-recover case only. The same silent lowering happens when a user writes an explicit recover block, and the resulting error is equally confusing. None of these options improve that case — but the explicit recover case is less surprising because the user wrote the recover themselves and can reason about it. The auto-recover case is worse because the user doesn't know a recover exists.

Method call auto-recovery (in call.c) is a separate mechanism with its own error messages that already include hints like “this would be possible if the receiver were more ephemeral.” It isn't affected by this change.

Existing precedent

The compiler already has explanatory hints in the same error infrastructure. The pattern is an ast_error_frame() call appended to the errorframe chain, producing an indented info line. Examples:

All of these use the errorframe_t chain. None of them currently explain auto-recovery. The proposed change would be the first to do so.