implements PR-19.4 global semantics identity and collision validation

This commit is contained in:
2026-03-26 19:12:42 +00:00
parent 738eea71ee
commit 71a993ad4a
7 changed files with 179 additions and 0 deletions
@@ -144,6 +144,65 @@ class PbsSemanticsDeclarationsTest {
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_DECLARATION.name())));
}
@Test
void shouldRejectVisibleTopLevelCollisionsBetweenFunctionServiceGlobalAndConst() {
final var source = """
fn Tick() -> int { return 1; }
declare service Tick {
fn run() -> void { return; }
}
declare global Score: int = 0;
declare const Score: int = 1;
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
final var duplicateCount = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_DECLARATION.name()))
.count();
assertTrue(duplicateCount >= 2);
}
@Test
void shouldAcceptSupportedGlobalInitializerForms() {
final var source = """
declare struct Box(value: int);
declare global BASE: int = 1;
declare global CLONE: int = BASE + 1;
declare global DEFAULT_BOX: Box = new Box(BASE);
fn read() -> int {
return CLONE;
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
assertFalse(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_GLOBAL_UNSUPPORTED_INITIALIZER.name())));
}
@Test
void shouldRejectUnsupportedGlobalInitializerForms() {
final var source = """
fn inc(v: int) -> int { return v + 1; }
declare global A: int = inc(1);
declare global B: int = if true { 1 } else { 2 };
declare global C: optional int = some(1);
declare global D: optional int = none;
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
final var invalidCount = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_GLOBAL_UNSUPPORTED_INITIALIZER.name()))
.count();
assertEquals(4, invalidCount);
}
@Test
void shouldAllowSelfInStructServiceMethodsAndCtors() {
final var source = """