implements PR-19.5 global dependency graph and cycle validation

This commit is contained in:
2026-03-26 19:21:34 +00:00
parent 71a993ad4a
commit 2003fc749e
10 changed files with 548 additions and 2 deletions
@@ -0,0 +1,82 @@
package p.studio.compiler.pbs.semantics;
import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.parser.PbsParser;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.identifiers.ModuleId;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PbsGlobalSemanticsValidatorTest {
@Test
void shouldProduceDeterministicGlobalOrderIndependentOfSourceOrder() {
final var firstAst = parse("""
declare global B: int = A + 1;
declare global C: int = B + 1;
declare global A: int = 1;
""");
final var secondAst = parse("""
declare global C: int = B + 1;
declare global A: int = 1;
declare global B: int = A + 1;
""");
final var validator = new PbsGlobalSemanticsValidator();
final var firstOrder = validator.analyse(firstAst, ModuleId.none(), ReadOnlyList.empty())
.graphAnalysis()
.traversalOrder()
.asList()
.stream()
.map(PbsGlobalDependencySupport.GlobalRef::globalName)
.toList();
final var secondOrder = validator.analyse(secondAst, ModuleId.none(), ReadOnlyList.empty())
.graphAnalysis()
.traversalOrder()
.asList()
.stream()
.map(PbsGlobalDependencySupport.GlobalRef::globalName)
.toList();
assertEquals(List.of("A", "B", "C"), firstOrder);
assertEquals(firstOrder, secondOrder);
}
@Test
void shouldRejectCyclicGlobalDependencies() {
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0), """
declare global A: int = B + 1;
declare global B: int = C + 1;
declare global C: int = A + 1;
""", diagnostics);
final var cycleCount = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_GLOBAL_CYCLIC_DEPENDENCY.name()))
.count();
assertEquals(3, cycleCount);
assertFalse(diagnostics.isEmpty());
}
private PbsAst.File parse(final String source) {
final var diagnostics = DiagnosticSink.empty();
final var fileId = new FileId(0);
final var ast = PbsParser.parse(
PbsLexer.lex(source, fileId, diagnostics),
fileId,
diagnostics,
PbsParser.ParseMode.ORDINARY);
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
return ast;
}
}
@@ -1168,6 +1168,65 @@ class PBSFrontendPhaseServiceTest {
assertTrue(intrinsicCalls.contains("input.touch.y"));
}
@Test
void shouldReportInterModuleGlobalCycleThroughImportedAlias() throws IOException {
final var projectRoot = tempDir.resolve("project-global-cycle-alias");
final var sourceRoot = projectRoot.resolve("src");
final var moduleAPath = sourceRoot.resolve("a");
final var moduleBPath = sourceRoot.resolve("b");
Files.createDirectories(moduleAPath);
Files.createDirectories(moduleBPath);
final var sourceA = moduleAPath.resolve("source.pbs");
final var barrelA = moduleAPath.resolve("mod.barrel");
Files.writeString(sourceA, """
import { B as ImportedB } from @app:b;
declare global A: int = ImportedB + 1;
""");
Files.writeString(barrelA, "pub global A;");
final var sourceB = moduleBPath.resolve("source.pbs");
final var barrelB = moduleBPath.resolve("mod.barrel");
Files.writeString(sourceB, """
import { A } from @app:a;
declare global B: int = A + 1;
""");
Files.writeString(barrelB, "pub global B;");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final var projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("app")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
.build());
registerFile(projectId, projectRoot, sourceA, fileTable);
registerFile(projectId, projectRoot, barrelA, fileTable);
registerFile(projectId, projectRoot, sourceB, fileTable);
registerFile(projectId, projectRoot, barrelB, fileTable);
final var ctx = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))));
final var diagnostics = DiagnosticSink.empty();
new PBSFrontendPhaseService().compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
final var cycleCount = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_GLOBAL_CYCLIC_DEPENDENCY.name()))
.count();
assertEquals(2, cycleCount, diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
}
private void registerFile(
final ProjectId projectId,
final Path projectRoot,