implements PR030

This commit is contained in:
2026-03-24 13:42:23 +00:00
parent 01c5b6649a
commit 495104db6d
23 changed files with 461 additions and 11 deletions
@@ -103,4 +103,29 @@ class PbsDiagnosticsContractTest {
assertEquals(diagnostic.getCode(), diagnostic.getTemplateId());
assertEquals(9, diagnostic.getSpan().getFileId().getId());
}
@Test
void shouldTagHostAdmissionDiagnosticsWithHostAdmissionPhase() {
final var source = """
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
fn draw_pixel(x: int, y: int) -> void;
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(
new FileId(10),
source,
diagnostics,
p.studio.compiler.models.SourceKind.SDK_INTERFACE);
final var diagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsHostAdmissionErrors.E_HOST_MISSING_REQUIRED_CAPABILITY.name()))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.HOST_ADMISSION, diagnostic.getPhase());
assertEquals(diagnostic.getCode(), diagnostic.getTemplateId());
assertEquals(10, diagnostic.getSpan().getFileId().getId());
}
}
@@ -1,11 +1,14 @@
package p.studio.compiler.pbs;
import org.junit.jupiter.api.Test;
import p.studio.compiler.messages.HostAdmissionContext;
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;
import p.studio.utilities.structures.ReadOnlyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -115,6 +118,7 @@ class PbsFrontendCompilerTest {
}
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw_pixel(x: int, y: int, color: Color) -> void;
}
[BuiltinConst(target = "Color", name = "white", version = 1)]
@@ -130,10 +134,102 @@ class PbsFrontendCompilerTest {
assertEquals(1, fileBackend.reservedMetadata().hostMethodBindings().size());
assertEquals(1, fileBackend.reservedMetadata().builtinTypeSurfaces().size());
assertEquals(1, fileBackend.reservedMetadata().builtinConstSurfaces().size());
assertEquals(1, fileBackend.reservedMetadata().requiredCapabilities().size());
assertEquals("gfx", fileBackend.reservedMetadata().requiredCapabilities().getFirst());
assertEquals("Color", fileBackend.reservedMetadata().builtinTypeSurfaces().getFirst().canonicalTypeName());
assertEquals(3, fileBackend.reservedMetadata().builtinTypeSurfaces().getFirst().fields().size());
}
@Test
void shouldRejectHostBindingWithoutCapabilityAtHostAdmission() {
final var source = """
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
fn draw_pixel(x: int, y: int) -> void;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(7), source, diagnostics, SourceKind.SDK_INTERFACE);
final var hostDiagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsHostAdmissionErrors.E_HOST_MISSING_REQUIRED_CAPABILITY.name()))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.HOST_ADMISSION, hostDiagnostic.getPhase());
assertEquals(hostDiagnostic.getCode(), hostDiagnostic.getTemplateId());
assertEquals(0, fileBackend.functions().size());
}
@Test
void shouldRejectHostBindingCapabilityNotDeclaredInStrictContext() {
final var source = """
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw_pixel(x: int, y: int) -> void;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
compiler.compileFile(
new FileId(8),
source,
diagnostics,
SourceKind.SDK_INTERFACE,
HostAdmissionContext.strictWithDeclaredCapabilities(ReadOnlyList.wrap(java.util.List.of("input"))));
final var hostDiagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsHostAdmissionErrors.E_HOST_CAPABILITY_NOT_DECLARED.name()))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.HOST_ADMISSION, hostDiagnostic.getPhase());
}
@Test
void shouldRejectUnknownHostCapabilityAtHostAdmission() {
final var source = """
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "video")]
fn draw_pixel(x: int, y: int) -> void;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
compiler.compileFile(new FileId(9), source, diagnostics, SourceKind.SDK_INTERFACE);
final var hostDiagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsHostAdmissionErrors.E_HOST_UNKNOWN_CAPABILITY.name()))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.HOST_ADMISSION, hostDiagnostic.getPhase());
}
@Test
void shouldRejectMalformedHostCapabilityMetadataAtHostAdmission() {
final var source = """
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(version = 1)]
fn draw_pixel(x: int, y: int) -> void;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
compiler.compileFile(new FileId(10), source, diagnostics, SourceKind.SDK_INTERFACE);
final var hostDiagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsHostAdmissionErrors.E_HOST_MALFORMED_CAPABILITY_METADATA.name()))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.HOST_ADMISSION, hostDiagnostic.getPhase());
}
@Test
void shouldInferBuiltinFieldSlotsFromDeclarationOrder() {
final var source = """
@@ -59,6 +59,7 @@ class PbsGateUSdkInterfaceConformanceTest {
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw_pixel(x: int, y: int, color: Color) -> void;
}
""";
@@ -165,6 +166,7 @@ class PbsGateUSdkInterfaceConformanceTest {
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw_pixel(x: int, y: int, color: Color) -> void;
}
""";
@@ -229,6 +231,7 @@ class PbsGateUSdkInterfaceConformanceTest {
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw_pixel(x: int, y: int, color: Color) -> void;
}
@@ -268,6 +271,30 @@ class PbsGateUSdkInterfaceConformanceTest {
DiagnosticPhase.STATIC_SEMANTICS);
}
@Test
void gateU_shouldEmitHostAdmissionDiagnosticForMissingCapabilityMetadata() {
final var source = """
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
fn draw_pixel(x: int, y: int) -> void;
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(
new FileId(42),
source,
diagnostics,
SourceKind.SDK_INTERFACE);
final var hostAdmission = firstDiagnostic(diagnostics,
d -> d.getCode().equals(PbsHostAdmissionErrors.E_HOST_MISSING_REQUIRED_CAPABILITY.name()));
assertStableDiagnosticIdentity(
hostAdmission,
PbsHostAdmissionErrors.E_HOST_MISSING_REQUIRED_CAPABILITY.name(),
DiagnosticPhase.HOST_ADMISSION);
}
private WorkspaceCompileResult compileWorkspaceModule(
final Path projectRoot,
final String sourceContent,
@@ -224,6 +224,7 @@ class PbsModuleVisibilityTest {
"""
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw_pixel(x: int, y: int) -> void;
}
""",
@@ -127,6 +127,7 @@ class PbsParserTest {
void shouldRejectAttributesInOrdinarySourceAndRecover() {
final var source = """
[Host(module = "gfx", name = "draw", version = 1)]
[Capability(name = "gfx")]
fn run() -> int { return 1; }
declare struct Point(x: int,);
""";
@@ -230,6 +231,7 @@ class PbsParserTest {
}
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw(x: int, y: int, color: Color) -> void;
}
[BuiltinConst(target = "Color", name = "white", version = 1)]
@@ -256,7 +258,7 @@ class PbsParserTest {
final var hostDecl = assertInstanceOf(PbsAst.HostDecl.class, ast.topDecls().get(1));
assertEquals(1, hostDecl.signatures().size());
assertEquals(1, hostDecl.signatures().getFirst().attributes().size());
assertEquals(2, hostDecl.signatures().getFirst().attributes().size());
final var constDecl = assertInstanceOf(PbsAst.ConstDecl.class, ast.topDecls().get(2));
assertEquals(1, constDecl.attributes().size());
@@ -267,6 +269,7 @@ class PbsParserTest {
final var source = """
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw(x: int, y: int) -> void;
unexpected_token
}
@@ -24,6 +24,7 @@ class PbsInterfaceModuleSemanticsTest {
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw_pixel(x: int, y: int, c: Color) -> void;
}
@@ -51,6 +52,7 @@ class PbsInterfaceModuleSemanticsTest {
declare contract Api {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn run() -> void;
}
@@ -246,6 +246,7 @@ class PBSFrontendPhaseServiceTest {
}
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw(x: int, y: int, color: Color) -> void;
}
""");
@@ -529,6 +530,7 @@ class PBSFrontendPhaseServiceTest {
"""
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
[Capability(name = "gfx")]
fn draw_pixel(x: int, y: int) -> void;
}
"""))),