Files
sx/examples/0778-modules-bare-generic-instance-by-value-receiver.sx
agra 6406d0fb1f fix(stdlib/E4): collapse generic-struct author matrix into four choke-points
The generic-struct author-selection matrix {bare,qualified} × {site} × {layout,
body} drifted per-site across 12 attempts because method bodies were resolved by
bare template name in `fn_ast_map["Box.method"]`, independent of which author
produced the instance's layout. Collapse it into four choke-points so
layout-author ≡ body-author by construction:

  CP-1 `selectGenericStructHead` — the single layout-head selector every generic
       struct head site funnels through (alias-RHS .call/.parameterized, array-
       literal, static head, resolveTypeCall/ParameterizedWithBindings). Emits the
       visibility / missing-member diagnostics inline; returns a control-flow-only
       union. No head site reads `struct_template_map` for selection directly.
  CP-2 author stamp — non-optional `decl: *StructDecl` on `StructTemplate` (set at
       the sole producer `buildGenericStructTemplate`) + `struct_instance_author`
       written at `instantiateGenericStruct` from the SAME `tmpl` that builds the
       layout; re-stamped on the dedup fast-path so an instance is never returned
       without an author.
  CP-3 alias metadata copy — mirror template/bindings/author from the mangled
       instance onto the alias display name, so an `ABox`-typed receiver is a
       first-class dispatch instance (Counter-2).
  CP-4 `genericInstanceMethod` / `ensureGenericInstanceMethodLowered` — the single
       body reader: inline methods select via the stamped author (`structMethodFn`,
       source-pin follows for free); impl-block methods fall back to the template-
       keyed `fn_ast_map` entry. Routes the four bespoke body sites (static head,
       instance dispatch, param typing, protocol thunk) + the new qualified static
       head (`a.Box(s64).make(7)`, finding #2).

A debug assert locks `struct_instance_author` / `struct_instance_template` keyset
coincidence so a future third writer that forgets the author trips a test.

Goldens 0777/0778/0780 (bare instance method — ptr/by-value/param-typed, finding
#1), 0779/0785 (qualified static head + missing member, finding #2), 0783 (alias
instance dispatch, Counter-2), 0782 (ambiguity containment). 0414/0415/0543 and
the FFI suites stay green.
2026-06-08 20:34:53 +03:00

21 lines
975 B
Plaintext

// A BARE generic struct instance method with a BY-VALUE receiver (`self: Box`)
// must dispatch to the VISIBLE author's method, whose receiver ABI matches the
// VISIBLE author's layout. The single visible `b.Box` is one field (size 8); the
// 2-flat-hop `c.Box` is two fields (size 16). Selecting `c.Box.dbl` (a `{i64,i64}`
// by-value receiver) for a `b.Box` instance (`{i64}`) mismatches the layout.
//
// Regression (Phase E4 finding #1, by-value receiver variant): before
// body-author ≡ layout-author, the instance method resolved by bare template name
// and selected `c.Box.dbl`, whose by-value receiver signature did not match the
// `b.Box` instance layout (an LLVM verify failure / wrong body). With the fix it
// runs `b.Box.dbl` (`self.x * 2`).
#import "modules/std.sx";
#import "0778-modules-bare-generic-instance-by-value-receiver/b.sx";
main :: () -> s32 {
x := Box(s64).make(5);
print("size={} dbl={}\n", size_of(type_of(x)), x.dbl());
0
}