implements PLN-0007

This commit is contained in:
2026-03-27 17:24:36 +00:00
parent 4a6cc35989
commit d28916578b
14 changed files with 357 additions and 12 deletions
@@ -10,6 +10,7 @@ import p.studio.compiler.pbs.parser.PbsParser;
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.diagnostics.Severity;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.utilities.structures.ReadOnlyList;
@@ -52,6 +53,28 @@ class PbsDiagnosticsContractTest {
assertEquals(DiagnosticPhase.STATIC_SEMANTICS, diagnostic.getPhase());
}
@Test
void shouldTagIgnoredValueWarningsWithStaticSemanticsPhase() {
final var source = """
fn pure() -> int { return 1; }
fn frame() -> void {
pure();
return;
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(11), source, diagnostics);
final var diagnostic = diagnostics.stream()
.filter(d -> d.getCode().equals(PbsSemanticsErrors.W_SEM_IGNORED_VALUE.name()))
.findFirst()
.orElseThrow();
assertEquals(DiagnosticPhase.STATIC_SEMANTICS, diagnostic.getPhase());
assertEquals(Severity.Warning, diagnostic.getSeverity());
assertEquals(diagnostic.getCode(), diagnostic.getTemplateId());
}
@Test
void shouldTagLinkingDiagnosticsAndExposeRelatedSpanForDuplicateBarrelEntry() {
final var diagnostics = DiagnosticSink.empty();
@@ -288,13 +288,17 @@ class PbsFrontendCompilerTest {
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(99), source, diagnostics);
assertTrue(diagnostics.isEmpty(), "Valid program should not report diagnostics");
assertEquals(0, diagnostics.errorCount(), "Valid program should not report errors");
assertEquals(2, diagnostics.warningCount());
final var executableA = fileBackend.executableFunctions().stream()
.filter(fn -> fn.callableName().equals("a"))
.findFirst()
.orElseThrow();
assertEquals("b", executableA.instructions().get(0).calleeCallableName());
assertEquals("c", executableA.instructions().get(1).calleeCallableName());
final var callNames = executableA.instructions().stream()
.filter(i -> i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_FUNC)
.map(p.studio.compiler.models.IRBackendExecutableFunction.Instruction::calleeCallableName)
.toList();
assertEquals(java.util.List.of("b", "c"), callNames);
}
@Test
@@ -312,7 +316,8 @@ class PbsFrontendCompilerTest {
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(100), source, diagnostics);
assertTrue(diagnostics.isEmpty(), "Valid program should not report diagnostics");
assertEquals(0, diagnostics.errorCount(), "Valid program should not report errors");
assertEquals(1, diagnostics.warningCount());
final var executableMain = fileBackend.executableFunctions().stream()
.filter(fn -> fn.callableName().equals("main"))
.findFirst()
@@ -512,6 +517,61 @@ class PbsFrontendCompilerTest {
assertEquals(0, fileBackend.executableFunctions().size());
}
@Test
void shouldWarnAndDiscardIgnoredMaterializedExpressionStatements() {
final var source = """
fn pure() -> int { return 1; }
fn frame() -> void {
pure();
1 + 2;
return;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(14), source, diagnostics);
assertEquals(2, diagnostics.warningCount());
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.W_SEM_IGNORED_VALUE.name())));
final var executableFrame = fileBackend.executableFunctions().stream()
.filter(fn -> fn.callableName().equals("frame"))
.findFirst()
.orElseThrow();
assertTrue(executableFrame.instructions().stream().anyMatch(i ->
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_FUNC));
assertEquals(2, executableFrame.instructions().stream()
.filter(i -> i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.POP)
.count());
}
@Test
void shouldNotWarnOrDiscardUnitExpressionStatements() {
final var source = """
fn log() -> void { return; }
fn frame() -> void {
log();
return;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(15), source, diagnostics);
assertEquals(0, diagnostics.warningCount());
final var executableFrame = fileBackend.executableFunctions().stream()
.filter(fn -> fn.callableName().equals("frame"))
.findFirst()
.orElseThrow();
assertEquals(0, executableFrame.instructions().stream()
.filter(i -> i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.POP)
.count());
}
@Test
void shouldRejectHostBindingWithoutCapabilityAtHostAdmission() {
final var source = """
@@ -0,0 +1,48 @@
package p.studio.compiler.pbs.semantics;
import org.junit.jupiter.api.Test;
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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PbsIgnoredValueSemanticsTest {
@Test
void shouldWarnWhenExpressionStatementIgnoresMaterializedValue() {
final var source = """
fn pure() -> int { return 1; }
fn frame() -> void {
pure();
return;
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
assertEquals(1, diagnostics.warningCount());
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.W_SEM_IGNORED_VALUE.name())));
}
@Test
void shouldNotWarnWhenExpressionStatementIsUnitLike() {
final var source = """
fn log() -> void { return; }
fn frame() -> void {
log();
return;
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(1), source, diagnostics);
assertEquals(0, diagnostics.warningCount());
}
}
@@ -87,7 +87,8 @@ class PBSFrontendPhaseServiceTest {
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
assertEquals(0, diagnostics.errorCount(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
assertEquals(0, diagnostics.warningCount());
final var frameFn = irBackend.getExecutableFunctions().stream()
.filter(function -> "frame".equals(function.callableName()))
.findFirst()
@@ -97,6 +98,62 @@ class PBSFrontendPhaseServiceTest {
&& "91".equals(instruction.label())));
}
@Test
void shouldKeepWarningsNonBlockingWhileLoweringExplicitDiscard() throws IOException {
final var projectRoot = tempDir.resolve("project-ignored-values");
final var sourceRoot = projectRoot.resolve("src");
final var modulePath = sourceRoot.resolve("main");
Files.createDirectories(modulePath);
final var sourceFile = modulePath.resolve("source.pbs");
final var modBarrel = modulePath.resolve("mod.barrel");
Files.writeString(sourceFile, """
fn pure() -> int { return 1; }
[Frame]
fn frame() -> void {
pure();
return;
}
""");
Files.writeString(modBarrel, "pub fn frame() -> void;");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final var projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("core")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
.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());
assertEquals(1, diagnostics.warningCount());
assertEquals(0, diagnostics.errorCount());
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.W_SEM_IGNORED_VALUE.name())));
final var frameFn = irBackend.getExecutableFunctions().stream()
.filter(function -> "frame".equals(function.callableName()))
.findFirst()
.orElseThrow();
assertTrue(frameFn.instructions().stream().anyMatch(instruction ->
instruction.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.POP));
}
@Test
void shouldReportInvalidBarrelFilename() throws IOException {
final var projectRoot = tempDir.resolve("project");
@@ -259,7 +316,8 @@ class PBSFrontendPhaseServiceTest {
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
assertEquals(0, diagnostics.errorCount(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
assertEquals(1, diagnostics.warningCount());
final var targetFn = irBackend.getExecutableFunctions().stream()
.filter(function -> "target".equals(function.callableName()))