NumberFour / n4js

Type-Safety of Java & Javascript Flexibility
http://numberfour.github.io/n4js/
38 stars 8 forks source link

#75: Shadowing superclass identifier causes runtime failure for super calls #389

Closed lbeurerkellner closed 6 years ago

lbeurerkellner commented 7 years ago

Moved: https://github.com/eclipse/n4js/issues/75

It is currently possible to shadow the superclass in a method by locally redefining its name. This causes problems when using the super keyword to call a super method:

class SomeClass {
    method() {
        console.log("Super.method");
    }
}

export public class Nested extends SomeClass {
    a() {
        var SomeClass = 4;
        super.method();
    }
}

var n = new Nested();
n.a();

The execution of this script results in the following output

(SystemJS) Cannot read property 'method' of undefined
    TypeError: Cannot read property 'method' of undefined
        at Nested.a___n4 (.../n4js-master-two/Eclipse.app/Contents/MacOS/workspace/InterfaceSuperAccess/src-gen/es/InterfaceSuperAccess/Nested.js:43:27)
        at execute (.../n4js-master-two/Eclipse.app/Contents/MacOS/workspace/InterfaceSuperAccess/src-gen/es/InterfaceSuperAccess/Nested.js:73:7)
    Error loading InterfaceSuperAccess/Nested

In the following snippet from the generated code, the cause of the issue becomes clear:

   ...
$makeClass(Nested, SomeClass, [], {
    a: {
        value: function a___n4() {
            var SomeClass = 4;
            SomeClass.prototype.method.call(this);
            }
        }
    },...