Skip to content

Use flow analysis to remove unused prototype methods #666

@dimvar

Description

@dimvar

If you compile the following example in advanced mode, Child1#method123 is not removed, even though it is never used. The reason is that the compiler doesn't know that x in x.method123(0) can only ever be a Child2. It would be nice if we can do some simple flow analysis (eg, 0CFA) to catch things like that and remove more dead code.

/** @interface */
function Parent() {}
/** @return {number} */
Parent.prototype.method123 = function(x) {};

/** @constructor @implements {Parent} */
function Child1() { this.prop = 123; }
/** @override */
Child1.prototype.method123 = function(x) { return x+1; };

/** @constructor @implements {Parent} */
function Child2() {}
/** @override */
Child2.prototype.method123 = function(x) { return x-1; };

/** @param {!Parent} x */
function f(x) {
  alert(x.method123(0));
}

alert((new Child1).prop);
f(new Child2);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions