diff --git a/src/vm/wren_compiler.c b/src/vm/wren_compiler.c index 7b85760b..9a9c3336 100644 --- a/src/vm/wren_compiler.c +++ b/src/vm/wren_compiler.c @@ -2150,11 +2150,6 @@ static void super_(Compiler* compiler, bool allowAssignment) { error(compiler, "Cannot use 'super' outside of a method."); } - else if (enclosingClass->inStatic) - { - // TODO: Why not? - error(compiler, "Cannot use 'super' in a static method."); - } loadThis(compiler); diff --git a/src/vm/wren_vm.c b/src/vm/wren_vm.c index 03adf27a..7e902ebf 100644 --- a/src/vm/wren_vm.c +++ b/src/vm/wren_vm.c @@ -300,13 +300,16 @@ static WrenForeignMethodFn findForeignMethod(WrenVM* vm, static Value bindMethod(WrenVM* vm, int methodType, int symbol, ObjModule* module, ObjClass* classObj, Value methodValue) { + const char* className = classObj->name->value; + if (methodType == CODE_METHOD_STATIC) classObj = classObj->obj.classObj; + Method method; if (IS_STRING(methodValue)) { const char* name = AS_CSTRING(methodValue); method.type = METHOD_FOREIGN; method.fn.foreign = findForeignMethod(vm, module->name->value, - classObj->name->value, + className, methodType == CODE_METHOD_STATIC, name); @@ -322,16 +325,13 @@ static Value bindMethod(WrenVM* vm, int methodType, int symbol, ObjFn* methodFn = IS_FN(methodValue) ? AS_FN(methodValue) : AS_CLOSURE(methodValue)->fn; - // Methods are always bound against the class, and not the metaclass, even - // for static methods, because static methods don't have instance fields - // anyway. + // Patch up the bytecode now that we know the superclass. wrenBindMethodCode(classObj, methodFn); method.type = METHOD_BLOCK; method.fn.obj = AS_OBJ(methodValue); } - if (methodType == CODE_METHOD_STATIC) classObj = classObj->obj.classObj; wrenBindMethod(vm, classObj, symbol, method); return NULL_VAL; } diff --git a/test/language/super/super_in_static_method.wren b/test/language/super/super_in_static_method.wren index 7e350dc5..65bd5274 100644 --- a/test/language/super/super_in_static_method.wren +++ b/test/language/super/super_in_static_method.wren @@ -1,5 +1,8 @@ class Foo { - static method { - super // expect error + static name { + System.print("Foo.name") // expect: Foo.name + System.print(super) // expect: Foo } } + +Foo.name