implements PR024

This commit is contained in:
2026-03-24 13:42:22 +00:00
parent 57f1a617ac
commit 0770bb869a
9 changed files with 459 additions and 203 deletions
@@ -216,6 +216,78 @@ class PbsParserTest {
assertInstanceOf(PbsAst.FunctionDecl.class, ast.topDecls().get(2));
}
@Test
void shouldParseReservedDeclarationsInInterfaceModuleMode() {
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 = "color.pack", version = 1)]
fn pack() -> int;
}
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
fn draw(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 fileId = new FileId(0);
final PbsAst.File ast = PbsParser.parse(
PbsLexer.lex(source, fileId, diagnostics),
fileId,
diagnostics,
PbsParser.ParseMode.INTERFACE_MODULE);
assertTrue(diagnostics.isEmpty(), "Parser should accept reserved interface declarations in interface-module mode");
assertEquals(3, ast.topDecls().size());
final var builtinType = assertInstanceOf(PbsAst.BuiltinTypeDecl.class, ast.topDecls().get(0));
assertEquals(1, builtinType.attributes().size());
assertEquals(3, builtinType.fields().size());
assertEquals(1, builtinType.signatures().size());
assertEquals(1, builtinType.fields().getFirst().attributes().size());
assertEquals(1, builtinType.signatures().getFirst().attributes().size());
final var hostDecl = assertInstanceOf(PbsAst.HostDecl.class, ast.topDecls().get(1));
assertEquals(1, hostDecl.signatures().size());
assertEquals(1, hostDecl.signatures().getFirst().attributes().size());
final var constDecl = assertInstanceOf(PbsAst.ConstDecl.class, ast.topDecls().get(2));
assertEquals(1, constDecl.attributes().size());
}
@Test
void shouldRecoverFromMalformedReservedDeclarationsInInterfaceModuleMode() {
final var source = """
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
fn draw(x: int, y: int) -> void;
unexpected_token
}
fn ok() -> int { return 1; }
""";
final var diagnostics = DiagnosticSink.empty();
final var fileId = new FileId(0);
final PbsAst.File ast = PbsParser.parse(
PbsLexer.lex(source, fileId, diagnostics),
fileId,
diagnostics,
PbsParser.ParseMode.INTERFACE_MODULE);
assertTrue(diagnostics.hasErrors(), "Malformed reserved forms must still emit deterministic parse diagnostics");
assertTrue(diagnostics.stream().anyMatch(d -> d.getCode().equals(ParseErrors.E_PARSE_INVALID_DECL_SHAPE.name())));
assertEquals(2, ast.topDecls().size());
assertInstanceOf(PbsAst.HostDecl.class, ast.topDecls().get(0));
assertInstanceOf(PbsAst.FunctionDecl.class, ast.topDecls().get(1));
}
@Test
void shouldRejectFunctionElseFallbackSurface() {
final var source = """
@@ -223,6 +223,64 @@ class PBSFrontendPhaseServiceTest {
assertEquals(0, irBackend.getFunctions().size());
}
@Test
void shouldParseReservedDeclarationsWhenSourceKindIsSdkInterface() throws IOException {
final var projectRoot = tempDir.resolve("project-sdk-interface-reserved");
final var sourceRoot = projectRoot.resolve("src");
final var modulePath = sourceRoot.resolve("gfx");
Files.createDirectories(modulePath);
final var sourceFile = modulePath.resolve("source.pbs");
final var modBarrel = modulePath.resolve("mod.barrel");
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
) {
[IntrinsicCall(name = "color.pack", version = 1)]
fn pack() -> int;
}
declare host Gfx {
[Host(module = "gfx", name = "draw_pixel", version = 1)]
fn draw(x: int, y: int, color: Color) -> void;
}
""");
Files.writeString(modBarrel, "");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final var projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("gfx-sdk")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
.sourceKind(SourceKind.SDK_INTERFACE)
.build());
registerFile(projectId, projectRoot, sourceFile, fileTable);
registerFile(projectId, projectRoot, modBarrel, 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().noneMatch(d ->
d.getCode().equals(PbsLinkErrors.E_LINK_UNRESOLVED_BARREL_ENTRY.name())));
assertTrue(diagnostics.stream().noneMatch(d ->
d.getCode().equals(PbsLinkErrors.E_LINK_MISSING_BARREL.name())));
assertEquals(0, irBackend.getFunctions().size());
}
@Test
void shouldResolveReservedImportsThroughStdlibResolverForSelectedMajor() throws IOException {
final var projectRoot = tempDir.resolve("project-stdlib");