implements PR015

This commit is contained in:
2026-03-24 13:42:21 +00:00
parent 4f32913577
commit 8d8c3dc180
10 changed files with 132 additions and 141 deletions
@@ -29,12 +29,20 @@ final class PbsDeclarationRuleValidator {
final PbsAst.ReturnKind returnKind,
final PbsAst.TypeRef returnType,
final PbsAst.TypeRef resultErrorType,
final Span span) {
final Span span,
final boolean allowSelf) {
validateParameters(parameters, "callable '%s'".formatted(callableName));
validateReturnSurface(returnKind, returnType, resultErrorType, "callable '%s'".formatted(callableName), span);
validateReturnSurface(
returnKind,
returnType,
resultErrorType,
"callable '%s'".formatted(callableName),
span,
allowSelf);
PbsTypeSurfaceSemanticsValidator.validateTypeSurfaceRecursive(
parameters.stream().map(PbsAst.Parameter::typeRef).toList(),
"callable '%s'".formatted(callableName),
allowSelf,
diagnostics);
}
@@ -58,8 +66,9 @@ final class PbsDeclarationRuleValidator {
void validateTypeSurfaces(
final List<PbsAst.TypeRef> typeRoots,
final String ownerDescription) {
PbsTypeSurfaceSemanticsValidator.validateTypeSurfaceRecursive(typeRoots, ownerDescription, diagnostics);
final String ownerDescription,
final boolean allowSelf) {
PbsTypeSurfaceSemanticsValidator.validateTypeSurfaceRecursive(typeRoots, ownerDescription, allowSelf, diagnostics);
}
void validateErrorDeclaration(final PbsAst.ErrorDecl errorDecl) {
@@ -114,10 +123,12 @@ final class PbsDeclarationRuleValidator {
callbackDecl.returnType(),
callbackDecl.resultErrorType(),
"callback '%s'".formatted(callbackDecl.name()),
callbackDecl.span());
callbackDecl.span(),
false);
PbsTypeSurfaceSemanticsValidator.validateTypeSurfaceRecursive(
callbackDecl.parameters().stream().map(PbsAst.Parameter::typeRef).toList(),
"callback '%s'".formatted(callbackDecl.name()),
false,
diagnostics);
}
@@ -131,6 +142,7 @@ final class PbsDeclarationRuleValidator {
PbsTypeSurfaceSemanticsValidator.validateTypeSurfaceRecursive(
List.of(constDecl.explicitType()),
"const '%s'".formatted(constDecl.name()),
false,
diagnostics);
}
@@ -147,7 +159,8 @@ final class PbsDeclarationRuleValidator {
final PbsAst.TypeRef returnType,
final PbsAst.TypeRef resultErrorType,
final String ownerDescription,
final Span span) {
final Span span,
final boolean allowSelf) {
if (returnKind == PbsAst.ReturnKind.RESULT && PbsTypeSurfaceSemanticsValidator.isOptionalType(returnType)) {
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
PbsSemanticsErrors.E_SEM_INVALID_MIXED_OPTIONAL_RESULT_RETURN.name(),
@@ -160,10 +173,10 @@ final class PbsDeclarationRuleValidator {
}
if (returnType != null) {
PbsTypeSurfaceSemanticsValidator.validateTypeSurfaceRecursive(List.of(returnType), ownerDescription, diagnostics);
PbsTypeSurfaceSemanticsValidator.validateTypeSurfaceRecursive(List.of(returnType), ownerDescription, allowSelf, diagnostics);
}
if (resultErrorType != null) {
PbsTypeSurfaceSemanticsValidator.validateTypeSurfaceRecursive(List.of(resultErrorType), ownerDescription, diagnostics);
PbsTypeSurfaceSemanticsValidator.validateTypeSurfaceRecursive(List.of(resultErrorType), ownerDescription, allowSelf, diagnostics);
}
}
@@ -23,6 +23,7 @@ public final class PbsDeclarationSemanticsValidator {
functionDecl.returnType(),
functionDecl.resultErrorType(),
functionDecl.span(),
false,
binder,
rules);
continue;
@@ -83,6 +84,10 @@ public final class PbsDeclarationSemanticsValidator {
final PbsDeclarationRuleValidator rules,
final DiagnosticSink diagnostics) {
final var ownerNameId = nameTable.register(structDecl.name());
rules.validateTypeSurfaces(
structDecl.fields().stream().map(PbsAst.StructField::typeRef).toList(),
"struct '%s'".formatted(structDecl.name()),
false);
final var methodScope = PbsCallableScope.structMethods(ownerNameId);
for (final var method : structDecl.methods()) {
validateCallable(
@@ -93,6 +98,7 @@ public final class PbsDeclarationSemanticsValidator {
method.returnType(),
method.resultErrorType(),
method.span(),
true,
binder,
rules);
}
@@ -102,7 +108,8 @@ public final class PbsDeclarationSemanticsValidator {
rules.validateParameters(ctor.parameters(), "ctor '%s'".formatted(ctor.name()));
rules.validateTypeSurfaces(
ctor.parameters().stream().map(PbsAst.Parameter::typeRef).toList(),
"ctor '%s'".formatted(ctor.name()));
"ctor '%s'".formatted(ctor.name()),
true);
binder.registerCtor(ctorScope, ctor.name(), PbsCallableShape.ctorShapeKey(ctor.parameters()), ctor.span());
if (PbsCtorReturnScanner.containsReturnStatement(ctor.body())) {
@@ -128,6 +135,7 @@ public final class PbsDeclarationSemanticsValidator {
method.returnType(),
method.resultErrorType(),
method.span(),
true,
binder,
rules);
}
@@ -147,6 +155,7 @@ public final class PbsDeclarationSemanticsValidator {
signature.returnType(),
signature.resultErrorType(),
signature.span(),
false,
binder,
rules);
}
@@ -167,6 +176,7 @@ public final class PbsDeclarationSemanticsValidator {
method.returnType(),
method.resultErrorType(),
method.span(),
false,
binder,
rules);
}
@@ -180,9 +190,10 @@ public final class PbsDeclarationSemanticsValidator {
final PbsAst.TypeRef returnType,
final PbsAst.TypeRef resultErrorType,
final Span span,
final boolean allowSelf,
final PbsNamespaceBinder binder,
final PbsDeclarationRuleValidator rules) {
rules.validateCallableHeader(callableName, parameters, returnKind, returnType, resultErrorType, span);
rules.validateCallableHeader(callableName, parameters, returnKind, returnType, resultErrorType, span, allowSelf);
binder.registerCallable(
scope,
callableName,
@@ -97,8 +97,15 @@ final class PbsFlowExpressionAnalyzer {
if (expression instanceof PbsAst.UnitExpr) {
return ExprResult.type(TypeView.unit());
}
if (expression instanceof PbsAst.ThisExpr) {
return ExprResult.type(receiverType == null ? TypeView.unknown() : receiverType);
if (expression instanceof PbsAst.ThisExpr thisExpr) {
if (receiverType == null) {
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
PbsSemanticsErrors.E_SEM_INVALID_THIS_CONTEXT.name(),
"Invalid 'this' usage outside struct/service methods and constructors",
thisExpr.span());
return ExprResult.type(TypeView.unknown());
}
return ExprResult.type(receiverType);
}
if (expression instanceof PbsAst.IdentifierExpr identifierExpr) {
final var localType = scope.resolve(identifierExpr.name());
@@ -18,6 +18,8 @@ public enum PbsSemanticsErrors {
E_SEM_APPLY_AMBIGUOUS_OVERLOAD,
E_SEM_BARE_METHOD_EXTRACTION,
E_SEM_INVALID_MEMBER_ACCESS,
E_SEM_INVALID_THIS_CONTEXT,
E_SEM_INVALID_SELF_CONTEXT,
E_SEM_IF_NON_BOOL_CONDITION,
E_SEM_IF_BRANCH_TYPE_MISMATCH,
E_SEM_SWITCH_SELECTOR_INVALID,
@@ -13,6 +13,7 @@ final class PbsTypeSurfaceSemanticsValidator {
static void validateTypeSurfaceRecursive(
final List<PbsAst.TypeRef> roots,
final String ownerDescription,
final boolean allowSelf,
final DiagnosticSink diagnostics) {
final var pending = new ArrayList<>(roots);
while (!pending.isEmpty()) {
@@ -37,6 +38,14 @@ final class PbsTypeSurfaceSemanticsValidator {
pending.add(field.typeRef());
}
}
case SELF -> {
if (!allowSelf) {
p.studio.compiler.source.diagnostics.Diagnostics.error(diagnostics,
PbsSemanticsErrors.E_SEM_INVALID_SELF_CONTEXT.name(),
"Invalid 'Self' type usage in %s".formatted(ownerDescription),
typeRef.span());
}
}
default -> {
// No recursive children.
}