implements PR-19.6 lifecycle and initallowed semantics

This commit is contained in:
2026-03-26 19:28:22 +00:00
parent 2003fc749e
commit 7fc38010a2
8 changed files with 607 additions and 2 deletions
@@ -203,6 +203,47 @@ class PbsSemanticsDeclarationsTest {
assertEquals(4, invalidCount);
}
@Test
void shouldRejectInvalidLifecycleSignaturesAndMultipleFileInitMarkers() {
final var source = """
[Init]
fn boot(v: int) -> void { return; }
[Init]
fn warmup() -> void { return; }
[Frame]
fn frame() { return; }
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
final var lifecycleSignatureCount = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_LIFECYCLE_SIGNATURE.name()))
.count();
final var duplicateInitCount = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_FILE_INIT.name()))
.count();
assertEquals(2, lifecycleSignatureCount);
assertEquals(1, duplicateInitCount);
}
@Test
void shouldRejectInitAllowedOutsideHostSignatures() {
final var source = """
[InitAllowed]
declare const LIMIT: int = 1;
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_RESERVED_ATTRIBUTE_TARGET.name())));
}
@Test
void shouldAllowSelfInStructServiceMethodsAndCtors() {
final var source = """
@@ -1227,6 +1227,199 @@ class PBSFrontendPhaseServiceTest {
assertEquals(2, cycleCount, diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
}
@Test
void shouldRejectDuplicateFrameMarkersAcrossExecutableProject() throws IOException {
final var projectRoot = tempDir.resolve("project-duplicate-frame-markers");
final var sourceRoot = projectRoot.resolve("src");
final var moduleAPath = sourceRoot.resolve("a");
final var moduleBPath = sourceRoot.resolve("b");
Files.createDirectories(moduleAPath);
Files.createDirectories(moduleBPath);
final var sourceA = moduleAPath.resolve("source.pbs");
final var barrelA = moduleAPath.resolve("mod.barrel");
Files.writeString(sourceA, """
[Frame]
fn frame_a() -> void { return; }
""");
Files.writeString(barrelA, "pub fn frame_a() -> void;");
final var sourceB = moduleBPath.resolve("source.pbs");
final var barrelB = moduleBPath.resolve("mod.barrel");
Files.writeString(sourceB, """
[Frame]
fn frame_b() -> void { return; }
""");
Files.writeString(barrelB, "pub fn frame_b() -> void;");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final var projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("app")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
.build());
registerFile(projectId, projectRoot, sourceA, fileTable);
registerFile(projectId, projectRoot, barrelA, fileTable);
registerFile(projectId, projectRoot, sourceB, fileTable);
registerFile(projectId, projectRoot, barrelB, fileTable);
final var ctx = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))));
final var diagnostics = DiagnosticSink.empty();
new PBSFrontendPhaseService().compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
final var duplicateFrameCount = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_PROJECT_FRAME.name()))
.count();
assertEquals(1, duplicateFrameCount, diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
}
@Test
void shouldRejectHostCallsInInitWithoutInitAllowedAndAcceptWhenPresent() throws IOException {
final var projectRoot = tempDir.resolve("project-init-host-calls");
final var sourceRoot = projectRoot.resolve("src");
Files.createDirectories(sourceRoot);
final var deniedSource = sourceRoot.resolve("denied.pbs");
final var deniedBarrel = sourceRoot.resolve("mod.barrel");
Files.writeString(deniedSource, """
import { Gfx } from @sdk:gfx;
[Init]
fn boot() -> void {
Gfx.clear();
}
[Frame]
fn frame() -> void { return; }
""");
Files.writeString(deniedBarrel, """
pub fn boot() -> void;
pub fn frame() -> void;
""");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final var projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("app")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
.build());
registerFile(projectId, projectRoot, deniedSource, fileTable);
registerFile(projectId, projectRoot, deniedBarrel, fileTable);
final var sdkModuleDenied = new StdlibModuleSource(
"sdk",
ReadOnlyList.wrap(List.of("gfx")),
ReadOnlyList.wrap(List.of(new StdlibModuleSource.SourceFile(
"gfx.pbs",
"""
declare host Gfx {
[Host(module = "gfx", name = "clear", version = 1)]
fn clear() -> void;
}
"""))),
"pub host Gfx;");
final var deniedFrontendService = new PBSFrontendPhaseService(
resolverForMajor(11, sdkModuleDenied),
new InterfaceModuleLoader(3_200_000));
final var deniedCtx = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))),
11);
final var deniedDiagnostics = DiagnosticSink.empty();
deniedFrontendService.compile(
deniedCtx,
deniedDiagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(deniedDiagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_INIT_HOST_CALL_NOT_ALLOWED.name())),
deniedDiagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
final var allowedProjectRoot = tempDir.resolve("project-init-host-calls-allowed");
final var allowedSourceRoot = allowedProjectRoot.resolve("src");
Files.createDirectories(allowedSourceRoot);
final var allowedSource = allowedSourceRoot.resolve("main.pbs");
final var allowedBarrel = allowedSourceRoot.resolve("mod.barrel");
Files.writeString(allowedSource, """
import { Gfx } from @sdk:gfx;
[Init]
fn boot() -> void {
Gfx.clear();
}
[Frame]
fn frame() -> void { return; }
""");
Files.writeString(allowedBarrel, """
pub fn boot() -> void;
pub fn frame() -> void;
""");
final var allowedProjectTable = new ProjectTable();
final var allowedFileTable = new FileTable(1);
final var allowedProjectId = allowedProjectTable.register(ProjectDescriptor.builder()
.rootPath(allowedProjectRoot)
.name("app")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(allowedSourceRoot)))
.build());
registerFile(allowedProjectId, allowedProjectRoot, allowedSource, allowedFileTable);
registerFile(allowedProjectId, allowedProjectRoot, allowedBarrel, allowedFileTable);
final var sdkModuleAllowed = new StdlibModuleSource(
"sdk",
ReadOnlyList.wrap(List.of("gfx")),
ReadOnlyList.wrap(List.of(new StdlibModuleSource.SourceFile(
"gfx.pbs",
"""
declare host Gfx {
[Host(module = "gfx", name = "clear", version = 1)]
[InitAllowed]
fn clear() -> void;
}
"""))),
"pub host Gfx;");
final var allowedFrontendService = new PBSFrontendPhaseService(
resolverForMajor(12, sdkModuleAllowed),
new InterfaceModuleLoader(3_300_000));
final var allowedCtx = new FrontendPhaseContext(
allowedProjectTable,
allowedFileTable,
new BuildStack(ReadOnlyList.wrap(List.of(allowedProjectId))),
12);
final var allowedDiagnostics = DiagnosticSink.empty();
allowedFrontendService.compile(
allowedCtx,
allowedDiagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(allowedDiagnostics.stream().noneMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_INIT_HOST_CALL_NOT_ALLOWED.name())),
allowedDiagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
}
private void registerFile(
final ProjectId projectId,
final Path projectRoot,