diff --git a/src/wren_compiler.c b/src/wren_compiler.c index 9a41c980..79fea9b1 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -1852,8 +1852,16 @@ static void super_(Compiler* compiler, bool allowAssignment) { // No explicit name, so use the name of the enclosing method. char name[MAX_METHOD_SIGNATURE]; - int length = enclosingClass->methodLength; - strncpy(name, enclosingClass->methodName, length); + int length; + if (enclosingClass != NULL) { + length = enclosingClass->methodLength; + strncpy(name, enclosingClass->methodName, length); + } else { + // The compiler errored since super is called outside a method. + // We want to continue, but we cannot generate valid bytecode. + length = 0; + strncpy(name, "", length); + } // Call the superclass method with the same name. methodCall(compiler, CODE_SUPER_0, name, length); diff --git a/test/super/super_at_top_level.wren b/test/super/super_at_top_level.wren index 4efe0ea6..eb2b1252 100644 --- a/test/super/super_at_top_level.wren +++ b/test/super/super_at_top_level.wren @@ -1 +1,4 @@ super.foo // expect error +super // expect error +super.foo("bar") // expect error +super("foo") // expect error