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: {

View File

@@ -386,6 +386,24 @@ type_assignable(Type *dst, Type *src)
return pa == NULL && pb == NULL;
}
/* #258: implicit [N]T → []T array-to-slice borrow. Hare admits an
* array with a DEFINED length wherever its element slice is expected
* — assign / return / call-arg / init alike (ref/harec/src/types.c:
* 1080-1097, the SLICE-dst arm). The checker accepts it here; the
* acceptance sites (clet / N_ASSIGN / call-arg gather / N_RETURN)
* then DESUGAR the array expr to an explicit full slice `arr[0:len
* (arr)]` via desugar_arrayslice, reusing the existing slice cgen so
* there is ZERO new array→slice store and the borrow header
* {.ptr=&arr[0], .len=N, .cap=N} is byte-identical across stages.
* Element types must match exactly — no element decay. */
{
Type *du = (dst->kind == TY_NAMED) ? dst->under : dst;
Type *su = (src->kind == TY_NAMED) ? src->under : src;
if (du && su && du->kind == TY_SLICE && su->kind == TY_ARRAY &&
su->alen != SIZE_UNDEFINED && type_eq(du->sub, su->sub))
return 1;
}
/* #108(c): opaque is a type-erasure sink. Any pointer is assignable
* to *opaque, and any slice to []opaque — the universal void-pointer
* and erased slice. harec type_is_assignable: ptr→*opaque at ref/harec/src/
@@ -394,13 +412,11 @@ type_assignable(Type *dst, Type *src)
* (to_secondary->storage == STORAGE_OPAQUE) return true;`).
*
* Array→[]opaque (harec's STORAGE_POINTER-to-array and array→slice
* decay, types.c:1080-1099) is deliberately EXCLUDED: ww has no
* implicit array→slice conversion for any element type (`let s:
* []i32 = a` is rejected too — a slice is built only via an explicit
* `a[0:n]` op), so there is no array→slice-header cgen. Accepting
* array→[]opaque alone would assign a fat array local into a 24-byte
* slot with no decay — a silent miscompile (rule 7). sort's caller
* passes a slice, so slice→[]opaque is the only shape it needs.
* decay, types.c:1080-1099) stays EXCLUDED here: the #258 arm above
* desugars a concrete-element borrow only on an EXACT element match,
* and this arm fires only when dst and src share a kind (ptr→ptr,
* slice→slice), so an array reaches []opaque only after an explicit
* `a[0:n]` slice. ww-stricter than Hare; documented divergence.
*
* Both rules fire only when the destination element is opaque, so
* they are inert on the opaque-free selfhost corpus. */