feat(pbs): add mutable lists and stable snapshot cursors
Implement SDK list<T> and Cursor<T> with trusted compiler bindings and generic storage publication across IR and PBX. Pin the runtime provider and include specs, conformance coverage, installed examples, and DSC-0090 lessons.
This commit is contained in:
+49
-3
@@ -41,6 +41,54 @@ class PbsGateUSdkInterfaceConformanceTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void listAndCursorUseDistributedSdkBodiesAndPrivateDefinitionScopes() throws IOException {
|
||||
var result=compileWorkspaceModule(tempDir.resolve("list-cursor"), """
|
||||
import { list, Cursor } from @sdk:collections;
|
||||
fn main() -> int {
|
||||
let capacity: int = 0;
|
||||
let values: list<int> = new list(capacity);
|
||||
values.add(20); values.insert(capacity, 22);
|
||||
let snapshot: Cursor<int> = values.cursor();
|
||||
values.clear();
|
||||
return snapshot.current() else 0;
|
||||
}
|
||||
[Frame] fn frame() -> void {let observed=main();}
|
||||
""", "pub fn main() -> int; pub fn frame() -> void;", 1, null);
|
||||
assertFalse(result.diagnostics().stream().anyMatch(d->d.getSeverity().isError()),result.diagnostics().stream().map(d->d.getCode()+":"+d.getMessage()).toList().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listApplicationsAndPrivateAdaptersRejectInvalidProjectUses() throws IOException {
|
||||
var cases=java.util.Map.of(
|
||||
"optional","let values:list<optional int>=new list<optional int>();",
|
||||
"grouped","let values:list<(optional int)>=new list<(optional int)>();",
|
||||
"arity","let values:list<int,long>=new list<int,long>();",
|
||||
"context","let values=new list();",
|
||||
"invariance","let values:list<long>=new list<int>();",
|
||||
"index","let values=new list<int>();let invalid=values[1.5];",
|
||||
"storage","let values=new list<int>();let exposed=values.storage;",
|
||||
"adapter","let values=new list<int>();let exposed=values._at(0);",
|
||||
"value-promotion","let values=new list<long>();let value:int=1;values.add(value);");
|
||||
for(var entry:cases.entrySet()) {
|
||||
var source="import {list} from @sdk:collections; [Frame] fn frame() -> void {"+entry.getValue()+"}";
|
||||
var result=compileWorkspaceModule(tempDir.resolve("list-negative-"+entry.getKey()),source,"pub fn frame() -> void;",1,null);
|
||||
assertTrue(result.diagnostics().hasErrors(),entry.getKey());
|
||||
}
|
||||
var homonym=new PbsFrontendCompiler();var diagnostics=DiagnosticSink.empty();
|
||||
homonym.compileFile(new FileId(200),"""
|
||||
declare struct list<T>(value:T) {}
|
||||
fn main() {let values=new list<int>(1);let invalid=values[0];}
|
||||
""",diagnostics,SourceKind.PROJECT);
|
||||
assertTrue(diagnostics.stream().anyMatch(d->d.getCode().equals("E_SEM_ARRAY_RECEIVER")));
|
||||
var copied=DiagnosticSink.empty();
|
||||
homonym.compileFile(new FileId(201),"""
|
||||
[SdkList] declare struct list<T>(value:T) {}
|
||||
[StoragePrimitive(name="indexGuard")] fn fake(index:long,limit:long) {}
|
||||
""",copied,SourceKind.PROJECT);
|
||||
assertTrue(copied.hasErrors(),"Project attributes cannot acquire SDK bindings");
|
||||
}
|
||||
|
||||
@Test
|
||||
void removedInputObjectsExplainTheirMigration() throws IOException {
|
||||
final var result = compileWorkspaceModule(tempDir.resolve("removed-input"), """
|
||||
@@ -296,9 +344,7 @@ class PbsGateUSdkInterfaceConformanceTest {
|
||||
"fn run() -> int { return 1; }",
|
||||
negativeSemanticsDiagnostics,
|
||||
SourceKind.SDK_INTERFACE);
|
||||
final var nonDeclarative = firstDiagnostic(negativeSemanticsDiagnostics,
|
||||
d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name()));
|
||||
assertStableDiagnosticIdentity(nonDeclarative, PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name(), DiagnosticPhase.STATIC_SEMANTICS);
|
||||
assertFalse(negativeSemanticsDiagnostics.hasErrors(), "SDK modules retain ordinary executable function bodies");
|
||||
|
||||
final var negativeLinkDiagnostics = DiagnosticSink.empty();
|
||||
final var invalidBarrelAst = PbsBarrelParser.parse(
|
||||
|
||||
+22
@@ -6,6 +6,28 @@ import p.studio.compiler.source.identifiers.FileId;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PbsGenericStructsTest {
|
||||
@Test void recursiveOptionalBindingsAndContractViewsKeepTheirDefinitionShapes() {
|
||||
accepts("""
|
||||
declare contract Reader<T> {fn read()->optional T;}
|
||||
declare struct Node<T>(pub value:T,pub next:optional Node<T>);
|
||||
declare struct Holder<T>(node:optional Node<T>) {
|
||||
fn read()->optional T {if let node=this.node {return some(node.value);}return none;}
|
||||
fn view()->Reader<T> {return this as Reader<T>;}
|
||||
}
|
||||
implements<T> Reader<T> for Holder<T> using owner {fn read()->optional T {return owner.read();}}
|
||||
fn main()->int {
|
||||
let value=new Holder<int>(some(new Node<int>(42,none)));
|
||||
return value.view().read() else 0;
|
||||
}
|
||||
""");
|
||||
accepts("""
|
||||
declare struct Cell<T>(values:array<T>) {
|
||||
fn set(value:T) {this.values[0]=value;}
|
||||
fn get()->optional T {return this.values[0];}
|
||||
}
|
||||
fn main()->int {let cell=new Cell<int>([0]);cell.set(42);return cell.get() else 0;}
|
||||
""");
|
||||
}
|
||||
private static final String BOX = """
|
||||
declare struct Box<T>(value: T) {
|
||||
fn get() -> T { return this.value; }
|
||||
|
||||
+2
-5
@@ -109,7 +109,7 @@ class PbsInterfaceModuleSemanticsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectExecutableDeclarationsInInterfaceModule() {
|
||||
void shouldAllowOrdinaryExecutableSdkDeclarations() {
|
||||
final var source = """
|
||||
fn run() -> int { return 1; }
|
||||
|
||||
@@ -123,10 +123,7 @@ class PbsInterfaceModuleSemanticsTest {
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics, SourceKind.SDK_INTERFACE);
|
||||
|
||||
final var nonDeclarativeCount = diagnostics.stream()
|
||||
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name()))
|
||||
.count();
|
||||
assertTrue(nonDeclarativeCount >= 2);
|
||||
assertFalse(diagnostics.hasErrors(), diagnostics.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.FileId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PbsListCursorEditorialTest {
|
||||
@Test void concreteApiHidesStorageAndKeepsSdkDefinitionOrigins() throws Exception {
|
||||
var diagnostics=DiagnosticSink.empty();var sdkId=new FileId(1);
|
||||
String sdk;
|
||||
try(var stream=getClass().getResourceAsStream("/game/stdlib/1/sdk/collections/main.pbs")) {
|
||||
assertNotNull(stream);sdk=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
|
||||
}
|
||||
var library=PbsParser.parse(PbsLexer.lex(sdk,sdkId,diagnostics),sdkId,diagnostics,PbsParser.ParseMode.INTERFACE_MODULE);
|
||||
var list=library.topDecls().stream().filter(d->d instanceof PbsAst.StructDecl s&&s.name().equals("list")).findFirst().orElseThrow();
|
||||
var cursor=library.topDecls().stream().filter(d->d instanceof PbsAst.ContractDecl c&&c.name().equals("Cursor")).findFirst().orElseThrow();
|
||||
var source="""
|
||||
import {list as Values,Cursor} from @sdk:collections;
|
||||
fn sample(values:Values<int>,cursor:Cursor<int>)->optional int {
|
||||
let created:Values<int>=new Values(0);
|
||||
values.add(42); cursor.hasNext(); return cursor.current();
|
||||
}
|
||||
""";
|
||||
var id=new FileId(2);var ast=PbsParser.parse(PbsLexer.lex(source,id,diagnostics),id,diagnostics);
|
||||
assertFalse(diagnostics.hasErrors());
|
||||
var supplemental=ReadOnlyList.<PbsAst.TopDecl>from(new PbsAst.TypeBindingDecl("Values",list,ast.span()),cursor);
|
||||
var service=new PbsEditorialSupportService();int member=source.indexOf("values.add")+7;
|
||||
var typeHover=service.hover(source,ast,supplemental,source.indexOf("Values<int>")).orElseThrow();
|
||||
assertFalse(typeHover.signatures().stream().anyMatch(s->s.label().startsWith("_")));
|
||||
var constructors=service.signatureHelp(source,ast,supplemental,source.indexOf("new Values(0")+11).orElseThrow().signatures();
|
||||
assertEquals(2,constructors.size());
|
||||
assertTrue(constructors.stream().allMatch(s->!s.label().contains("storage")&&!s.label().contains("$generic$")));
|
||||
var completions=service.completion(source,ast,supplemental,member);
|
||||
assertTrue(completions.stream().anyMatch(c->c.label().equals("add")&&c.detail().contains("int")));
|
||||
assertFalse(completions.stream().anyMatch(c->java.util.Set.of("storage","length","_at","_put").contains(c.label())));
|
||||
var definition=service.definition(source,ast,supplemental,member);
|
||||
assertEquals(sdkId,definition.getFirst().span().getFileId());
|
||||
int current=source.indexOf("cursor.current")+7;
|
||||
assertTrue(service.hover(source,ast,supplemental,current).orElseThrow().signatures().getFirst().label().contains("optional int"));
|
||||
var methods=service.completion(source,ast,supplemental,current);
|
||||
assertEquals(java.util.Set.of("hasNext","next","current"),methods.stream().map(c->c.label()).collect(java.util.stream.Collectors.toSet()));
|
||||
assertFalse(service.definition(source,ast,supplemental,current).isEmpty());
|
||||
var rename=service.rename(source,ast,supplemental,source.indexOf("values:Values"),"items");
|
||||
assertTrue(rename.allowed());
|
||||
assertEquals(2,rename.sites().size());
|
||||
assertTrue(service.references(source,ast,supplemental,source.indexOf("values:Values")).size()>=2);
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -654,7 +654,7 @@ class PBSFrontendPhaseServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSkipIrLoweringForSourcesMarkedAsSdkInterface() throws IOException {
|
||||
void shouldLowerOrdinaryExecutableSdkFunctions() throws IOException {
|
||||
final var projectRoot = tempDir.resolve("project-sdk-interface");
|
||||
final var sourceRoot = projectRoot.resolve("src");
|
||||
final var modulePath = sourceRoot.resolve("gfx");
|
||||
@@ -691,9 +691,9 @@ class PBSFrontendPhaseServiceTest {
|
||||
LogAggregator.empty(),
|
||||
BuildingIssueSink.empty());
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name())));
|
||||
assertFalse(diagnostics.hasErrors(), diagnostics.stream().map(d -> d.getMessage()).toList().toString());
|
||||
assertEquals(0, irBackend.getFunctions().size());
|
||||
assertEquals(1, irBackend.getExecutableFunctions().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user