implements PR025
This commit is contained in:
+69
-1
@@ -215,6 +215,66 @@ class PbsModuleVisibilityTest {
|
||||
d.getCode().equals(PbsLinkErrors.E_LINK_UNRESOLVED_BARREL_ENTRY.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveHostBarrelEntryWhenHostDeclarationExistsInInterfaceModule() {
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var sourceFileId = new FileId(100);
|
||||
final var barrelFileId = new FileId(101);
|
||||
final var sourceAst = parseSource(
|
||||
"""
|
||||
declare host Gfx {
|
||||
[Host(module = "gfx", name = "draw_pixel", version = 1)]
|
||||
fn draw_pixel(x: int, y: int) -> void;
|
||||
}
|
||||
""",
|
||||
sourceFileId,
|
||||
diagnostics,
|
||||
PbsParser.ParseMode.INTERFACE_MODULE);
|
||||
final var barrelAst = parseBarrel(
|
||||
"""
|
||||
pub host Gfx;
|
||||
""",
|
||||
barrelFileId,
|
||||
diagnostics);
|
||||
final var module = new PbsModuleVisibilityValidator.ModuleUnit(
|
||||
new PbsModuleVisibilityValidator.ModuleCoordinates("sdk", ReadOnlyList.wrap(List.of("gfx"))),
|
||||
ReadOnlyList.wrap(List.of(new PbsModuleVisibilityValidator.SourceFile(sourceFileId, sourceAst))),
|
||||
ReadOnlyList.wrap(List.of(new PbsModuleVisibilityValidator.BarrelFile(barrelFileId, barrelAst))));
|
||||
|
||||
new PbsModuleVisibilityValidator().validate(ReadOnlyList.wrap(List.of(module)), diagnostics);
|
||||
|
||||
assertTrue(diagnostics.stream().noneMatch(d ->
|
||||
d.getCode().equals(PbsLinkErrors.E_LINK_UNRESOLVED_BARREL_ENTRY.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectHostBarrelEntryWhenHostDeclarationIsMissing() {
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var sourceFileId = new FileId(110);
|
||||
final var barrelFileId = new FileId(111);
|
||||
final var sourceAst = parseSource(
|
||||
"""
|
||||
declare enum Color(Red);
|
||||
""",
|
||||
sourceFileId,
|
||||
diagnostics);
|
||||
final var barrelAst = parseBarrel(
|
||||
"""
|
||||
pub host Gfx;
|
||||
""",
|
||||
barrelFileId,
|
||||
diagnostics);
|
||||
final var module = new PbsModuleVisibilityValidator.ModuleUnit(
|
||||
new PbsModuleVisibilityValidator.ModuleCoordinates("sdk", ReadOnlyList.wrap(List.of("gfx"))),
|
||||
ReadOnlyList.wrap(List.of(new PbsModuleVisibilityValidator.SourceFile(sourceFileId, sourceAst))),
|
||||
ReadOnlyList.wrap(List.of(new PbsModuleVisibilityValidator.BarrelFile(barrelFileId, barrelAst))));
|
||||
|
||||
new PbsModuleVisibilityValidator().validate(ReadOnlyList.wrap(List.of(module)), diagnostics);
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsLinkErrors.E_LINK_UNRESOLVED_BARREL_ENTRY.name())));
|
||||
}
|
||||
|
||||
private PbsModuleVisibilityValidator.ModuleUnit module(
|
||||
final String project,
|
||||
final String modulePath,
|
||||
@@ -257,7 +317,15 @@ class PbsModuleVisibilityTest {
|
||||
final String source,
|
||||
final FileId fileId,
|
||||
final DiagnosticSink diagnostics) {
|
||||
return PbsParser.parse(PbsLexer.lex(source, fileId, diagnostics), fileId, diagnostics);
|
||||
return parseSource(source, fileId, diagnostics, PbsParser.ParseMode.ORDINARY);
|
||||
}
|
||||
|
||||
private PbsAst.File parseSource(
|
||||
final String source,
|
||||
final FileId fileId,
|
||||
final DiagnosticSink diagnostics,
|
||||
final PbsParser.ParseMode parseMode) {
|
||||
return PbsParser.parse(PbsLexer.lex(source, fileId, diagnostics), fileId, diagnostics, parseMode);
|
||||
}
|
||||
|
||||
private PbsAst.BarrelFile parseBarrel(
|
||||
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.pbs.PbsFrontendCompiler;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsInterfaceModuleSemanticsTest {
|
||||
|
||||
@Test
|
||||
void shouldAcceptValidInterfaceModuleReservedMetadataShapes() {
|
||||
final var source = """
|
||||
[BuiltinType(name = "color", version = 1)]
|
||||
declare builtin type Color(
|
||||
[Slot(index = 0)] pub raw: int
|
||||
) {
|
||||
[IntrinsicCall(name = "pack", version = 1)]
|
||||
fn pack() -> int;
|
||||
}
|
||||
|
||||
declare host Gfx {
|
||||
[Host(module = "gfx", name = "draw_pixel", version = 1)]
|
||||
fn draw_pixel(x: int, y: int, c: Color) -> void;
|
||||
}
|
||||
|
||||
[BuiltinConst(target = "color", name = "white", version = 1)]
|
||||
declare const WHITE: Color;
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics, SourceKind.SDK_INTERFACE);
|
||||
|
||||
assertFalse(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_MISSING_REQUIRED_RESERVED_ATTRIBUTE.name())));
|
||||
assertFalse(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_RESERVED_ATTRIBUTE_TARGET.name())));
|
||||
assertFalse(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_MALFORMED_RESERVED_ATTRIBUTE.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectInvalidReservedAttributeTargetsAndShapesInInterfaceModule() {
|
||||
final var source = """
|
||||
declare host Gfx {
|
||||
fn draw_pixel(x: int, y: int) -> void;
|
||||
}
|
||||
|
||||
declare contract Api {
|
||||
[Host(module = "gfx", name = "draw_pixel", version = 1)]
|
||||
fn run() -> void;
|
||||
}
|
||||
|
||||
declare builtin type Color(pub raw: int) {
|
||||
[IntrinsicCall(version = 1)]
|
||||
fn pack() -> int;
|
||||
}
|
||||
|
||||
[BuiltinConst(target = "", name = "white", version = 0)]
|
||||
declare const WHITE: int = 1;
|
||||
""";
|
||||
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_MISSING_REQUIRED_RESERVED_ATTRIBUTE.name())));
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_RESERVED_ATTRIBUTE_TARGET.name())));
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_MALFORMED_RESERVED_ATTRIBUTE.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectExecutableDeclarationsInInterfaceModule() {
|
||||
final var source = """
|
||||
fn run() -> int { return 1; }
|
||||
|
||||
declare service Game {
|
||||
fn tick() -> int { return 1; }
|
||||
}
|
||||
|
||||
declare contract C { fn run() -> int; }
|
||||
declare struct S(v: int);
|
||||
implements C for S using s {
|
||||
fn run() -> int { return 1; }
|
||||
}
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics, SourceKind.SDK_INTERFACE);
|
||||
|
||||
final var nonDeclarativeCount = diagnostics.stream()
|
||||
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name()))
|
||||
.count();
|
||||
assertTrue(nonDeclarativeCount >= 3);
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -13,6 +13,7 @@ import p.studio.compiler.pbs.stdlib.StdlibEnvironment;
|
||||
import p.studio.compiler.pbs.stdlib.StdlibEnvironmentResolver;
|
||||
import p.studio.compiler.pbs.stdlib.StdlibModuleSource;
|
||||
import p.studio.compiler.pbs.linking.PbsLinkErrors;
|
||||
import p.studio.compiler.pbs.semantics.PbsSemanticsErrors;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
import p.studio.compiler.source.tables.FileTable;
|
||||
@@ -219,7 +220,8 @@ class PBSFrontendPhaseServiceTest {
|
||||
LogAggregator.empty(),
|
||||
BuildingIssueSink.empty());
|
||||
|
||||
assertTrue(diagnostics.isEmpty());
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name())));
|
||||
assertEquals(0, irBackend.getFunctions().size());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user