w6c+wwstage: implicit [N]T->[]T array-to-slice coercion via desugar (#258)

Hare admits an array with a defined length wherever its element slice is
expected (assign / return / call-arg / init) as a borrow; ww rejected it
everywhere (the #108(c) exclusion), so base64 worked around the gap with
explicit a[0:n] slices.

type_assignable / isassignable now admit array->slice on an exact element
match (mirror ref/harec/src/types.c:1080-1097, the SLICE-dst arm). The four
acceptance sites route through one shared helper (desugar_arrayslice /
desugararrayslice) that rewrites the array expr to the explicit full slice
arr[0:len(arr)] — an N_SLICE over the array base. cgen is untouched: the
existing slice lowering (#252/#257/#135 made array bases, incl struct-field
arrays, correct) materialises the borrow header {.ptr=&arr[0], .len=N,
.cap=N}, byte-identically in both stages.

wwstage runs no general call-arg / N_ASSIGN typecheck, so checkassign +
desugarcallargs are added solely to route those two contexts through the
shared desugar (rule-10). desugarcallargs additionally loud-rejects an
element-MISMATCH array into a []T param, scoped to that shape so wwstage's
broader call-arg leniency is untouched.

953_arraytoslice_run covers the four contexts + a borrow-alias proof + the
i32/u8 element axis (dual-stage run + cs==ww byte-id), plus mismatch-reject
rows asserting both stages refuse [4]i32 -> []u8. Regen'd w6c + wwdump
combined.ww (#110).
This commit is contained in:
2026-06-02 05:26:55 +09:00
parent 6bcb0929f8
commit e92708ecda
7 changed files with 829 additions and 7 deletions

View File

@@ -924,6 +924,46 @@ coerce_floatlit(Node *n, Type *target)
n->type = ty_f32;
}
/* desugar_arrayslice — #258. The single shared injection point for the
* implicit [N]T → []T borrow. type_assignable already admits an array
* with a defined length into a matching []T slot (see type.c:#258); here
* we lower it to the explicit full slice `arr[0:len(arr)]` (an N_SLICE
* over the array base), reusing the existing slice cgen — #252/#257/#135
* made array bases (incl struct-field arrays) correct. No new array→slice
* store cgen, and the borrow header is byte-identical across stages.
*
* Mutates `expr` IN PLACE: the original array expr moves into a fresh base
* node (keeping its stamped array type for cgen's esz/alen), and `expr`
* becomes the N_SLICE — preserving the sibling link so a desugared
* call-arg keeps its place in the argument list. Self-guards on shape, so
* the four acceptance sites can call it unconditionally; it no-ops unless
* the dst is a slice and the src an array with an exactly-matching
* element. */
static void
desugar_arrayslice(Checker *c, Type *dst, Node *expr)
{
if (dst == NULL || expr == NULL || expr->type == NULL)
return;
Type *du = (dst->kind == TY_NAMED) ? dst->under : dst;
Type *su = (expr->type->kind == TY_NAMED) ? expr->type->under
: expr->type;
if (du == NULL || su == NULL ||
du->kind != TY_SLICE || su->kind != TY_ARRAY)
return;
if (su->alen == SIZE_UNDEFINED || !type_eq(du->sub, su->sub))
return;
Node *base = newnode(c->a, expr->kind, expr->pos);
Node *next = expr->next;
*base = *expr;
base->next = NULL;
memset(expr, 0, sizeof *expr);
expr->kind = N_SLICE;
expr->pos = base->pos;
expr->next = next;
expr->lhs = base; /* sliced base; lo/hi NULL → 0 : len(arr) */
expr->type = type_slice(c->a, su->sub);
}
static Type *
cbinop(Checker *c, Node *n)
{
@@ -1483,6 +1523,8 @@ cexpr(Checker *c, Node *n)
&& !assignable_addrfn(c, p->type, a))
err(c, a->pos, "argument type %s not assignable to %s",
type_name(c->a, at), type_name(c->a, p->type));
/* #258: `f(arr)` borrows the array as a full slice. */
desugar_arrayslice(c, p->type, a);
p = p->next;
}
if (p != NULL && !p->variadic)
@@ -1510,6 +1552,8 @@ cexpr(Checker *c, Node *n)
!assignable_addrfn(c, l, n->rhs))
err(c, n->pos, "cannot assign %s to %s",
type_name(c->a, r), type_name(c->a, l));
/* #258: `s = arr` borrows the array as a full slice. */
desugar_arrayslice(c, l, n->rhs);
return n->type = l;
}
case N_STRUCTLIT: {
@@ -1904,6 +1948,8 @@ clet(Checker *c, Node *n)
type_name(c->a, initt), type_name(c->a, declared));
/* #104 fold-2: `let x: f32 = 1.0` — narrow the init literal to f32. */
coerce_floatlit(n->rhs, declared);
/* #258: `let s: []T = arr` borrows the array as a full slice. */
desugar_arrayslice(c, declared, n->rhs);
n->type = t;
if (n->str && n->str[0]) {
check_module_shadow(c, n->str, n->pos, "let");
@@ -1945,6 +1991,8 @@ cstmt(Checker *c, Node *n)
type_name(c->a, rt), type_name(c->a, c->ret));
/* #104 fold-2: `fn g() f32 = { return 1.0; }` — narrow to f32. */
coerce_floatlit(n->lhs, c->ret);
/* #258: `return arr` borrows the array as a full slice. */
desugar_arrayslice(c, c->ret, n->lhs);
break;
}
case N_IF: {