implements PLN-0035 pbs inline type hint payload production

This commit is contained in:
2026-04-03 11:35:29 +01:00
parent 3e8f53dc16
commit 09d9bb4c96
14 changed files with 559 additions and 25 deletions
@@ -343,6 +343,7 @@ public final class PbsAst {
public record LetStatement(
boolean isConst,
String name,
Span nameSpan,
TypeRef explicitType,
Expression initializer,
Span span) implements Statement {
@@ -118,6 +118,7 @@ final class PbsBlockParser {
return new PbsAst.LetStatement(
isConst,
name.lexeme(),
context.span(name.start(), name.end()),
explicitType,
initializer,
context.span(letToken.start(), semicolon.end()));
@@ -5,9 +5,12 @@ import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.Model;
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.Scope;
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.TypeView;
import p.studio.compiler.services.PbsInlineHintSurface;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.utilities.structures.ReadOnlyList;
import java.util.ArrayList;
final class PbsFlowBodyAnalyzer {
private final PbsFlowTypeOps typeOps = new PbsFlowTypeOps();
private final PbsFlowExpressionAnalyzer expressionAnalyzer = new PbsFlowExpressionAnalyzer(typeOps);
@@ -16,24 +19,75 @@ final class PbsFlowBodyAnalyzer {
typeOps,
expressionAnalyzer,
this::analyzeBlock);
private final PbsFlowStatementAnalyzer statementAnalyzer = new PbsFlowStatementAnalyzer(
typeOps,
expressionAnalyzer,
assignmentAnalyzer::analyzeAssignmentStatement);
private final PbsFlowCallableBodyAnalyzer callableBodyAnalyzer = new PbsFlowCallableBodyAnalyzer(
typeOps,
completionAnalyzer,
this::analyzeBlock);
private PbsFlowStatementAnalyzer.InlineHintCollector inlineHintCollector = PbsFlowStatementAnalyzer.InlineHintCollector.noop();
public void validate(
final PbsAst.File ast,
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
final FESurfaceContext feSurfaceContext,
final DiagnosticSink diagnostics) {
final var model = Model.from(ast, supplementalTopDecls, feSurfaceContext, diagnostics);
final var previousCollector = inlineHintCollector;
inlineHintCollector = PbsFlowStatementAnalyzer.InlineHintCollector.noop();
try {
final var model = Model.from(ast, supplementalTopDecls, feSurfaceContext, diagnostics);
for (final var topDecl : ast.topDecls()) {
validateTopDecl(topDecl, model, diagnostics);
for (final var topDecl : ast.topDecls()) {
validateTopDecl(topDecl, model, diagnostics);
}
} finally {
inlineHintCollector = previousCollector;
}
}
public ReadOnlyList<PbsInlineHintSurface> collectInlineHints(
final PbsAst.File ast,
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
final FESurfaceContext feSurfaceContext,
final DiagnosticSink diagnostics) {
final var collector = new InlineHintsCollector();
final var previousCollector = inlineHintCollector;
inlineHintCollector = collector;
try {
final var model = Model.from(ast, supplementalTopDecls, feSurfaceContext, diagnostics);
for (final var topDecl : ast.topDecls()) {
validateTopDecl(topDecl, model, diagnostics);
}
} finally {
inlineHintCollector = previousCollector;
}
return collector.hints();
}
private PbsFlowStatementAnalyzer statementAnalyzer() {
return new PbsFlowStatementAnalyzer(
typeOps,
expressionAnalyzer,
assignmentAnalyzer::analyzeAssignmentStatement,
inlineHintCollector);
}
private static final class InlineHintsCollector implements PbsFlowStatementAnalyzer.InlineHintCollector {
private static final String TYPE_HINT_CATEGORY = "type";
private final ArrayList<PbsInlineHintSurface> hints = new ArrayList<>();
private final PbsInlineHintSurfaceFormatter formatter = new PbsInlineHintSurfaceFormatter();
@Override
public void collect(final PbsAst.LetStatement letStatement, final TypeView inferredType) {
final var label = formatter.format(inferredType);
if (label == null || letStatement.nameSpan() == null || letStatement.nameSpan().isNone()) {
return;
}
hints.add(new PbsInlineHintSurface(letStatement.nameSpan(), label, TYPE_HINT_CATEGORY));
}
ReadOnlyList<PbsInlineHintSurface> hints() {
return ReadOnlyList.wrap(hints);
}
}
@@ -194,6 +248,6 @@ final class PbsFlowBodyAnalyzer {
final PbsAst.Block block,
final PbsFlowBodyContext context,
final boolean valueContext) {
return statementAnalyzer.analyzeBlock(block, context, valueContext);
return statementAnalyzer().analyzeBlock(block, context, valueContext);
}
}
@@ -2,6 +2,7 @@ package p.studio.compiler.pbs.semantics;
import p.studio.compiler.messages.FESurfaceContext;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.services.PbsInlineHintSurface;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.utilities.structures.ReadOnlyList;
@@ -19,4 +20,12 @@ public final class PbsFlowSemanticsValidator {
final DiagnosticSink diagnostics) {
flowBodyAnalyzer.validate(ast, supplementalTopDecls, feSurfaceContext, diagnostics);
}
public ReadOnlyList<PbsInlineHintSurface> collectInlineHints(
final PbsAst.File ast,
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
final FESurfaceContext feSurfaceContext,
final DiagnosticSink diagnostics) {
return flowBodyAnalyzer.collectInlineHints(ast, supplementalTopDecls, feSurfaceContext, diagnostics);
}
}
@@ -11,17 +11,30 @@ final class PbsFlowStatementAnalyzer {
void analyze(PbsAst.AssignStatement assignStatement, PbsFlowBodyContext context);
}
@FunctionalInterface
interface InlineHintCollector {
void collect(PbsAst.LetStatement letStatement, TypeView inferredType);
static InlineHintCollector noop() {
return (letStatement, inferredType) -> {
};
}
}
private final PbsFlowTypeOps typeOps;
private final PbsFlowExpressionAnalyzer expressionAnalyzer;
private final AssignmentStatementAnalyzer assignmentStatementAnalyzer;
private final InlineHintCollector inlineHintCollector;
PbsFlowStatementAnalyzer(
final PbsFlowTypeOps typeOps,
final PbsFlowExpressionAnalyzer expressionAnalyzer,
final AssignmentStatementAnalyzer assignmentStatementAnalyzer) {
final AssignmentStatementAnalyzer assignmentStatementAnalyzer,
final InlineHintCollector inlineHintCollector) {
this.typeOps = typeOps;
this.expressionAnalyzer = expressionAnalyzer;
this.assignmentStatementAnalyzer = assignmentStatementAnalyzer;
this.inlineHintCollector = inlineHintCollector;
}
TypeView analyzeBlock(
@@ -96,7 +109,11 @@ final class PbsFlowStatementAnalyzer {
ExprUse.VALUE,
true,
this::analyzeNestedBlock);
scope.bind(letStatement.name(), expected == null ? initializer.type() : expected, !letStatement.isConst());
final var resolvedType = expected == null ? initializer.type() : expected;
scope.bind(letStatement.name(), resolvedType, !letStatement.isConst());
if (letStatement.explicitType() == null) {
inlineHintCollector.collect(letStatement, resolvedType);
}
return;
}
if (statement instanceof PbsAst.ReturnStatement returnStatement) {
@@ -0,0 +1,62 @@
package p.studio.compiler.pbs.semantics;
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.TupleField;
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.TypeView;
final class PbsInlineHintSurfaceFormatter {
String format(final TypeView type) {
if (type == null) {
return null;
}
return switch (type.kind()) {
case UNKNOWN, ASSET_NAMESPACE -> null;
case UNIT -> "void";
case INT, FLOAT, BOOL, STR -> type.name();
case STRUCT,
SERVICE,
CONTRACT,
CALLBACK,
ENUM,
ERROR,
TYPE_REF,
ADDRESSABLE -> type.name();
case OPTIONAL -> {
final var inner = format(type.inner());
yield inner == null ? null : "optional " + inner;
}
case RESULT -> {
final var errorType = format(type.errorType());
final var payloadType = format(type.inner());
if (errorType == null) {
yield null;
}
yield payloadType == null
? "result<%s>".formatted(errorType)
: "result<%s> %s".formatted(errorType, payloadType);
}
case TUPLE -> formatTuple(type.tupleFields());
};
}
private String formatTuple(final java.util.List<TupleField> fields) {
if (fields.isEmpty()) {
return "()";
}
final var builder = new StringBuilder("(");
for (int i = 0; i < fields.size(); i++) {
if (i > 0) {
builder.append(", ");
}
final var field = fields.get(i);
final var fieldType = format(field.type());
if (fieldType == null) {
return null;
}
builder.append(field.label())
.append(": ")
.append(fieldType);
}
builder.append(')');
return builder.toString();
}
}
@@ -13,6 +13,7 @@ import p.studio.compiler.models.IRHiddenGlobalKind;
import p.studio.compiler.models.IRSyntheticCallableKind;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.pbs.PbsReservedMetadataExtractor;
import p.studio.compiler.pbs.semantics.PbsFlowSemanticsValidator;
import p.studio.compiler.pbs.stdlib.InterfaceModuleLoader;
import p.studio.compiler.pbs.stdlib.ResourceStdlibEnvironmentResolver;
import p.studio.compiler.pbs.stdlib.StdlibEnvironmentResolver;
@@ -112,16 +113,52 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
final FrontendPhaseContext ctx,
final DiagnosticSink diagnostics,
final BuildingIssueSink issues) {
return semanticReadSurface(ctx, diagnostics, issues).supplementalTopDeclsByFile();
}
public static Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile(
final FrontendPhaseContext ctx,
final DiagnosticSink diagnostics,
final BuildingIssueSink issues) {
return semanticReadSurface(ctx, diagnostics, issues).inlineHintsByFile();
}
public static PbsSemanticReadSurface semanticReadSurface(
final FrontendPhaseContext ctx,
final DiagnosticSink diagnostics,
final BuildingIssueSink issues) {
final var service = new PBSFrontendPhaseService();
final var assembly = service.moduleAssemblyService.assemble(ctx, ctx.nameTable(), diagnostics, issues);
final var importedSemanticContexts = service.importedSemanticContextService.build(
assembly.parsedSourceFiles(),
assembly.moduleTable());
final var flowSemanticsValidator = new PbsFlowSemanticsValidator();
final Map<FileId, ReadOnlyList<p.studio.compiler.pbs.ast.PbsAst.TopDecl>> supplementalTopDeclsByFile = new LinkedHashMap<>();
final Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile = new LinkedHashMap<>();
for (final var entry : importedSemanticContexts.entrySet()) {
supplementalTopDeclsByFile.put(entry.getKey(), entry.getValue().supplementalTopDecls());
}
return Map.copyOf(supplementalTopDeclsByFile);
for (final var parsedSourceFile : assembly.parsedSourceFiles()) {
final var importedSemanticContext = importedSemanticContexts.getOrDefault(
parsedSourceFile.fileId(),
PbsImportedSemanticContext.empty());
final var inlineHints = flowSemanticsValidator.collectInlineHints(
parsedSourceFile.ast(),
importedSemanticContext.supplementalTopDecls(),
ctx.feSurfaceContext(),
diagnostics);
if (!inlineHints.isEmpty()) {
inlineHintsByFile.put(parsedSourceFile.fileId(), inlineHints);
}
}
return new PbsSemanticReadSurface(
Map.copyOf(supplementalTopDeclsByFile),
Map.copyOf(inlineHintsByFile));
}
public record PbsSemanticReadSurface(
Map<FileId, ReadOnlyList<p.studio.compiler.pbs.ast.PbsAst.TopDecl>> supplementalTopDeclsByFile,
Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile) {
}
private IRBackend mergeCompiledSources(
@@ -0,0 +1,23 @@
package p.studio.compiler.services;
import p.studio.compiler.source.Span;
import java.util.Objects;
public record PbsInlineHintSurface(
Span anchorSpan,
String label,
String category) {
public PbsInlineHintSurface {
anchorSpan = Objects.requireNonNull(anchorSpan, "anchorSpan");
label = Objects.requireNonNull(label, "label").trim();
category = Objects.requireNonNull(category, "category").trim();
if (label.isBlank()) {
throw new IllegalArgumentException("label cannot be blank");
}
if (category.isBlank()) {
throw new IllegalArgumentException("category cannot be blank");
}
}
}
@@ -194,6 +194,29 @@ class PbsParserTest {
assertFalse(diagnostics.isEmpty(), "Parser should reject reserved keyword 'error' as callable/member name");
}
@Test
void shouldTrackLetNameSpanSeparatelyFromWholeStatementSpan() {
final var source = """
fn main() -> void {
let inferred = 1;
return;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var fileId = new FileId(0);
final PbsAst.File ast = PbsParser.parse(PbsLexer.lex(source, fileId, diagnostics), fileId, diagnostics);
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
final var functionDecl = assertInstanceOf(PbsAst.FunctionDecl.class, ast.topDecls().getFirst());
final var letStatement = assertInstanceOf(PbsAst.LetStatement.class, functionDecl.body().statements().getFirst());
assertEquals("inferred", letStatement.name());
assertEquals(source.indexOf("inferred"), letStatement.nameSpan().getStart());
assertEquals(source.indexOf("inferred") + "inferred".length(), letStatement.nameSpan().getEnd());
assertTrue(letStatement.span().getStart() < letStatement.nameSpan().getStart());
assertTrue(letStatement.span().getEnd() > letStatement.nameSpan().getEnd());
}
@Test
void shouldAcceptStructFieldTrailingComma() {
final var source = "declare struct S(a: int,);";
@@ -20,6 +20,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.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.source.identifiers.ProjectId;
import p.studio.compiler.source.tables.FileTable;
import p.studio.compiler.source.tables.ProjectTable;
@@ -1868,13 +1869,127 @@ class PBSFrontendPhaseServiceTest {
allowedDiagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
}
private void registerFile(
@Test
void shouldProduceInlineHintsForEligibleInferredLetBindings() throws IOException {
final var projectRoot = tempDir.resolve("project-inline-hints");
final var sourceRoot = projectRoot.resolve("src");
Files.createDirectories(sourceRoot);
final var sourceFile = sourceRoot.resolve("main.pbs");
final var modBarrel = sourceRoot.resolve("mod.barrel");
Files.writeString(sourceFile, """
declare struct Player(score: int);
declare error Failure {
Boom;
}
fn fetch_pair() -> result<Failure> (left: int, right: int) {
return ok((left: 1, right: 2));
}
fn main() -> void {
let scalar = 1;
let player = new Player(1);
let maybe = some(1);
let pair = (left: 1, right: 2);
let outcome = fetch_pair();
return;
}
""");
Files.writeString(modBarrel, "pub fn main() -> void;");
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());
final var sourceFileId = 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 inlineHintsByFile = PBSFrontendPhaseService.inlineHintsByFile(
ctx,
diagnostics,
BuildingIssueSink.empty());
final var sourceHints = inlineHintsByFile.get(sourceFileId);
assertNotNull(sourceHints, () -> "diagnostics="
+ diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList()
+ ", keys=" + inlineHintsByFile.keySet());
final var labels = sourceHints.asList().stream()
.map(PbsInlineHintSurface::label)
.toList();
assertEquals(List.of(
"int",
"Player",
"optional int",
"(left: int, right: int)",
"result<Failure> (left: int, right: int)"),
labels);
assertEquals(0, diagnostics.errorCount(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
}
@Test
void shouldSuppressInlineHintsForExplicitlyTypedLetBindings() throws IOException {
final var projectRoot = tempDir.resolve("project-inline-hints-explicit");
final var sourceRoot = projectRoot.resolve("src");
Files.createDirectories(sourceRoot);
final var sourceFile = sourceRoot.resolve("main.pbs");
final var modBarrel = sourceRoot.resolve("mod.barrel");
Files.writeString(sourceFile, """
fn main() -> void {
let inferred = 1;
let explicit: int = 2;
return;
}
""");
Files.writeString(modBarrel, "pub fn main() -> void;");
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());
final var sourceFileId = 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 inlineHints = PBSFrontendPhaseService.inlineHintsByFile(
ctx,
diagnostics,
BuildingIssueSink.empty()).get(sourceFileId);
assertEquals(1, inlineHints.size());
assertEquals("int", inlineHints.getFirst().label());
assertEquals("type", inlineHints.getFirst().category());
}
private FileId registerFile(
final ProjectId projectId,
final Path projectRoot,
final Path file,
final FileTable fileTable) throws IOException {
final BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class);
fileTable.register(new SourceHandle(
return fileTable.register(new SourceHandle(
projectId,
projectRoot.relativize(file),
file,