Files
sx/examples/0783-modules-qualified-generic-alias-instance-dispatch.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

24 lines
1.1 KiB
Plaintext

// A generic-struct ALIAS whose RHS is a qualified head (`ABox :: a.Box(s64)`)
// must make an ALIAS-typed receiver (`x: ABox`) a first-class dispatch instance:
// `x.tag()` runs the author `a`'s body with `a`'s bindings — never a dead end.
// Two namespaces author a same-name generic `Box($T)` with a DIFFERENT layout and
// a DIFFERENT `tag`; the alias over `a.Box(s64)` must dispatch `a.Box.tag`.
//
// Regression (Phase E4 Counter-2): the alias registration cloned the layout into
// a fresh type named `ABox` but did NOT mirror the instance template/bindings/
// author onto the alias name, so `x.tag()` on an `ABox` receiver resolved to
// nothing (unresolved). Copying the three instance-map entries to the alias name
// makes the alias receiver dispatch its author's method.
#import "modules/std.sx";
a :: #import "0783-modules-qualified-generic-alias-instance-dispatch/a.sx";
b :: #import "0783-modules-qualified-generic-alias-instance-dispatch/b.sx";
ABox :: a.Box(s64);
main :: () -> s32 {
x : ABox = .{ x = 5 };
print("size={} tag={}\n", size_of(ABox), x.tag());
0
}