feat(pbs): add parametric arrays and concrete generic contracts
Support explicit contract type parameters, canonical nominal applications, recursive substitution, concrete implementations and instantiated editor views. Complete parametric Array syntax and contextual construction integration. Preserve type origins across imports and reexports, fix trait argument stack order, and relocate global slots before lifecycle assembly. Publish specs, installed examples and consolidated discussion lessons. Validate frontend, common pipeline, architecture, LSP and installed knowledge gates, plus the full production VM conformance corpus with 14 generic fixtures.
This commit is contained in:
+2
-2
@@ -41,7 +41,7 @@ class PbsClosuresTest {
|
||||
let counter = new Counter(40);
|
||||
let increment = fn [counter]() -> void { counter.value += 1; };
|
||||
let read = fn [counter]() -> int { return counter.value; };
|
||||
let reads: array (Fn() -> int) = [read];
|
||||
let reads: array<(Fn() -> int)> = [read];
|
||||
increment(); increment();
|
||||
if let f = reads[0] { return f(); }
|
||||
return 999;
|
||||
@@ -156,7 +156,7 @@ class PbsClosuresTest {
|
||||
@Test void closuresSurviveExistingFrameLifecycleAndAreCollectible() throws Exception {
|
||||
var source = """
|
||||
declare struct Counter(pub mut value: int);
|
||||
declare global callbacks: array (Fn() -> int) = new array(1);
|
||||
declare global callbacks: array<(Fn() -> int)> = new array(1);
|
||||
declare global frames: int = 0;
|
||||
[Frame]
|
||||
fn frame() -> void {
|
||||
|
||||
+234
@@ -0,0 +1,234 @@
|
||||
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.ast.PbsAst;
|
||||
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 PbsGenericContractsTest {
|
||||
private static final String ECHO = "declare contract Echo<T> { fn echo(value: T) -> T; } declare service Strategy {}\n";
|
||||
|
||||
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 supplemental = ReadOnlyList.<PbsAst.TopDecl>empty();
|
||||
var reserved = IRReservedMetadata.empty();
|
||||
if (name.equals("native")) {
|
||||
var nativeId = new FileId(100);
|
||||
var nativeSource = "[BuiltinType(name = \"vec2\", version = 1)] declare builtin type Vector(pub x: float, pub y: float) {}";
|
||||
var nativeAst = PbsParser.parse(PbsLexer.lex(nativeSource, nativeId, diagnostics), nativeId, diagnostics, PbsParser.ParseMode.INTERFACE_MODULE);
|
||||
supplemental = nativeAst.topDecls();
|
||||
reserved = new PbsReservedMetadataExtractor().extract(nativeAst, SourceKind.SDK_INTERFACE);
|
||||
}
|
||||
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(),
|
||||
supplemental, ReadOnlyList.empty(), ReadOnlyList.empty(), reserved, java.util.Map.of());
|
||||
assertFalse(diagnostics.hasErrors(), () -> name + ": " + diagnostics.stream().map(d -> d.getCode()+":"+d.getMessage()).toList().toString());
|
||||
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);
|
||||
var directory = Path.of(System.getProperty("pbs.registryConformanceDirectory"));
|
||||
Files.createDirectories(directory);
|
||||
Files.writeString(directory.resolve("pbs-generic-contract-"+name+".ir.txt"), file.executableFunctions().stream()
|
||||
.map(f -> f.callableName()+" "+f.metadata()+"\n"+f.instructions().stream().map(Object::toString).collect(java.util.stream.Collectors.joining("\n")))
|
||||
.collect(java.util.stream.Collectors.joining("\n\n")));
|
||||
new BytecodePreloadVerifierService().verify(module);
|
||||
Files.write(directory.resolve("pbs-generic-contract-"+name+".pbx"), module.serialize());
|
||||
return module;
|
||||
}
|
||||
|
||||
@Test void concreteAndGenericContractsHaveTheSameBehavior() throws Exception {
|
||||
for (var generic : new boolean[]{true, false}) {
|
||||
var declaration = generic ? "declare contract Identity<T> { fn hash(e: T) -> int; fn equals(a: T, b: T) -> bool; }" :
|
||||
"declare contract IntContract { fn hash(e: int) -> int; fn equals(a: int, b: int) -> bool; }";
|
||||
var application = generic ? "Identity<int>" : "IntContract";
|
||||
compile(generic ? "identity" : "manual", declaration + "declare service Strategy {} implements " + application + " for Strategy using s {" +
|
||||
"fn hash(e: int) -> int { return e; } fn equals(a: int, b: int) -> bool { return a == b; } } " +
|
||||
"declare service Probe { fn main() -> int { let identity: " + application + " = Strategy; if identity.equals(7, 7) { return identity.hash(42); } return 0; } }");
|
||||
}
|
||||
}
|
||||
|
||||
@Test void oneOwnerDispatchesIndependentApplicationsAndExplicitViews() throws Exception {
|
||||
compile("multiple", ECHO + """
|
||||
implements Echo<int> for Strategy using s { fn echo(value: int) -> int { return value; } }
|
||||
implements Echo<bool> for Strategy using s { fn echo(value: bool) -> bool { return value; } }
|
||||
declare service Probe { fn main() -> int {
|
||||
let ints: Echo<int> = Strategy;
|
||||
let flags = ints as Echo<bool>;
|
||||
if flags.echo(true) { return ints.echo(42); }
|
||||
return 0;
|
||||
} }
|
||||
""");
|
||||
}
|
||||
|
||||
@Test void composedArgumentsPreserveCompleteGroups() throws Exception {
|
||||
compile("tuple", """
|
||||
declare contract Convert<A, B> { fn convert(value: A) -> B; }
|
||||
declare service Strategy {}
|
||||
implements Convert<array<int>, (left: int, right: int)> for Strategy using s {
|
||||
fn convert(value: array<int>) -> (left: int, right: int) { return (left: value[0] else 0, right: 2); }
|
||||
}
|
||||
declare service Probe { fn main() -> int {
|
||||
let c: Convert<array<int>, (left: int, right: int)> = Strategy;
|
||||
let pair = c.convert([40]); return pair.left + pair.right;
|
||||
} }
|
||||
""");
|
||||
compile("optional", ECHO + """
|
||||
implements Echo<optional int> for Strategy using s { fn echo(value: optional int) -> optional int { return value; } }
|
||||
declare service Probe { fn main() -> int {
|
||||
let c: Echo<optional int> = Strategy;
|
||||
if let wrong = c.echo(none) { return 0; }
|
||||
return c.echo(some(42)) else 0;
|
||||
} }
|
||||
""");
|
||||
compile("function", ECHO + """
|
||||
implements Echo<Fn(int) -> int> for Strategy using s { fn echo(value: Fn(int) -> int) -> Fn(int) -> int { return value; } }
|
||||
fn add(value: int) -> int { return value + 2; }
|
||||
declare service Probe { fn main() -> int {
|
||||
let c: Echo<Fn(int) -> int> = Strategy;
|
||||
let f = c.echo(add); return f(40);
|
||||
} }
|
||||
""");
|
||||
compile("contract", ECHO + """
|
||||
declare contract Read<T> { fn read() -> T; }
|
||||
implements Read<int> for Strategy using s { fn read() -> int { return 42; } }
|
||||
implements Echo<Read<int>> for Strategy using s { fn echo(value: Read<int>) -> Read<int> { return value; } }
|
||||
declare service Probe { fn main() -> int {
|
||||
let reader: Read<int> = Strategy;
|
||||
let c: Echo<Read<int>> = Strategy;
|
||||
return c.echo(reader).read();
|
||||
} }
|
||||
""");
|
||||
compile("native", ECHO + """
|
||||
implements Echo<Vector> for Strategy using s { fn echo(value: Vector) -> Vector { return value; } }
|
||||
declare service Probe { fn main() -> int {
|
||||
let c: Echo<Vector> = Strategy;
|
||||
let value = c.echo(new Vector(20.0, 22.0));
|
||||
return (value.x + value.y) as int;
|
||||
} }
|
||||
""");
|
||||
}
|
||||
|
||||
@Test void finiteRecursionPhantomArgumentsAndReceiverIdentityExecute() throws Exception {
|
||||
compile("recursive", """
|
||||
declare contract Node<T> { fn next() -> Node<T>; fn value() -> T; }
|
||||
declare service Strategy {}
|
||||
implements Node<int> for Strategy using s {
|
||||
fn next() -> Node<int> { return Strategy; }
|
||||
fn value() -> int { return 42; }
|
||||
}
|
||||
declare service Probe { fn main() -> int { let node: Node<int> = Strategy; return node.next().value(); } }
|
||||
""");
|
||||
compile("phantom", """
|
||||
declare contract Marker<T> { fn answer() -> int; }
|
||||
declare service Strategy {}
|
||||
implements Marker<array<int>> for Strategy using s { fn answer() -> int { return 42; } }
|
||||
implements Marker<array<bool>> for Strategy using s { fn answer() -> int { return 42; } }
|
||||
declare service Probe { fn main() -> int {
|
||||
let first: Marker<array<int>> = Strategy;
|
||||
let second: Marker<array<bool>> = Strategy;
|
||||
return first.answer() + second.answer() - 42;
|
||||
} }
|
||||
""");
|
||||
compile("receiver", """
|
||||
declare contract Read<T> { fn read() -> T; }
|
||||
declare struct Counter(pub mut value: int) { fn increment() { this.value += 1; } }
|
||||
implements Read<int> for Counter using counter { fn read() -> int { return counter.value; } }
|
||||
declare service Probe { fn main() -> int {
|
||||
let counter = new Counter(41);
|
||||
let reader: Read<int> = counter;
|
||||
counter.increment(); return reader.read();
|
||||
} }
|
||||
""");
|
||||
}
|
||||
|
||||
@Test void contractCaptureSurvivesARealGcSafepoint() throws Exception {
|
||||
compile("gc", """
|
||||
declare contract Read<T> { fn read() -> T; }
|
||||
declare struct Counter(pub value: int);
|
||||
implements Read<int> for Counter using counter { fn read() -> int { return counter.value; } }
|
||||
fn capture() -> Fn() -> int {
|
||||
let reader: Read<int> = new Counter(42);
|
||||
return fn [reader]() -> int { return reader.read(); };
|
||||
}
|
||||
declare service Probe {
|
||||
fn garbage() { let discarded: array<int> = new array(1); }
|
||||
fn collect() { let marker = 314159; }
|
||||
fn main() -> int {
|
||||
let read = capture();
|
||||
for i: int from 0 until 1100 { Probe.garbage(); }
|
||||
Probe.collect(); return read();
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
@Test void installedContractExampleExecutes() throws Exception {
|
||||
try (var stream = PbsFrontendCompiler.class.getResourceAsStream("/pbs-knowledge/examples/game/1/src/contracts.pbs")) {
|
||||
assertNotNull(stream);
|
||||
compile("installed", new String(stream.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8)
|
||||
+ "\ndeclare service Probe { fn main() -> int { return contractExample(); } }");
|
||||
}
|
||||
}
|
||||
|
||||
@Test void moduleOriginsAliasesAndExportedStrategiesExecute(@org.junit.jupiter.api.io.TempDir Path root) throws Exception {
|
||||
Files.createDirectories(root.resolve("src/policy"));
|
||||
Files.createDirectories(root.resolve("src/facade"));
|
||||
Files.createDirectories(root.resolve("src/left"));
|
||||
Files.createDirectories(root.resolve("src/right"));
|
||||
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;");
|
||||
for (var name : new String[]{"left", "right"}) {
|
||||
Files.writeString(root.resolve("src/"+name+"/mod.barrel"), "pub struct Enemy;");
|
||||
Files.writeString(root.resolve("src/"+name+"/main.pbs"), "declare struct Enemy(pub hp: int);");
|
||||
}
|
||||
Files.writeString(root.resolve("src/policy/mod.barrel"), "pub contract Identity<T>; pub contract Marker<T>; pub service Strategy;");
|
||||
Files.writeString(root.resolve("src/facade/mod.barrel"), "pub contract Identity<T>; pub contract Marker<T>;");
|
||||
Files.writeString(root.resolve("src/facade/main.pbs"), "import { Identity, Marker } from @app:policy;");
|
||||
Files.writeString(root.resolve("src/policy/main.pbs"), """
|
||||
import { Enemy as A } from @app:left;
|
||||
import { Enemy as B } from @app:right;
|
||||
declare contract Identity<T> { fn hash(value: T) -> int; }
|
||||
declare contract Marker<T> { fn tag() -> int; }
|
||||
declare service Strategy {}
|
||||
implements Identity<int> for Strategy using s { fn hash(value: int) -> int { return value; } }
|
||||
implements Marker<A> for Strategy using s { fn tag() -> int { return 20; } }
|
||||
implements Marker<B> for Strategy using s { fn tag() -> int { return 22; } }
|
||||
""");
|
||||
Files.writeString(root.resolve("src/main.pbs"), """
|
||||
import { Identity as Policy, Marker as Tag } from @app:facade;
|
||||
import { Strategy as Selected } from @app:policy;
|
||||
import { Enemy as Left } from @app:left;
|
||||
import { Enemy as Right } from @app:right;
|
||||
[Frame]
|
||||
fn frame() -> void {
|
||||
let identity: Policy<int> = Selected;
|
||||
let left: Tag<Left> = Selected;
|
||||
let right: Tag<Right> = Selected;
|
||||
if identity.hash(42) != 42 { let fail = 1 / 0; }
|
||||
if left.tag() + right.tag() != 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.write(Path.of(System.getProperty("pbs.registryConformanceDirectory"), "pbs-generic-contract-imports.pbx"), result.bytecodeBytes());
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -127,7 +127,7 @@ class PbsRuntimeRandomTest {
|
||||
let rng = random.new(42 as long);
|
||||
saved = echo(rng);
|
||||
let box = new Box(saved);
|
||||
let values: array Random = [box.value];
|
||||
let values: array<Random> = [box.value];
|
||||
let draw = capture(rng);
|
||||
let a = random.next(saved);
|
||||
let b = random.next(box.value);
|
||||
@@ -188,7 +188,7 @@ class PbsRuntimeRandomTest {
|
||||
pair("typed-null", """
|
||||
declare struct Empty(pub value: Random) { ctor create() {} } declare global empty: Random = new Empty.create().value;
|
||||
declare service Probe { fn main() -> long {
|
||||
let values: array Random = [empty];
|
||||
let values: array<Random> = [empty];
|
||||
if let value = values[0] { return random.next(value); }
|
||||
return 99;
|
||||
} }
|
||||
@@ -232,9 +232,9 @@ class PbsRuntimeRandomTest {
|
||||
@Test void referencesSurviveCollection() throws Exception {
|
||||
pair("gc", """
|
||||
declare struct Box(pub value: Random);
|
||||
fn make() -> (items: array Random, draw: Fn() -> long, box: Box) {
|
||||
fn make() -> (items: array<Random>, draw: Fn() -> long, box: Box) {
|
||||
let rng = random.new(42 as long);
|
||||
let items: array Random = [rng];
|
||||
let items: array<Random> = [rng];
|
||||
let draw = fn [rng]() -> long { return random.next(rng); };
|
||||
return (items: items, draw: draw, box: new Box(rng));
|
||||
}
|
||||
|
||||
+65
-29
@@ -79,7 +79,7 @@ class PbsStructuredValuesTest {
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let count: int = 3;
|
||||
let values: array int = new array(count);
|
||||
let values: array<int> = new array(count);
|
||||
let index: int = 1;
|
||||
values[index] = 42;
|
||||
let answer = values[index] else 0;
|
||||
@@ -94,7 +94,7 @@ class PbsStructuredValuesTest {
|
||||
compile("array-nested", """
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let groups: array[2] int = new array(3);
|
||||
let groups: array<array<int>> = new array(3);
|
||||
groups[0] = [10, 42];
|
||||
if let group = groups[0] {
|
||||
if let value = group[1] { return value; }
|
||||
@@ -128,7 +128,7 @@ class PbsStructuredValuesTest {
|
||||
implements Target for Enemy using enemy { fn health() -> int { return enemy.health(); } }
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let enemies: array Enemy = new array(2);
|
||||
let enemies: array<Enemy> = new array(2);
|
||||
let enemy = new Enemy.create(50);
|
||||
enemies[0] = enemy;
|
||||
if let selected = enemies[0] { selected.damage(8); }
|
||||
@@ -166,7 +166,7 @@ class PbsStructuredValuesTest {
|
||||
declare struct Pair(pub mut value: (left: int, right: int)) {}
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let values: array (left: int, right: int) = [(left: 20, right: 22)];
|
||||
let values: array<(left: int, right: int)> = [(left: 20, right: 22)];
|
||||
if let pair = values[0] {
|
||||
let holder = new Pair(pair);
|
||||
holder.value = (left: 21, right: 21);
|
||||
@@ -180,7 +180,7 @@ class PbsStructuredValuesTest {
|
||||
declare service Probe {
|
||||
fn absent() -> optional int {}
|
||||
fn main() -> int {
|
||||
let values: array int = [0];
|
||||
let values: array<int> = [0];
|
||||
if values[0].hasSome() {
|
||||
if Probe.absent().hasNone() { return 42; }
|
||||
}
|
||||
@@ -191,7 +191,7 @@ class PbsStructuredValuesTest {
|
||||
compile("optional-lazy", """
|
||||
declare service Probe {
|
||||
fn forbidden() -> int {
|
||||
let empty: array int = [];
|
||||
let empty: array<int> = [];
|
||||
return empty[0] else 999;
|
||||
}
|
||||
fn main() -> int {
|
||||
@@ -204,10 +204,10 @@ class PbsStructuredValuesTest {
|
||||
@Test void contextualArraysAndAliasReachProductionPbx() throws Exception {
|
||||
compile("array-context", """
|
||||
declare service Probe {
|
||||
fn make() -> array int { return [20, 22]; }
|
||||
fn sum(values: array int) -> int { return (values[0] else 0) + (values[1] else 0); }
|
||||
fn make() -> array<int> { return [20, 22]; }
|
||||
fn sum(values: array<int>) -> int { return (values[0] else 0) + (values[1] else 0); }
|
||||
fn main() -> int {
|
||||
let values: array int = { [1, 2] };
|
||||
let values: array<int> = { [1, 2] };
|
||||
let alias = values;
|
||||
alias[0] = 20;
|
||||
alias[1] = 22;
|
||||
@@ -249,7 +249,7 @@ class PbsStructuredValuesTest {
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let counter = new Counter(0);
|
||||
let values: array int = new array(2);
|
||||
let values: array<int> = new array(2);
|
||||
values[counter.next()] = counter.next();
|
||||
if counter.count != 2 { return 999; }
|
||||
if (values[0] else 99) != 1 { return 998; }
|
||||
@@ -263,16 +263,16 @@ class PbsStructuredValuesTest {
|
||||
}
|
||||
@Test void boundsFixturesReachProductionPbx() throws Exception {
|
||||
for (var entry : java.util.Map.of(
|
||||
"negative-index", "let a: array int = new array(1); return a[-1] else 42;",
|
||||
"past-end", "let a: array int = new array(1); return a[1] else 42;",
|
||||
"negative-length", "let size: int = -1; let a: array int = new array(size); return 42;",
|
||||
"large-length", "let size: long = 4294967296; let a: array int = new array(size); return 42;"
|
||||
"negative-index", "let a: array<int> = new array(1); return a[-1] else 42;",
|
||||
"past-end", "let a: array<int> = new array(1); return a[1] else 42;",
|
||||
"negative-length", "let size: int = -1; let a: array<int> = new array(size); return 42;",
|
||||
"large-length", "let size: long = 4294967296; let a: array<int> = new array(size); return 42;"
|
||||
).entrySet()) compile(entry.getKey(), "declare service Probe { fn main() -> int { " + entry.getValue() + " } }");
|
||||
compile("long-boundaries", """
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let size: long = 2;
|
||||
let a: array int = new array(size);
|
||||
let a: array<int> = new array(size);
|
||||
let first: long = 0;
|
||||
let last: int = 1;
|
||||
a[first] = 20;
|
||||
@@ -287,12 +287,12 @@ class PbsStructuredValuesTest {
|
||||
compile("gc-roots", """
|
||||
declare struct Enemy(pub hp: int) {}
|
||||
declare service Probe {
|
||||
fn garbage() { let discarded: array int = new array(1); }
|
||||
fn garbage() { let discarded: array<int> = new array(1); }
|
||||
fn gcCollect() { let marker = 314159; }
|
||||
fn main() -> int {
|
||||
let keep: array Enemy = [new Enemy(42)];
|
||||
let tuples: array (left: int, right: int) = [(left: 20, right: 22)];
|
||||
let names: array str = new array(2);
|
||||
let keep: array<Enemy> = [new Enemy(42)];
|
||||
let tuples: array<(left: int, right: int)> = [(left: 20, right: 22)];
|
||||
let names: array<str> = new array(2);
|
||||
names[0] = "enemy";
|
||||
for i: int from 0 until 1100 { Probe.garbage(); }
|
||||
Probe.gcCollect();
|
||||
@@ -314,7 +314,7 @@ class PbsStructuredValuesTest {
|
||||
declare builtin type Vector(pub x: float, pub y: float) {}
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let vectors: array Vector = [new Vector(20.0, 22.0)];
|
||||
let vectors: array<Vector> = [new Vector(20.0, 22.0)];
|
||||
let vector = vectors[0] else new Vector(0.0, 0.0);
|
||||
return (vector.x + vector.y) as int;
|
||||
}
|
||||
@@ -327,7 +327,7 @@ class PbsStructuredValuesTest {
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let enemy = new Enemy(42);
|
||||
let targets: array Target = [enemy as Target];
|
||||
let targets: array<Target> = [enemy as Target];
|
||||
if let target = targets[0] { return target.health(); }
|
||||
return 999;
|
||||
}
|
||||
@@ -348,7 +348,7 @@ class PbsStructuredValuesTest {
|
||||
String clientSource = """
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let values: array Enemy = [new Enemy.create(42)];
|
||||
let values: array<Enemy> = [new Enemy.create(42)];
|
||||
let first = Source;
|
||||
let second = Source;
|
||||
if first != second { return 999; }
|
||||
@@ -398,8 +398,8 @@ class PbsStructuredValuesTest {
|
||||
}
|
||||
@Test void globalArraysAreInitializedBeforeUse() throws Exception {
|
||||
compile("global-array", """
|
||||
declare global values: array int = [20, 22];
|
||||
declare global empty: array int = new array(3);
|
||||
declare global values: array<int> = [20, 22];
|
||||
declare global empty: array<int> = new array(3);
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
if empty.length != 3 as long { return 999; }
|
||||
@@ -445,23 +445,59 @@ class PbsStructuredValuesTest {
|
||||
compile("inline-integers", """
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let values: array int = [10, 20, 30];
|
||||
let values: array<int> = [10, 20, 30];
|
||||
return (values.length as int) + 39;
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
@Test void parametricConstructorsReachProductionPbx() throws Exception {
|
||||
compile("array-parametric", """
|
||||
declare global saved: array<int>=new array<>(1);
|
||||
declare service Probe {
|
||||
fn make() -> array<int> { return new array<>(1); }
|
||||
fn size(values: array<int>) -> int { return values.length as int; }
|
||||
fn main() -> int {
|
||||
let inferred = new array<int>(2);
|
||||
let alias = inferred;
|
||||
alias[0] = 20;
|
||||
let nested: array<array<int>>=new array<>(2);
|
||||
if let unexpected = nested[0] { return 999; }
|
||||
nested[0] = inferred;
|
||||
let explicitNested = new array<array<int>>(1);
|
||||
explicitNested[0] = new array<int>(1);
|
||||
saved = new array<>(1);
|
||||
saved[0] = 18;
|
||||
let fromReturn = Probe.make();
|
||||
let count = Probe.size(new array<>(1)) + Probe.size(new array(1));
|
||||
if 2 >= 1 {
|
||||
return (inferred[0] else 0) + (saved[0] else 0) + count
|
||||
+ (fromReturn.length as int) + (explicitNested.length as int);
|
||||
}
|
||||
return 999;
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
@Test void invalidArraySurfacesAreRejected() {
|
||||
for(var body : new String[] {
|
||||
"let values = new array<>(3);",
|
||||
"let values = new array<Missing>(3);",
|
||||
"let values = new array<optional int>(3);",
|
||||
"let values = new array<void>(3);",
|
||||
"let values: array<int> = new array<bool>(3);",
|
||||
"let values: array int = new array(3);", // removed syntax
|
||||
"let values: array<int>[2] = new array(3);", // removed depth notation
|
||||
"let values = [1, 2];", "let values = [];", "let values = new array(3);",
|
||||
"let values: array (optional int) = new array(3);",
|
||||
"let values: array<(optional int)> = new array(3);",
|
||||
"let values: array[2147483647] int = new array(3);", "let values: array[0] int = new array(3);", "let values: array[-1] int = new array(3);",
|
||||
"let values: array int = new array(3); let x = values[true];",
|
||||
"let values: array int = new array(3); values[0] = true;",
|
||||
"let values: array<int> = new array(3); let x = values[true];",
|
||||
"let values: array<int> = new array(3); values[0] = true;",
|
||||
"if let value = 42 { let x = value; }",
|
||||
"let value: optional int = some(1); if let found = value {} let bad = found;",
|
||||
"let value: optional int = some(1); if let found = value {} else { let bad = found; }",
|
||||
"let values: array int = new array(3); let wrong: array bool = values;"
|
||||
"let values: array<int> = new array(3); let wrong: array<bool> = values;"
|
||||
}) {
|
||||
var diagnostics=DiagnosticSink.empty();
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0),"fn main() { "+body+" }",diagnostics);
|
||||
|
||||
Reference in New Issue
Block a user