From faa7ac69bb393ac5d14cbdb0e79ffb858f208360 Mon Sep 17 00:00:00 2001 From: Daniel Chao Date: Fri, 15 Mar 2024 07:53:09 -0700 Subject: [PATCH] Fix amending module with abstract class (#319) This fixes an assertion error that gets thrown if: 1. A module declares a class as abstract 2. An amending module does not use that abstract class as a type Underneath the hood, the modifiers of the class/typelias object member is considered different from the modifiers on the VmClass/VmTypeAlias values. The object model skips forcing any explicitly members that are explicitly declared hidden, abstract, or local. However, it _should_ evaluate any abstract classes found in a module. So, it's incorrect to apply the same modifiers on the class to the object member. --- .../src/main/java/org/pkl/core/ast/VmModifier.java | 8 ++++++++ .../java/org/pkl/core/ast/builder/AstBuilder.java | 8 ++++++-- .../input-helper/modules/Birds.pkl | 12 ++++++++++++ .../input/modules/amendModule6.pkl | 1 + .../output/modules/amendModule6.pcf | 1 + 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 pkl-core/src/test/files/LanguageSnippetTests/input-helper/modules/Birds.pkl create mode 100644 pkl-core/src/test/files/LanguageSnippetTests/input/modules/amendModule6.pkl create mode 100644 pkl-core/src/test/files/LanguageSnippetTests/output/modules/amendModule6.pcf diff --git a/pkl-core/src/main/java/org/pkl/core/ast/VmModifier.java b/pkl-core/src/main/java/org/pkl/core/ast/VmModifier.java index 7e63ab28..a9050c8d 100644 --- a/pkl-core/src/main/java/org/pkl/core/ast/VmModifier.java +++ b/pkl-core/src/main/java/org/pkl/core/ast/VmModifier.java @@ -72,6 +72,14 @@ public final class VmModifier { public static final int VALID_OBJECT_MEMBER_MODIFIERS = LOCAL; + public static final int TYPEALIAS_OBJECT_MEMBER = TYPE_ALIAS | CONST; + + public static final int LOCAL_TYPEALIAS_OBJECT_MEMBER = LOCAL | TYPEALIAS_OBJECT_MEMBER; + + public static final int CLASS_OBJECT_MEMBER = CLASS | CONST; + + public static final int LOCAL_CLASS_OBJECT_MEMBER = LOCAL | CLASS_OBJECT_MEMBER; + public static boolean isLocal(int modifiers) { return (modifiers & LOCAL) != 0; } diff --git a/pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java b/pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java index d63cd935..35f43bfc 100644 --- a/pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java +++ b/pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java @@ -305,11 +305,13 @@ public final class AstBuilder extends AbstractAstBuilder { doVisitClassProperties(propertyCtxs, propertyNames), doVisitMethodDefs(methodCtxs)); + var isLocal = VmModifier.isLocal(modifiers); + var result = new ObjectMember( sourceSection, headerSection, - modifiers | VmModifier.CONST, + isLocal ? VmModifier.LOCAL_CLASS_OBJECT_MEMBER : VmModifier.CLASS_OBJECT_MEMBER, scope.getName(), scope.getQualifiedName()); @@ -360,7 +362,9 @@ public final class AstBuilder extends AbstractAstBuilder { new ObjectMember( sourceSection, headerSection, - modifiers | VmModifier.CONST, + isLocal + ? VmModifier.LOCAL_TYPEALIAS_OBJECT_MEMBER + : VmModifier.TYPEALIAS_OBJECT_MEMBER, scopeName, scope.getQualifiedName()); diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input-helper/modules/Birds.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input-helper/modules/Birds.pkl new file mode 100644 index 00000000..2baf72f4 --- /dev/null +++ b/pkl-core/src/test/files/LanguageSnippetTests/input-helper/modules/Birds.pkl @@ -0,0 +1,12 @@ +module Birds + +abstract class Bird { + name: String +} + +class Pigeon extends Bird { + name = "Pigeon" + passenger: Boolean +} + +pidgy: Pigeon? diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input/modules/amendModule6.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input/modules/amendModule6.pkl new file mode 100644 index 00000000..a63ca8d2 --- /dev/null +++ b/pkl-core/src/test/files/LanguageSnippetTests/input/modules/amendModule6.pkl @@ -0,0 +1 @@ +amends ".../input-helper/modules/Birds.pkl" diff --git a/pkl-core/src/test/files/LanguageSnippetTests/output/modules/amendModule6.pcf b/pkl-core/src/test/files/LanguageSnippetTests/output/modules/amendModule6.pcf new file mode 100644 index 00000000..6bca070a --- /dev/null +++ b/pkl-core/src/test/files/LanguageSnippetTests/output/modules/amendModule6.pcf @@ -0,0 +1 @@ +pidgy = null