implements PR027

This commit is contained in:
2026-03-24 13:42:23 +00:00
parent 9fb8622ddb
commit bb5cc05c0b
12 changed files with 611 additions and 120 deletions
@@ -80,4 +80,25 @@ class PbsDiagnosticsContractTest {
assertEquals(DiagnosticPhase.LINKING, diagnostic.getPhase());
assertEquals(1, diagnostic.getRelated().size());
}
@Test
void shouldTagLoadFacingDiagnosticsWithStableTemplateAndAttribution() {
final var source = """
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
pub r: int
);
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(9), source, diagnostics, p.studio.compiler.models.SourceKind.SDK_INTERFACE);
final var diagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsLoadErrors.E_LOAD_UNSUPPORTED_RESERVED_METADATA_SURFACE.name()))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.LOAD_FACING_REJECTION, diagnostic.getPhase());
assertEquals(diagnostic.getCode(), diagnostic.getTemplateId());
assertEquals(9, diagnostic.getSpan().getFileId().getId());
}
}
@@ -1,8 +1,10 @@
package p.studio.compiler.pbs;
import org.junit.jupiter.api.Test;
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.pbs.lexer.LexErrors;
import p.studio.compiler.pbs.semantics.PbsSemanticsErrors;
import p.studio.compiler.source.diagnostics.DiagnosticPhase;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
@@ -99,4 +101,84 @@ class PbsFrontendCompilerTest {
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_CALLABLE_SHAPE.name())));
assertEquals(0, fileBackend.functions().size());
}
@Test
void shouldExtractReservedMetadataForInterfaceModuleSurfaces() {
final var source = """
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
[Slot(index = 0)] pub r: int,
[Slot(index = 1)] pub g: int,
[Slot(index = 2)] pub b: int
) {
[IntrinsicCall(name = "core.color.pack")]
fn pack() -> int;
}
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
fn draw_pixel(x: int, y: int, color: Color) -> void;
}
[BuiltinConst(target = "Color", name = "white", version = 1)]
declare const WHITE: Color;
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(0), source, diagnostics, SourceKind.SDK_INTERFACE);
assertTrue(diagnostics.isEmpty());
assertEquals(0, fileBackend.functions().size());
assertEquals(1, fileBackend.reservedMetadata().hostMethodBindings().size());
assertEquals(1, fileBackend.reservedMetadata().builtinTypeSurfaces().size());
assertEquals(1, fileBackend.reservedMetadata().builtinConstSurfaces().size());
assertEquals("Color", fileBackend.reservedMetadata().builtinTypeSurfaces().getFirst().canonicalTypeName());
assertEquals(3, fileBackend.reservedMetadata().builtinTypeSurfaces().getFirst().fields().size());
}
@Test
void shouldRejectUnsupportedBuiltinSlotInferenceAtLoadAdmissionBoundary() {
final var source = """
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
pub r: int,
[Slot(index = 1)] pub g: int
);
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(3), source, diagnostics, SourceKind.SDK_INTERFACE);
final var rejectionDiagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsLoadErrors.E_LOAD_UNSUPPORTED_RESERVED_METADATA_SURFACE.name()))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.LOAD_FACING_REJECTION, rejectionDiagnostic.getPhase());
assertEquals(rejectionDiagnostic.getCode(), rejectionDiagnostic.getTemplateId());
assertEquals(3, rejectionDiagnostic.getSpan().getFileId().getId());
assertEquals(0, fileBackend.functions().size());
}
@Test
void shouldRejectDuplicateBuiltinSlotIndexesAtLoadAdmissionBoundary() {
final var source = """
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
[Slot(index = 0)] pub r: int,
[Slot(index = 0)] pub g: int
);
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
compiler.compileFile(new FileId(5), source, diagnostics, SourceKind.SDK_INTERFACE);
final var rejectionDiagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsLoadErrors.E_LOAD_DUPLICATE_BUILTIN_SLOT_INDEX.name()))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.LOAD_FACING_REJECTION, rejectionDiagnostic.getPhase());
assertEquals(rejectionDiagnostic.getCode(), rejectionDiagnostic.getTemplateId());
assertEquals(1, rejectionDiagnostic.getRelated().size());
}
}
@@ -437,6 +437,8 @@ class PBSFrontendPhaseServiceTest {
assertTrue(diagnostics.stream().noneMatch(d ->
d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_SYMBOL_NOT_PUBLIC.name())));
assertEquals(0, irBackend.getFunctions().size());
assertTrue(irBackend.getReservedMetadata().builtinTypeSurfaces().stream()
.anyMatch(t -> t.sourceTypeName().equals("Color") && t.fields().size() == 4));
}
@Test
@@ -488,6 +490,8 @@ class PBSFrontendPhaseServiceTest {
assertTrue(diagnostics.stream().noneMatch(d ->
d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_SYMBOL_NOT_PUBLIC.name())));
assertEquals(0, irBackend.getFunctions().size());
assertTrue(irBackend.getReservedMetadata().hostMethodBindings().stream()
.anyMatch(h -> h.ownerName().equals("Gfx") && h.sourceMethodName().equals("draw_pixel")));
}
@Test
@@ -552,6 +556,71 @@ class PBSFrontendPhaseServiceTest {
assertEquals(0, irBackend.getFunctions().size());
}
@Test
void shouldEmitLoadFacingRejectionForUnsupportedStdlibReservedMetadataShape() throws IOException {
final var projectRoot = tempDir.resolve("project-stdlib-invalid-metadata");
final var sourceRoot = projectRoot.resolve("src");
final var modulePath = sourceRoot.resolve("app");
Files.createDirectories(modulePath);
final var sourceFile = modulePath.resolve("source.pbs");
final var modBarrel = modulePath.resolve("mod.barrel");
Files.writeString(sourceFile, """
import { Color } from @core:color;
fn run() -> int { return 1; }
""");
Files.writeString(modBarrel, "pub fn run() -> 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, sourceFile, fileTable);
registerFile(projectId, projectRoot, modBarrel, fileTable);
final var invalidColorModule = new StdlibModuleSource(
"core",
ReadOnlyList.wrap(List.of("color")),
ReadOnlyList.wrap(List.of(new StdlibModuleSource.SourceFile(
"main.pbs",
"""
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
pub r: int
);
"""))),
"pub struct Color;");
final var frontendService = new PBSFrontendPhaseService(
resolverForMajor(1, invalidColorModule),
new InterfaceModuleLoader(2_700_000));
final var ctx = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))),
1);
final var diagnostics = DiagnosticSink.empty();
final var irBackend = frontendService.compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
final var rejectionDiagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(p.studio.compiler.pbs.PbsLoadErrors.E_LOAD_UNSUPPORTED_RESERVED_METADATA_SURFACE.name()))
.findFirst()
.orElseThrow();
assertEquals(p.studio.compiler.source.diagnostics.DiagnosticPhase.LOAD_FACING_REJECTION, rejectionDiagnostic.getPhase());
assertEquals(rejectionDiagnostic.getCode(), rejectionDiagnostic.getTemplateId());
assertEquals(1, irBackend.getFunctions().size());
assertEquals("run", irBackend.getFunctions().getFirst().name());
}
private void registerFile(
final ProjectId projectId,
final Path projectRoot,