feat(pbs): add concrete generic structs and free functions

This commit is contained in:
2026-10-22 14:42:38 +01:00
parent 240216170c
commit 76a8b074e8
52 changed files with 3285 additions and 103 deletions
@@ -0,0 +1,211 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import p.studio.compiler.backend.bytecode.*;
import p.studio.compiler.models.*;
import p.studio.compiler.pbs.*;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.parser.PbsParser;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.*;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.file.*;
import static org.junit.jupiter.api.Assertions.*;
class PbsGenericStructsTest {
private static final String BOX = """
declare struct Box<T>(value: T) {
fn get() -> T { return this.value; }
fn set(value: T) { this.value = value; }
}
""";
private static Path directory() { return Path.of(System.getProperty("pbs.registryConformanceDirectory")); }
private static BytecodeModule compile(String name, String source) throws Exception {
var diagnostics = DiagnosticSink.empty(); var id = new FileId(0);
var ast = PbsParser.parse(PbsLexer.lex(source, id, diagnostics), id, diagnostics, PbsParser.ParseMode.ORDINARY);
var file = new PbsFrontendCompiler().compileParsedFile(id, ast, diagnostics, SourceKind.PROJECT, new ModuleId(0),
ReadOnlyList.empty(), p.studio.compiler.messages.HostAdmissionContext.permissiveDefault(),
p.studio.compiler.messages.FESurfaceContext.empty(), new p.studio.compiler.source.tables.NameTable());
assertFalse(diagnostics.hasErrors(), () -> name+": "+diagnostics.stream().map(d -> d.getCode()+":"+d.getMessage()).toList());
var backend = IRBackend.builder().entryPointCallableName("Probe.main").entryPointModuleId(new ModuleId(0))
.executableFunctions(file.executableFunctions()).globals(file.globals()).registry(file.registry())
.intrinsicPool(file.intrinsicPool()).callableSignatures(file.callableSignatures()).build();
var program = new LowerToIRVMService().lower(backend);
var module = new BytecodeEmitter().emit(program.coherentEmissionPlan());
if (name.equals("gc")) module = PbsStructuredValuesTest.withTestSafepoint(module);
new BytecodePreloadVerifierService().verify(module);
Files.createDirectories(directory());
Files.write(directory().resolve("pbs-generic-struct-"+name+".pbx"), module.serialize());
return module;
}
@Test void genericAndHandwrittenObjectsPreserveReferenceIdentity() throws Exception {
compile("basic", BOX+"""
declare service Probe { fn main() -> int {
let box = new Box<int>(40); let alias = box;
alias.set(42); return box.get();
} }
""");
compile("manual", """
declare struct IntBox(value: int) {
fn get() -> int { return this.value; }
fn set(value: int) { this.value = value; }
}
declare service Probe { fn main() -> int {
let box = new IntBox(40); let alias = box;
alias.set(42); return box.get();
} }
""");
}
@Test void constructorsSelfAndPhantomApplicationsAreConcrete() throws Exception {
compile("constructor", """
declare struct Box<T>(value: T) {
ctor make(value: T) { this.value = value; }
fn same() -> Self { return this; }
fn get() -> T { return this.value; }
}
declare service Probe { fn main() -> int {
let box = new Box<int>.make(42); return box.same().get();
} }
""");
compile("phantom", """
declare struct Marker<T>() { fn answer() -> int { return 21; } }
declare service Probe { fn main() -> int {
let a = new Marker<int>(); let b = new Marker<long>(); return a.answer() + b.answer();
} }
""");
}
@Test void composedStorageAndCompleteGroupsExecute() throws Exception {
compile("multiple", """
declare struct Pair<K, V>(key: K, value: V) {
fn first() -> K { return this.key; }
fn second() -> V { return this.value; }
}
declare service Probe { fn main() -> int {
let pair = new Pair<int, int>(20, 22); return pair.first() + pair.second();
} }
""");
compile("tuple", BOX+"""
declare service Probe { fn main() -> int {
let box = new Box<(left: int, right: int)>((left: 20, right: 22));
let pair = box.get(); return pair.left + pair.right;
} }
""");
compile("optional", BOX+"""
declare service Probe { fn main() -> int {
let box = new Box<optional int>(none); box.set(some(42)); return box.get() else 0;
} }
""");
compile("array", BOX+"""
declare service Probe { fn main() -> int {
let box = new Box<array<int>>([42]); return box.get()[0] else 0;
} }
""");
compile("recursive", """
declare struct Node<T>(value: T, next: optional Node<T>) { fn get() -> T { return this.value; } }
declare service Probe { fn main() -> int { let node = new Node<int>(42, none); return node.get(); } }
""");
}
@Test void freeFunctionsFnAndOwnerBoundDispatchInteract() throws Exception {
compile("combined", BOX+"""
declare contract Reader<T> { fn read() -> T; }
implements<T> Reader<T> for Box<T> using box { fn read() -> T { return box.get(); } }
fn read<T>(reader: Reader<T>) -> T { return reader.read(); }
declare service Probe { fn main() -> int {
let box = new Box<int>(42); let reader: Reader<int> = box;
let invoke: Fn(Reader<int>) -> int = read<int>; return invoke(reader);
} }
""");
compile("function", """
fn identity<T>(value: T) -> T { return value; }
declare service Probe { fn main() -> int { let f: Fn(int) -> int = identity<int>; return f(42); } }
""");
compile("function-manual", """
fn identity(value: int) -> int { return value; }
declare service Probe { fn main() -> int { let f: Fn(int) -> int = identity; return f(42); } }
""");
}
@Test void specializedCapturesRemainRootedAcrossCollection() throws Exception {
compile("gc", BOX+"""
fn capture<T>(value: T) -> Fn() -> T {
let box = new Box<T>(value); return fn [box]() -> T { return box.get(); };
}
declare service Probe {
fn garbage() { let discarded: array<int> = new array(1); }
fn collect() { let marker = 314159; }
fn main() -> int {
let read = capture<int>(42);
for i: int from 0 until 1100 { Probe.garbage(); }
Probe.collect(); return read();
}
}
""");
}
@Test void installedGenericExampleUsesTheSameProductionPipeline() throws Exception {
try (var stream = PbsFrontendCompiler.class.getResourceAsStream("/pbs-knowledge/examples/game/1/src/generics.pbs")) {
assertNotNull(stream);
compile("installed", new String(stream.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8)
+ "\ndeclare service Probe { fn main() -> int { return genericsExample(); } }");
}
}
@Test void importsAliasesReexportsAndDefinitionScopedHelpersExecute(@org.junit.jupiter.api.io.TempDir Path root) throws Exception {
Files.createDirectories(root.resolve("src/policy")); Files.createDirectories(root.resolve("src/facade"));
Files.writeString(root.resolve("prometeu.json"), """
{"name":"app","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game","dependencies":[]}
""");
Files.writeString(root.resolve("src/mod.barrel"), "pub fn frame() -> void;");
var exports = "pub struct Box<T>; pub contract Reader<T>; pub fn read<T>(reader: Reader<T>) -> T; pub fn wrap<U>(value: U) -> Box<U>;";
Files.writeString(root.resolve("src/policy/mod.barrel"), exports);
Files.writeString(root.resolve("src/facade/mod.barrel"), exports);
Files.writeString(root.resolve("src/facade/main.pbs"), "import { Box, Reader, read, wrap } from @app:policy;");
Files.writeString(root.resolve("src/policy/main.pbs"), BOX+"""
declare contract Reader<T> { fn read() -> T; }
implements<T> Reader<T> for Box<T> using box { fn read() -> T { return box.get(); } }
fn helper<T>(value: T) -> T { return value; }
fn wrap<U>(value: U) -> Box<U> { return new Box<U>(helper<U>(value)); }
fn read<T>(reader: Reader<T>) -> T { return reader.read(); }
""");
Files.writeString(root.resolve("src/main.pbs"), """
import { Box as B, Reader as R, read as invoke, wrap as make } from @app:facade;
fn helper(value: int) -> int { return 0; }
[Frame] fn frame() -> void {
let box: B<int> = make<int>(42); let reader: R<int> = box;
let f: Fn(R<int>) -> int = invoke<int>;
if f(reader) != 42 { let fail = 1 / 0; }
}
""");
var result = p.studio.compiler.workspaces.BuilderPipelineService.INSTANCE.compile(
p.studio.compiler.models.BuilderPipelineContext.fromConfig(new p.studio.compiler.messages.BuilderPipelineConfig(false, root.toString())),
p.studio.utilities.logs.LogAggregator.stdout());
assertFalse(result.diagnostics().stream().anyMatch(d -> d.isError()), result.diagnostics().toString());
assertTrue(result.bytecodeBytes().length > 0);
Files.createDirectories(directory()); Files.write(directory().resolve("pbs-generic-struct-imports.pbx"), result.bytecodeBytes());
}
@Test void homonymousArgumentsFromDifferentModulesRemainNominallyDistinct(@org.junit.jupiter.api.io.TempDir Path root) throws Exception {
for (var module : new String[]{"policy", "left", "right"}) Files.createDirectories(root.resolve("src/"+module));
Files.writeString(root.resolve("prometeu.json"), """
{"name":"app","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game","dependencies":[]}
""");
Files.writeString(root.resolve("src/mod.barrel"), "pub fn frame() -> void;");
Files.writeString(root.resolve("src/policy/mod.barrel"), "pub struct Box<T>;");
Files.writeString(root.resolve("src/policy/main.pbs"), BOX);
for (var module : new String[]{"left", "right"}) {
Files.writeString(root.resolve("src/"+module+"/mod.barrel"), "pub struct Enemy;");
Files.writeString(root.resolve("src/"+module+"/main.pbs"), "declare struct Enemy(pub hp: int);");
}
Files.writeString(root.resolve("src/main.pbs"), """
import { Box } from @app:policy;
import { Enemy as Left } from @app:left;
import { Enemy as Right } from @app:right;
[Frame] fn frame() -> void {
let a = new Box<Left>(new Left(20));
let b = new Box<Right>(new Right(22));
if a.get().hp + b.get().hp != 42 { let fail = 1 / 0; }
}
""");
var result = p.studio.compiler.workspaces.BuilderPipelineService.INSTANCE.compile(
p.studio.compiler.models.BuilderPipelineContext.fromConfig(new p.studio.compiler.messages.BuilderPipelineConfig(false, root.toString())),
p.studio.utilities.logs.LogAggregator.stdout());
assertFalse(result.diagnostics().stream().anyMatch(d -> d.isError()), result.diagnostics().toString());
Files.createDirectories(directory()); Files.write(directory().resolve("pbs-generic-struct-origins.pbx"), result.bytecodeBytes());
}
}
@@ -87,10 +87,33 @@ class PbsRuntimeRandomTest {
var program = new LowerToIRVMService().lower(backend);
if(name.endsWith("-optimized")) program = new OptimizeIRVMService().optimize(program);
var module = new p.studio.compiler.backend.bytecode.BytecodeEmitter().emit(program.coherentEmissionPlan());
if (name.startsWith("random-null") || name.startsWith("random-typed-null")) module = withInjectedNull(module);
if (name.startsWith("random-gc")) module = PbsStructuredValuesTest.withTestSafepoint(module);
new p.studio.compiler.backend.bytecode.BytecodePreloadVerifierService().verify(module);
java.nio.file.Files.write(java.nio.file.Path.of(System.getProperty("pbs.registryConformanceDirectory"),"pbs-"+name+".pbx"),module.serialize());
}
// Controlled provider fixture: compile valid, fully initialized source, then
// suppress its unique one-slot field store. Allocation, field defaults, typed
// Array access and Random consumption still use the production provider.
private static p.studio.compiler.backend.bytecode.BytecodeModule withInjectedNull(
p.studio.compiler.backend.bytecode.BytecodeModule module) {
byte[] code = module.code().clone();
var entry = module.debugInfo().functionNames().stream().filter(f -> f.name().contains("Empty.$ctor.create")).findFirst().orElseThrow();
var function = module.functions().get(entry.funcIdx());
int replaced = 0;
for (int pc = function.codeOffset(); pc + 10 <= function.codeOffset() + function.codeLen(); pc += 2) {
if (code[pc] == (byte) 0xa2 && code[pc + 1] == 0
&& java.nio.ByteBuffer.wrap(code, pc + 6, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt() == 0) {
java.util.Arrays.fill(code, pc, pc + 10, (byte) 0);
code[pc] = 0x11; code[pc + 2] = 0x11; // discard field value and receiver
replaced++;
}
}
assertEquals(1, replaced, "unique controlled provider field store");
return new p.studio.compiler.backend.bytecode.BytecodeModule(module.version(), module.constPool(),
module.functions(), code, module.debugInfo(), module.exports(), module.syscalls(),
module.registry(), module.intrinsicSignatures());
}
@Test void providerSeedVectors() throws Exception {
String corpus;
try (var stream = PbsRuntimeRandomTest.class.getResourceAsStream("/random/random-conformance.tsv")) {
@@ -118,7 +141,7 @@ class PbsRuntimeRandomTest {
}
@Test void storageAndIndependentStreams() throws Exception {
pair("storage", """
declare struct Empty(pub value: Random) { ctor create() {} } declare global saved: Random = new Empty.create().value;
declare struct Empty(pub value: Random) { ctor create() { this.value = random.new(9223372036854775807); } } declare global saved: Random = new Empty.create().value;
declare struct Box(pub value: Random);
fn echo(rng: Random) -> Random { return rng; }
fn capture(rng: Random) -> Fn() -> long { return fn [rng]() -> long { return random.next(rng); }; }
@@ -144,7 +167,7 @@ class PbsRuntimeRandomTest {
""");
pair("named", """
declare global trace: int = 0;
declare struct Empty(pub value: Random) { ctor create() {} } declare global rng: Random = new Empty.create().value;
declare struct Empty(pub value: Random) { ctor create() { this.value = random.new(9223372036854775807); } } declare global rng: Random = new Empty.create().value;
declare service Probe {
fn receiver() -> Random { trace = trace * 10 + 1; return rng; }
fn low() -> long { trace = trace * 10 + 2; random.next(rng); return -7; }
@@ -158,7 +181,7 @@ class PbsRuntimeRandomTest {
""");
pair("positional", """
declare global trace: int = 0;
declare struct Empty(pub value: Random) { ctor create() {} } declare global rng: Random = new Empty.create().value;
declare struct Empty(pub value: Random) { ctor create() { this.value = random.new(9223372036854775807); } } declare global rng: Random = new Empty.create().value;
declare service Probe {
fn receiver() -> Random { trace = trace * 10 + 1; return rng; }
fn low() -> long { trace = trace * 10 + 2; random.next(rng); return -7; }
@@ -171,7 +194,7 @@ class PbsRuntimeRandomTest {
}
""");
}
@Test void rangeBoundsAndNullDefaults() throws Exception {
@Test void rangeBoundsAndControlledProviderNulls() throws Exception {
String[][] bounds = {{"0","1"},{"-10","-3"},{"-10","10"},{"0","16"},
{"-9223372036854775808","0"},{"-1","9223372036854775807"},{"-9223372036854775808","9223372036854775807"},
{"5","5"},{"6","5"}};
@@ -184,9 +207,9 @@ class PbsRuntimeRandomTest {
return (value: value, next: random.next(rng));
} }
""");
pair("null", "declare struct Empty(pub value: Random) { ctor create() {} } declare global empty: Random = new Empty.create().value; declare service Probe { fn main() -> long { return random.next(empty); } }");
pair("null", "declare struct Empty(pub value: Random) { ctor create() { this.value = random.new(9223372036854775807); } } declare global empty: Random = new Empty.create().value; declare service Probe { fn main() -> long { return random.next(empty); } }");
pair("typed-null", """
declare struct Empty(pub value: Random) { ctor create() {} } declare global empty: Random = new Empty.create().value;
declare struct Empty(pub value: Random) { ctor create() { this.value = random.new(9223372036854775807); } } declare global empty: Random = new Empty.create().value;
declare service Probe { fn main() -> long {
let values: array<Random> = [empty];
if let value = values[0] { return random.next(value); }