implements PR031

This commit is contained in:
2026-03-24 13:42:23 +00:00
parent 495104db6d
commit f7abaf27d4
4 changed files with 224 additions and 6 deletions
@@ -8,6 +8,7 @@ import p.studio.compiler.models.BuildStack;
import p.studio.compiler.models.ProjectDescriptor;
import p.studio.compiler.models.SourceHandle;
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.pbs.PbsHostAdmissionErrors;
import p.studio.compiler.pbs.stdlib.InterfaceModuleLoader;
import p.studio.compiler.pbs.stdlib.StdlibEnvironment;
import p.studio.compiler.pbs.stdlib.StdlibEnvironmentResolver;
@@ -294,7 +295,7 @@ class PBSFrontendPhaseServiceTest {
final var sourceFile = modulePath.resolve("source.pbs");
final var modBarrel = modulePath.resolve("mod.barrel");
Files.writeString(sourceFile, """
import { draw } from @sdk:gfx;
import { Gfx } from @sdk:gfx;
fn use() -> int { return 1; }
""");
Files.writeString(modBarrel, "pub fn use() -> int;");
@@ -314,8 +315,16 @@ class PBSFrontendPhaseServiceTest {
final var stdlibModule = new StdlibModuleSource(
"sdk",
ReadOnlyList.wrap(List.of("gfx")),
ReadOnlyList.wrap(List.of(new StdlibModuleSource.SourceFile("gfx.pbs", "fn draw() -> int { return 1; }"))),
"pub fn draw() -> int;");
ReadOnlyList.wrap(List.of(new StdlibModuleSource.SourceFile(
"gfx.pbs",
"""
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw(x: int, y: int) -> void;
}
"""))),
"pub host Gfx;");
final var frontendService = new PBSFrontendPhaseService(
resolverForMajor(7, stdlibModule),
new InterfaceModuleLoader(2_000_000));
@@ -621,8 +630,157 @@ class PBSFrontendPhaseServiceTest {
.orElseThrow();
assertEquals(p.studio.compiler.source.diagnostics.DiagnosticPhase.STATIC_SEMANTICS, semanticsDiagnostic.getPhase());
assertEquals(semanticsDiagnostic.getCode(), semanticsDiagnostic.getTemplateId());
assertEquals(0, irBackend.getFunctions().size());
}
@Test
void shouldBlockDependentModulesTransitivelyWhenUpstreamModuleFailsLinking() throws IOException {
final var projectRoot = tempDir.resolve("project-fail-fast-linking");
final var sourceRoot = projectRoot.resolve("src");
final var moduleAPath = sourceRoot.resolve("a");
final var moduleBPath = sourceRoot.resolve("b");
final var moduleCPath = sourceRoot.resolve("c");
final var moduleDPath = sourceRoot.resolve("d");
Files.createDirectories(moduleAPath);
Files.createDirectories(moduleBPath);
Files.createDirectories(moduleCPath);
Files.createDirectories(moduleDPath);
final var aSource = moduleAPath.resolve("source.pbs");
final var aBarrel = moduleAPath.resolve("mod.barrel");
final var aInvalidBarrel = moduleAPath.resolve("extra.barrel");
Files.writeString(aSource, "fn a_entry() -> int { return 1; }");
Files.writeString(aBarrel, "pub fn a_entry() -> int;");
Files.writeString(aInvalidBarrel, "pub fn a_entry() -> int;");
final var bSource = moduleBPath.resolve("source.pbs");
final var bBarrel = moduleBPath.resolve("mod.barrel");
Files.writeString(bSource, """
import { a_entry } from @app:a;
fn b_entry() -> int { return 2; }
""");
Files.writeString(bBarrel, "pub fn b_entry() -> int;");
final var cSource = moduleCPath.resolve("source.pbs");
final var cBarrel = moduleCPath.resolve("mod.barrel");
Files.writeString(cSource, """
import { b_entry } from @app:b;
fn c_entry() -> int { return 3; }
""");
Files.writeString(cBarrel, "pub fn c_entry() -> int;");
final var dSource = moduleDPath.resolve("source.pbs");
final var dBarrel = moduleDPath.resolve("mod.barrel");
Files.writeString(dSource, "fn d_entry() -> int { return 4; }");
Files.writeString(dBarrel, "pub fn d_entry() -> int;");
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, aSource, fileTable);
registerFile(projectId, projectRoot, aBarrel, fileTable);
registerFile(projectId, projectRoot, aInvalidBarrel, fileTable);
registerFile(projectId, projectRoot, bSource, fileTable);
registerFile(projectId, projectRoot, bBarrel, fileTable);
registerFile(projectId, projectRoot, cSource, fileTable);
registerFile(projectId, projectRoot, cBarrel, fileTable);
registerFile(projectId, projectRoot, dSource, fileTable);
registerFile(projectId, projectRoot, dBarrel, fileTable);
final var ctx = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))));
final var diagnostics = DiagnosticSink.empty();
final var irBackend = new PBSFrontendPhaseService().compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsLinkErrors.E_LINK_INVALID_BARREL_FILENAME.name())));
assertEquals(1, irBackend.getFunctions().size());
assertEquals("run", irBackend.getFunctions().getFirst().name());
assertEquals("d_entry", irBackend.getFunctions().getFirst().name());
}
@Test
void shouldBlockDependentModulesWhenUpstreamModuleFailsHostAdmission() throws IOException {
final var projectRoot = tempDir.resolve("project-fail-fast-host-admission");
final var sourceRoot = projectRoot.resolve("src");
final var dependentModulePath = sourceRoot.resolve("dependent");
final var independentModulePath = sourceRoot.resolve("independent");
Files.createDirectories(dependentModulePath);
Files.createDirectories(independentModulePath);
final var dependentSource = dependentModulePath.resolve("source.pbs");
final var dependentBarrel = dependentModulePath.resolve("mod.barrel");
Files.writeString(dependentSource, """
import { Gfx } from @sdk:gfx;
fn dependent() -> int { return 2; }
""");
Files.writeString(dependentBarrel, "pub fn dependent() -> int;");
final var independentSource = independentModulePath.resolve("source.pbs");
final var independentBarrel = independentModulePath.resolve("mod.barrel");
Files.writeString(independentSource, "fn independent() -> int { return 3; }");
Files.writeString(independentBarrel, "pub fn independent() -> int;");
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, dependentSource, fileTable);
registerFile(projectId, projectRoot, dependentBarrel, fileTable);
registerFile(projectId, projectRoot, independentSource, fileTable);
registerFile(projectId, projectRoot, independentBarrel, fileTable);
final var badSdkModule = new StdlibModuleSource(
"sdk",
ReadOnlyList.wrap(List.of("gfx")),
ReadOnlyList.wrap(List.of(new StdlibModuleSource.SourceFile(
"main.pbs",
"""
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "unknown_capability")]
fn draw_pixel(x: int, y: int) -> void;
}
"""))),
"pub host Gfx;");
final var frontendService = new PBSFrontendPhaseService(
resolverForMajor(9, badSdkModule),
new InterfaceModuleLoader(3_000_000));
final var ctx = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))),
9);
final var diagnostics = DiagnosticSink.empty();
final var irBackend = frontendService.compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsHostAdmissionErrors.E_HOST_UNKNOWN_CAPABILITY.name())));
assertEquals(1, irBackend.getFunctions().size());
assertEquals("independent", irBackend.getFunctions().getFirst().name());
}
private void registerFile(