selfhost+test: wwstage type-info loss through struct-field N_INDEX (#37)

The original #37 symptom (worker-34's `..findflag` mangle) cannot
reproduce on master — was a runtime miscompile misattributed to a
link-time issue. Investigation surfaced three real wwstage cgen
gaps in the N_INDEX-through-struct-field family, sister bugs to
#34 (N_INDEX N_IDENT-base) and #36 (primsize-default-to-8).

1. `indexbaseesz` slice-element stride defaulted to 8 for named-
   struct elements. `&opts.ptr[i]` for `opts: *[]option` computed
   MOVQ $8 instead of $24. Fix: route slice case through
   `elemsizeofc(c, innert)`; ptr-to-named-struct via structlookup.

2. `cgun TK_AMP N_INDEX` ignored N_DOT base. `&p.ptr[i]` left
   esz=8 because only N_IDENT base was handled. Mirror cgindex's
   existing N_DOT arm.

3. `nodeisstr` N_INDEX arm only walked N_IDENT bases. `cmd.argsptr[i]`
   for `argsptr: *str` returned false; pushargsrev dropped the
   .len half at call sites. Add N_DOT-base arm that walks the
   struct field's pointee.

Test 713 (struct_field_index): 3 rows × 2 stages = 6 fixtures, one
per fix shape. Runtime-only; bootstrap byte-id (995_self_rebuild)
covers cross-stage drift.

Residual: getopttest's errortable still fails through wwstage with
a slice-of-str-via-&arr[expr] miscompile. Filed as task #38.
This commit is contained in:
2026-05-16 12:52:36 +09:00
parent 2fb594748c
commit de3bd5cc3b
6 changed files with 452 additions and 6 deletions

View File

@@ -6798,6 +6798,60 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
};
};
};
// N_INDEX through a struct field: e.g. cmd.argsptr[i]
// where argsptr: *str. cgindex correctly loads the
// (ptr, len) pair via indexbaseesz; without this arm
// pushargsrev would only push AX and lose the .len.
if (base.kind == nkind.N_DOT) {
let fld: str = base.str;
if (streq(fld, "ptr")) { return false; };
if (streq(fld, "len")) { return false; };
if (streq(fld, "cap")) { return false; };
let inner: *node = base.lhs;
if (inner != nil) {
if (inner.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, inner.str);
if (lc != nil) {
let tn: *node = lc.tnode;
let sname: str;
sname.ptr = nil; sname.len = 0;
if (tn != nil) {
if (tn.kind == nkind.N_TNAME) { sname = tn.str; };
if (tn.kind == nkind.N_TPTR) {
let pinner: *node = tn.lhs;
if (pinner != nil) {
if (pinner.kind == nkind.N_TNAME) {
sname = pinner.str;
};
};
};
};
if (sname.len > 0) {
let si: *structinfo = structlookup(c, sname);
if (si != nil) {
let fi: *fieldinfo = si.fields;
for (fi != nil) {
if (streq(fi.fname, fld)) {
let ft: *node = fi.tnode;
if (ft != nil) {
let elem: *node = nil;
let fk: nkind = ft.kind;
if (fk == nkind.N_TPTR) { elem = ft.lhs; };
if (fk == nkind.N_TSLICE) { elem = ft.lhs; };
if (fk == nkind.N_TARRAY) { elem = ft.lhs; };
if (elem != nil) {
return isstrtype(c, elem);
};
};
};
fi = fi.finext;
};
};
};
};
};
};
};
};
return false;
};
@@ -7155,7 +7209,10 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
if (innert.kind == nkind.N_TNAME) {
if (streq(innert.str, "str")) { return 1; };
};
if (innert.kind == nkind.N_TSLICE) { return elemsizeof(innert); };
// Slice element: resolve through elemsizeofc so a slice of a
// named struct (e.g. *[]option) returns the struct stride
// instead of falling through to elemsizeof's default 8.
if (innert.kind == nkind.N_TSLICE) { return elemsizeofc(c, innert); };
return 8;
};
@@ -7186,6 +7243,13 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
if (streq(elem.str, "str")) { return 16; };
let ps: i32 = primsize(elem.str);
if (ps > 0) { return ps; };
// Pointer to named struct: indexing
// stride is the struct slot size.
// Without this, &p.ptr[i] for p.ptr:
// *S falls through to 8 and reads
// the wrong element.
let si: *structinfo = structlookup(c, elem.str);
if (si != nil) { return si.totsize; };
};
};
return 8;
@@ -11942,7 +12006,13 @@ fn cgun(c: *cgen, n: *node) void = {
};
};
};
};
} else { if (base.kind == nkind.N_DOT) {
// `&p.ptr[i]` shape: stride is the element
// of the slice/struct-pointer field, not
// the default 8. Mirrors cgindex's N_DOT
// arm so &p.ptr[i] and p.ptr[i] agree.
esz = indexbaseesz(c, base);
};};
};
cgexpr(c, idx);
if (esz > 1) {

View File

@@ -2403,7 +2403,13 @@ fn cgun(c: *cgen, n: *node) void = {
};
};
};
};
} else { if (base.kind == nkind.N_DOT) {
// `&p.ptr[i]` shape: stride is the element
// of the slice/struct-pointer field, not
// the default 8. Mirrors cgindex's N_DOT
// arm so &p.ptr[i] and p.ptr[i] agree.
esz = indexbaseesz(c, base);
};};
};
cgexpr(c, idx);
if (esz > 1) {

View File

@@ -555,6 +555,60 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
};
};
};
// N_INDEX through a struct field: e.g. cmd.argsptr[i]
// where argsptr: *str. cgindex correctly loads the
// (ptr, len) pair via indexbaseesz; without this arm
// pushargsrev would only push AX and lose the .len.
if (base.kind == nkind.N_DOT) {
let fld: str = base.str;
if (streq(fld, "ptr")) { return false; };
if (streq(fld, "len")) { return false; };
if (streq(fld, "cap")) { return false; };
let inner: *node = base.lhs;
if (inner != nil) {
if (inner.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, inner.str);
if (lc != nil) {
let tn: *node = lc.tnode;
let sname: str;
sname.ptr = nil; sname.len = 0;
if (tn != nil) {
if (tn.kind == nkind.N_TNAME) { sname = tn.str; };
if (tn.kind == nkind.N_TPTR) {
let pinner: *node = tn.lhs;
if (pinner != nil) {
if (pinner.kind == nkind.N_TNAME) {
sname = pinner.str;
};
};
};
};
if (sname.len > 0) {
let si: *structinfo = structlookup(c, sname);
if (si != nil) {
let fi: *fieldinfo = si.fields;
for (fi != nil) {
if (streq(fi.fname, fld)) {
let ft: *node = fi.tnode;
if (ft != nil) {
let elem: *node = nil;
let fk: nkind = ft.kind;
if (fk == nkind.N_TPTR) { elem = ft.lhs; };
if (fk == nkind.N_TSLICE) { elem = ft.lhs; };
if (fk == nkind.N_TARRAY) { elem = ft.lhs; };
if (elem != nil) {
return isstrtype(c, elem);
};
};
};
fi = fi.finext;
};
};
};
};
};
};
};
};
return false;
};
@@ -912,7 +966,10 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
if (innert.kind == nkind.N_TNAME) {
if (streq(innert.str, "str")) { return 1; };
};
if (innert.kind == nkind.N_TSLICE) { return elemsizeof(innert); };
// Slice element: resolve through elemsizeofc so a slice of a
// named struct (e.g. *[]option) returns the struct stride
// instead of falling through to elemsizeof's default 8.
if (innert.kind == nkind.N_TSLICE) { return elemsizeofc(c, innert); };
return 8;
};
@@ -943,6 +1000,13 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
if (streq(elem.str, "str")) { return 16; };
let ps: i32 = primsize(elem.str);
if (ps > 0) { return ps; };
// Pointer to named struct: indexing
// stride is the struct slot size.
// Without this, &p.ptr[i] for p.ptr:
// *S falls through to 8 and reads
// the wrong element.
let si: *structinfo = structlookup(c, elem.str);
if (si != nil) { return si.totsize; };
};
};
return 8;

View File

@@ -6798,6 +6798,60 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
};
};
};
// N_INDEX through a struct field: e.g. cmd.argsptr[i]
// where argsptr: *str. cgindex correctly loads the
// (ptr, len) pair via indexbaseesz; without this arm
// pushargsrev would only push AX and lose the .len.
if (base.kind == nkind.N_DOT) {
let fld: str = base.str;
if (streq(fld, "ptr")) { return false; };
if (streq(fld, "len")) { return false; };
if (streq(fld, "cap")) { return false; };
let inner: *node = base.lhs;
if (inner != nil) {
if (inner.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, inner.str);
if (lc != nil) {
let tn: *node = lc.tnode;
let sname: str;
sname.ptr = nil; sname.len = 0;
if (tn != nil) {
if (tn.kind == nkind.N_TNAME) { sname = tn.str; };
if (tn.kind == nkind.N_TPTR) {
let pinner: *node = tn.lhs;
if (pinner != nil) {
if (pinner.kind == nkind.N_TNAME) {
sname = pinner.str;
};
};
};
};
if (sname.len > 0) {
let si: *structinfo = structlookup(c, sname);
if (si != nil) {
let fi: *fieldinfo = si.fields;
for (fi != nil) {
if (streq(fi.fname, fld)) {
let ft: *node = fi.tnode;
if (ft != nil) {
let elem: *node = nil;
let fk: nkind = ft.kind;
if (fk == nkind.N_TPTR) { elem = ft.lhs; };
if (fk == nkind.N_TSLICE) { elem = ft.lhs; };
if (fk == nkind.N_TARRAY) { elem = ft.lhs; };
if (elem != nil) {
return isstrtype(c, elem);
};
};
};
fi = fi.finext;
};
};
};
};
};
};
};
};
return false;
};
@@ -7155,7 +7209,10 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
if (innert.kind == nkind.N_TNAME) {
if (streq(innert.str, "str")) { return 1; };
};
if (innert.kind == nkind.N_TSLICE) { return elemsizeof(innert); };
// Slice element: resolve through elemsizeofc so a slice of a
// named struct (e.g. *[]option) returns the struct stride
// instead of falling through to elemsizeof's default 8.
if (innert.kind == nkind.N_TSLICE) { return elemsizeofc(c, innert); };
return 8;
};
@@ -7186,6 +7243,13 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
if (streq(elem.str, "str")) { return 16; };
let ps: i32 = primsize(elem.str);
if (ps > 0) { return ps; };
// Pointer to named struct: indexing
// stride is the struct slot size.
// Without this, &p.ptr[i] for p.ptr:
// *S falls through to 8 and reads
// the wrong element.
let si: *structinfo = structlookup(c, elem.str);
if (si != nil) { return si.totsize; };
};
};
return 8;
@@ -11942,7 +12006,13 @@ fn cgun(c: *cgen, n: *node) void = {
};
};
};
};
} else { if (base.kind == nkind.N_DOT) {
// `&p.ptr[i]` shape: stride is the element
// of the slice/struct-pointer field, not
// the default 8. Mirrors cgindex's N_DOT
// arm so &p.ptr[i] and p.ptr[i] agree.
esz = indexbaseesz(c, base);
};};
};
cgexpr(c, idx);
if (esz > 1) {