implements PLN-0007
This commit is contained in:
+102
@@ -143,6 +143,7 @@ final class PbsExecutableBodyLowerer {
|
||||
case PbsAst.WhileStatement whileStatement -> lowerWhileStatement(whileStatement, context);
|
||||
case PbsAst.ExpressionStatement expressionStatement -> {
|
||||
lowerExpression(expressionStatement.expression(), context);
|
||||
discardIgnoredValueSlots(expressionStatement.expression(), context);
|
||||
yield false;
|
||||
}
|
||||
case PbsAst.BreakStatement breakStatement -> lowerBreakStatement(breakStatement, context);
|
||||
@@ -426,6 +427,21 @@ final class PbsExecutableBodyLowerer {
|
||||
}
|
||||
}
|
||||
|
||||
private void discardIgnoredValueSlots(
|
||||
final PbsAst.Expression expression,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
final var slots = materializedValueSlots(expression, context);
|
||||
for (int i = 0; i < slots; i++) {
|
||||
context.instructions().add(new IRBackendExecutableFunction.Instruction(
|
||||
IRBackendExecutableFunction.InstructionKind.POP,
|
||||
"",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
expression == null ? p.studio.compiler.source.Span.none() : expression.span()));
|
||||
}
|
||||
}
|
||||
|
||||
private void lowerMemberExpression(
|
||||
final PbsAst.MemberExpr memberExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
@@ -508,6 +524,92 @@ final class PbsExecutableBodyLowerer {
|
||||
return receiverPath + "." + memberExpr.memberName();
|
||||
}
|
||||
|
||||
private int materializedValueSlots(
|
||||
final PbsAst.Expression expression,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
if (expression == null) {
|
||||
return 0;
|
||||
}
|
||||
return switch (expression) {
|
||||
case PbsAst.CallExpr callExpr -> callsiteEmitter.returnSlotsOf(callExpr, context);
|
||||
case PbsAst.ApplyExpr ignored -> 0;
|
||||
case PbsAst.BinaryExpr ignored -> 1;
|
||||
case PbsAst.UnaryExpr ignored -> 1;
|
||||
case PbsAst.ElseExpr elseExpr ->
|
||||
Math.max(
|
||||
materializedValueSlots(elseExpr.optionalExpression(), context),
|
||||
materializedValueSlots(elseExpr.fallbackExpression(), context));
|
||||
case PbsAst.IfExpr ifExpr ->
|
||||
Math.max(
|
||||
blockValueSlots(ifExpr.thenBlock(), context),
|
||||
materializedValueSlots(ifExpr.elseExpression(), context));
|
||||
case PbsAst.SwitchExpr ignored -> 0;
|
||||
case PbsAst.HandleExpr ignored -> 0;
|
||||
case PbsAst.AsExpr asExpr -> materializedValueSlots(asExpr.expression(), context);
|
||||
case PbsAst.MemberExpr memberExpr -> {
|
||||
final var assetReference = flattenAssetReference(memberExpr);
|
||||
if (assetReference != null && context.resolveAssetId(assetReference) != null) {
|
||||
yield 1;
|
||||
}
|
||||
yield materializedValueSlots(memberExpr.receiver(), context);
|
||||
}
|
||||
case PbsAst.PropagateExpr propagateExpr -> materializedValueSlots(propagateExpr.expression(), context);
|
||||
case PbsAst.GroupExpr groupExpr -> materializedValueSlots(groupExpr.expression(), context);
|
||||
case PbsAst.NewExpr newExpr -> expressionListValueSlots(newExpr.arguments(), context);
|
||||
case PbsAst.BindExpr bindExpr -> materializedValueSlots(bindExpr.contextExpression(), context);
|
||||
case PbsAst.SomeExpr someExpr -> materializedValueSlots(someExpr.value(), context);
|
||||
case PbsAst.OkExpr okExpr -> materializedValueSlots(okExpr.value(), context);
|
||||
case PbsAst.TupleExpr tupleExpr -> {
|
||||
var slots = 0;
|
||||
for (final var item : tupleExpr.items()) {
|
||||
slots += materializedValueSlots(item.expression(), context);
|
||||
}
|
||||
yield slots;
|
||||
}
|
||||
case PbsAst.BlockExpr blockExpr -> blockValueSlots(blockExpr.block(), context);
|
||||
case PbsAst.IdentifierExpr identifierExpr -> producesIdentifierValue(identifierExpr, context) ? 1 : 0;
|
||||
case PbsAst.IntLiteralExpr ignored -> 1;
|
||||
case PbsAst.StringLiteralExpr ignoredString -> 1;
|
||||
case PbsAst.BoolLiteralExpr ignoredBool -> 1;
|
||||
case PbsAst.FloatLiteralExpr ignoredFloat -> 0;
|
||||
case PbsAst.BoundedLiteralExpr ignoredBounded -> 0;
|
||||
case PbsAst.ThisExpr ignoredThis -> 0;
|
||||
case PbsAst.NoneExpr ignoredNone -> 0;
|
||||
case PbsAst.ErrExpr ignoredErr -> 0;
|
||||
case PbsAst.UnitExpr ignoredUnit -> 0;
|
||||
};
|
||||
}
|
||||
|
||||
private int blockValueSlots(
|
||||
final PbsAst.Block block,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
if (block == null || block.tailExpression() == null) {
|
||||
return 0;
|
||||
}
|
||||
return materializedValueSlots(block.tailExpression(), context);
|
||||
}
|
||||
|
||||
private int expressionListValueSlots(
|
||||
final ReadOnlyList<PbsAst.Expression> expressions,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
var slots = 0;
|
||||
for (final var expression : expressions) {
|
||||
slots += materializedValueSlots(expression, context);
|
||||
}
|
||||
return slots;
|
||||
}
|
||||
|
||||
private boolean producesIdentifierValue(
|
||||
final PbsAst.IdentifierExpr identifierExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
if ("assets".equals(identifierExpr.name())) {
|
||||
return false;
|
||||
}
|
||||
return context.resolveLocalSlot(identifierExpr.name()) != null
|
||||
|| context.resolveGlobalSlot(identifierExpr.name()) != null
|
||||
|| context.resolveConstDecl(identifierExpr.name()) != null;
|
||||
}
|
||||
|
||||
private void lowerConstExpression(
|
||||
final PbsAst.Expression expression,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
|
||||
+23
@@ -16,6 +16,29 @@ import java.util.Map;
|
||||
import static p.studio.compiler.pbs.lowering.PbsExecutableLoweringModels.*;
|
||||
|
||||
final class PbsExecutableCallsiteEmitter {
|
||||
int returnSlotsOf(
|
||||
final PbsAst.CallExpr callExpr,
|
||||
final PbsExecutableLoweringContext context) {
|
||||
final var calleeIdentity = resolveCalleeIdentity(callExpr.callee(), context.nameTable());
|
||||
if (calleeIdentity == null) {
|
||||
return 0;
|
||||
}
|
||||
final var candidates = resolveCallsiteCandidates(callExpr, calleeIdentity, context);
|
||||
final var resolvedCategory = resolveCallsiteCategory(candidates, callExpr, context);
|
||||
if (resolvedCategory == null) {
|
||||
return 0;
|
||||
}
|
||||
return switch (resolvedCategory) {
|
||||
case HOST -> candidates.hostCandidates().getFirst().retSlots();
|
||||
case INTRINSIC -> candidates.intrinsicCandidates().getFirst().retSlots();
|
||||
case CALLABLE -> {
|
||||
if (candidates.callableCandidates().size() != 1) {
|
||||
yield 0;
|
||||
}
|
||||
yield context.returnSlotsByCallableId().getOrDefault(candidates.callableCandidates().getFirst(), 0);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void emitCallsite(
|
||||
final PbsAst.CallExpr callExpr,
|
||||
|
||||
+8
-2
@@ -172,7 +172,7 @@ final class PbsFlowStatementAnalyzer {
|
||||
return;
|
||||
}
|
||||
if (statement instanceof PbsAst.ExpressionStatement expressionStatement) {
|
||||
expressionAnalyzer.analyzeExpression(
|
||||
final var expressionType = expressionAnalyzer.analyzeExpression(
|
||||
expressionStatement.expression(),
|
||||
scope,
|
||||
null,
|
||||
@@ -183,7 +183,13 @@ final class PbsFlowStatementAnalyzer {
|
||||
diagnostics,
|
||||
ExprUse.VALUE,
|
||||
false,
|
||||
this::analyzeNestedBlock);
|
||||
this::analyzeNestedBlock).type();
|
||||
if (typeOps.isMaterializedValue(expressionType)) {
|
||||
Diagnostics.warning(diagnostics,
|
||||
PbsSemanticsErrors.W_SEM_IGNORED_VALUE.name(),
|
||||
"Ignored materialized value in expression statement",
|
||||
expressionStatement.span());
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (statement instanceof PbsAst.AssignStatement assignStatement) {
|
||||
|
||||
+10
@@ -139,6 +139,16 @@ final class PbsFlowTypeOps {
|
||||
return type.kind() == Kind.STR || type.kind() == Kind.UNKNOWN;
|
||||
}
|
||||
|
||||
boolean isMaterializedValue(final TypeView type) {
|
||||
if (type == null) {
|
||||
return false;
|
||||
}
|
||||
return switch (type.kind()) {
|
||||
case UNKNOWN, UNIT, ASSET_NAMESPACE -> false;
|
||||
default -> true;
|
||||
};
|
||||
}
|
||||
|
||||
private boolean tupleCompatible(final TypeView actual, final TypeView expected) {
|
||||
if (actual.tupleFields().size() != expected.tupleFields().size()) {
|
||||
return false;
|
||||
|
||||
+1
@@ -69,4 +69,5 @@ public enum PbsSemanticsErrors {
|
||||
E_SEM_HANDLE_ERROR_MISMATCH,
|
||||
E_SEM_HANDLE_ARM_TERMINAL_INVALID,
|
||||
E_SEM_EXEC_LOWERING_UNRESOLVED_CALLEE,
|
||||
W_SEM_IGNORED_VALUE,
|
||||
}
|
||||
|
||||
+23
@@ -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();
|
||||
|
||||
+64
-4
@@ -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 = """
|
||||
|
||||
+48
@@ -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());
|
||||
}
|
||||
}
|
||||
+60
-2
@@ -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()))
|
||||
|
||||
Reference in New Issue
Block a user