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.
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
^
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
The flow crosses three files:
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.
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.
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.
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.
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.
// 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));
}
}
}
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.
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.
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.
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.
| 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 |
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.
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.