implements PR029

This commit is contained in:
2026-03-24 13:42:23 +00:00
parent fc734403b5
commit 01c5b6649a
19 changed files with 497 additions and 256 deletions
@@ -82,22 +82,24 @@ class PbsDiagnosticsContractTest {
}
@Test
void shouldTagLoadFacingDiagnosticsWithStableTemplateAndAttribution() {
void shouldTagBuiltinLayoutDiagnosticsWithStaticSemanticsPhase() {
final var source = """
declare struct S(value: int);
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
pub r: int
);
pub raw: S
) {
}
""";
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()))
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_BUILTIN_LAYOUT_FIELD_TYPE_NOT_ADMISSIBLE.name()))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.LOAD_FACING_REJECTION, diagnostic.getPhase());
assertEquals(DiagnosticPhase.STATIC_SEMANTICS, diagnostic.getPhase());
assertEquals(diagnostic.getCode(), diagnostic.getTemplateId());
assertEquals(9, diagnostic.getSpan().getFileId().getId());
}
@@ -4,7 +4,6 @@ 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;
@@ -107,9 +106,9 @@ class PbsFrontendCompilerTest {
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
pub r: int,
pub g: int,
pub b: int
) {
[IntrinsicCall(name = "core.color.pack")]
fn pack() -> int;
@@ -136,49 +135,61 @@ class PbsFrontendCompilerTest {
}
@Test
void shouldRejectUnsupportedBuiltinSlotInferenceAtLoadAdmissionBoundary() {
void shouldInferBuiltinFieldSlotsFromDeclarationOrder() {
final var source = """
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
pub r: int,
[Slot(index = 1)] pub g: int
);
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());
assertTrue(diagnostics.isEmpty());
final var fields = fileBackend.reservedMetadata().builtinTypeSurfaces().getFirst().fields();
assertEquals(2, fields.size());
assertEquals(0L, fields.get(0).slotStart());
assertEquals(1L, fields.get(1).slotStart());
assertEquals(0, fileBackend.functions().size());
}
@Test
void shouldRejectDuplicateBuiltinSlotIndexesAtLoadAdmissionBoundary() {
void shouldInferBuiltinFieldSlotsUsingFlattenedNestedWidth() {
final var source = """
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
[Slot(index = 0)] pub r: int,
[Slot(index = 0)] pub g: int
);
pub r: int,
pub g: int,
pub b: int,
pub a: int
) {
}
[BuiltinType(name = "Pixel", version = 1)]
declare builtin type Pixel(
pub x: int,
pub y: int,
pub color: Color
) {
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
compiler.compileFile(new FileId(5), source, diagnostics, SourceKind.SDK_INTERFACE);
final var fileBackend = 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()))
assertTrue(diagnostics.isEmpty());
final var pixel = fileBackend.reservedMetadata().builtinTypeSurfaces().stream()
.filter(t -> t.sourceTypeName().equals("Pixel"))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.LOAD_FACING_REJECTION, rejectionDiagnostic.getPhase());
assertEquals(rejectionDiagnostic.getCode(), rejectionDiagnostic.getTemplateId());
assertEquals(1, rejectionDiagnostic.getRelated().size());
assertEquals(3, pixel.fields().size());
assertEquals(0L, pixel.fields().get(0).slotStart());
assertEquals(1L, pixel.fields().get(1).slotStart());
assertEquals(2L, pixel.fields().get(2).slotStart());
}
}
@@ -53,7 +53,7 @@ class PbsGateUSdkInterfaceConformanceTest {
final var source = """
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
[Slot(index = 0)] pub r: int
pub r: int
) {
}
@@ -159,7 +159,7 @@ class PbsGateUSdkInterfaceConformanceTest {
final var source = """
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
[Slot(index = 0)] pub r: int
pub r: int
) {
}
@@ -216,12 +216,12 @@ class PbsGateUSdkInterfaceConformanceTest {
}
@Test
void gateU_shouldExposeReservedMetadataAndRejectUnsupportedLoadAdmission() {
void gateU_shouldExposeReservedMetadataAndRejectInvalidBuiltinLayoutAtSemantics() {
final var validSource = """
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
[Slot(index = 0)] pub r: int,
[Slot(index = 1)] pub g: int
pub r: int,
pub g: int
) {
[IntrinsicCall(name = "core.color.pack", version = 1)]
fn pack() -> int;
@@ -247,9 +247,10 @@ class PbsGateUSdkInterfaceConformanceTest {
assertEquals(1, validBackend.reservedMetadata().builtinConstSurfaces().size());
final var invalidSource = """
declare struct RGBA(raw: int);
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
pub r: int
pub raw: RGBA
) {
}
""";
@@ -259,9 +260,12 @@ class PbsGateUSdkInterfaceConformanceTest {
invalidSource,
invalidDiagnostics,
SourceKind.SDK_INTERFACE);
final var unsupportedLoad = firstDiagnostic(invalidDiagnostics,
d -> d.getCode().equals(PbsLoadErrors.E_LOAD_UNSUPPORTED_RESERVED_METADATA_SURFACE.name()));
assertStableDiagnosticIdentity(unsupportedLoad, PbsLoadErrors.E_LOAD_UNSUPPORTED_RESERVED_METADATA_SURFACE.name(), DiagnosticPhase.LOAD_FACING_REJECTION);
final var invalidLayout = firstDiagnostic(invalidDiagnostics,
d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_BUILTIN_LAYOUT_FIELD_TYPE_NOT_ADMISSIBLE.name()));
assertStableDiagnosticIdentity(
invalidLayout,
PbsSemanticsErrors.E_SEM_BUILTIN_LAYOUT_FIELD_TYPE_NOT_ADMISSIBLE.name(),
DiagnosticPhase.STATIC_SEMANTICS);
}
private WorkspaceCompileResult compileWorkspaceModule(
@@ -256,9 +256,9 @@ class PbsModuleVisibilityTest {
"""
[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
pub r: int,
pub g: int,
pub b: int
) {
[IntrinsicCall(name = "core.color.pack", version = 1)]
fn pack() -> int;
@@ -221,9 +221,9 @@ class PbsParserTest {
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,
pub r: int,
pub g: int,
pub b: int,
) {
[IntrinsicCall(name = "color.pack", version = 1)]
fn pack() -> int;
@@ -251,7 +251,7 @@ class PbsParserTest {
assertEquals(1, builtinType.attributes().size());
assertEquals(3, builtinType.fields().size());
assertEquals(1, builtinType.signatures().size());
assertEquals(1, builtinType.fields().getFirst().attributes().size());
assertEquals(0, builtinType.fields().getFirst().attributes().size());
assertEquals(1, builtinType.signatures().getFirst().attributes().size());
final var hostDecl = assertInstanceOf(PbsAst.HostDecl.class, ast.topDecls().get(1));
@@ -303,6 +303,27 @@ class PbsParserTest {
assertTrue(diagnostics.hasErrors(), "Function-level else fallback syntax is not valid PBS core syntax");
}
@Test
void shouldRejectSlotAttributeInInterfaceModuleMode() {
final var source = """
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
[Slot(index = 0)] pub raw: int
) {
}
""";
final var diagnostics = DiagnosticSink.empty();
final var fileId = new FileId(0);
PbsParser.parse(
PbsLexer.lex(source, fileId, diagnostics),
fileId,
diagnostics,
PbsParser.ParseMode.INTERFACE_MODULE);
assertTrue(diagnostics.stream().anyMatch(d -> d.getCode().equals(ParseErrors.E_PARSE_INVALID_DECL_SHAPE.name())));
}
@Test
void shouldReportEnumMixedAndDuplicateCases() {
final var source = """
@@ -16,7 +16,7 @@ class PbsInterfaceModuleSemanticsTest {
final var source = """
[BuiltinType(name = "color", version = 1)]
declare builtin type Color(
[Slot(index = 0)] pub raw: int
pub raw: int
) {
[IntrinsicCall(name = "pack", version = 1)]
fn pack() -> int;
@@ -98,4 +98,71 @@ class PbsInterfaceModuleSemanticsTest {
.count();
assertTrue(nonDeclarativeCount >= 3);
}
@Test
void shouldRejectBuiltinFieldWithNonAdmissibleLayoutType() {
final var source = """
declare struct RGBA(raw: int);
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
pub raw: RGBA
) {
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics, SourceKind.SDK_INTERFACE);
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_BUILTIN_LAYOUT_FIELD_TYPE_NOT_ADMISSIBLE.name())));
}
@Test
void shouldRejectCyclicBuiltinLayoutComposition() {
final var source = """
[BuiltinType(name = "A", version = 1)]
declare builtin type A(
pub b: B
) {
}
[BuiltinType(name = "B", version = 1)]
declare builtin type B(
pub a: A
) {
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(1), source, diagnostics, SourceKind.SDK_INTERFACE);
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_BUILTIN_LAYOUT_CYCLIC_COMPOSITION.name())));
}
@Test
void shouldRejectBuiltinFieldWhenNestedBuiltinWidthIsUnresolvable() {
final var source = """
declare struct RGBA(raw: int);
[BuiltinType(name = "Bad", version = 1)]
declare builtin type Bad(
pub raw: RGBA
) {
}
[BuiltinType(name = "Pixel", version = 1)]
declare builtin type Pixel(
pub bad: Bad
) {
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(2), source, diagnostics, SourceKind.SDK_INTERFACE);
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_BUILTIN_LAYOUT_UNRESOLVABLE_WIDTH.name())));
}
}
@@ -237,9 +237,9 @@ class PBSFrontendPhaseServiceTest {
Files.writeString(sourceFile, """
[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
pub r: int,
pub g: int,
pub b: int
) {
[IntrinsicCall(name = "color.pack", version = 1)]
fn pack() -> int;
@@ -557,7 +557,7 @@ class PBSFrontendPhaseServiceTest {
}
@Test
void shouldEmitLoadFacingRejectionForUnsupportedStdlibReservedMetadataShape() throws IOException {
void shouldEmitStaticSemanticsErrorForInvalidStdlibBuiltinLayoutShape() throws IOException {
final var projectRoot = tempDir.resolve("project-stdlib-invalid-metadata");
final var sourceRoot = projectRoot.resolve("src");
final var modulePath = sourceRoot.resolve("app");
@@ -589,10 +589,12 @@ class PBSFrontendPhaseServiceTest {
ReadOnlyList.wrap(List.of(new StdlibModuleSource.SourceFile(
"main.pbs",
"""
declare struct RGBA(raw: int);
[BuiltinType(name = "Color", version = 1)]
declare builtin type Color(
pub r: int
);
pub raw: RGBA
) {
}
"""))),
"pub struct Color;");
final var frontendService = new PBSFrontendPhaseService(
@@ -611,12 +613,12 @@ class PBSFrontendPhaseServiceTest {
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()))
final var semanticsDiagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_BUILTIN_LAYOUT_FIELD_TYPE_NOT_ADMISSIBLE.name()))
.findFirst()
.orElseThrow();
assertEquals(p.studio.compiler.source.diagnostics.DiagnosticPhase.LOAD_FACING_REJECTION, rejectionDiagnostic.getPhase());
assertEquals(rejectionDiagnostic.getCode(), rejectionDiagnostic.getTemplateId());
assertEquals(p.studio.compiler.source.diagnostics.DiagnosticPhase.STATIC_SEMANTICS, semanticsDiagnostic.getPhase());
assertEquals(semanticsDiagnostic.getCode(), semanticsDiagnostic.getTemplateId());
assertEquals(1, irBackend.getFunctions().size());
assertEquals("run", irBackend.getFunctions().getFirst().name());
}