closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

ADVANCED mode removes assignment to property with setter when the property is never read

Open dimanux opened this issue 8 years ago • 8 comments

This is a simple test:

class A {
    constructor() {
        this.value = "A";
    }

    set someValue(value) {
        this.value = value;
    }

    removeMe() {}
};

class B extends A {
    constructor() {
        super();
        this.someValue = "B";
    }
};

console.log(new B().value);

Output should be B, but with ADVANCED mode it's A. But if you'll remove removeMe() {} the output is B as expected.

dimanux avatar Jan 08 '18 00:01 dimanux

What's happening here is that RemoveUnusedClassProperties sees a write to this.someValue but it appears that this.someValue is never read. So, it believes it can safely remove the this.someValue = "B"; statement and the setter also.

More puzzling is why removing the removeMe() method fixes things. After the first few passes the code looks something like this:

/** @const @type {?} */ var $jscomp$global = function(maybeGlobal) {
  return typeof window != "undefined" && window === maybeGlobal ? maybeGlobal : typeof global != "undefined" && global != null ? global : maybeGlobal;
}(this);

/* OTHER COMPILER-GENERATED CODE OMITTED HERE */

/** @struct @constructor */ var A = function() {
  this.value = "A";
};
A.prototype.removeMe = function() {
};
$jscomp$global.Object.defineProperties(A.prototype, {someValue:{configurable:true, enumerable:true, /**
 @this {A}
 */
set:function(value$jscomp$84) {
  this.value = value$jscomp$84;
}}});
/**
 @struct
 @constructor
 @extends {A}
 */
var B = function() {
  A.call(this);
  this.someValue = "B";
};
$jscomp$inherits(B, A);
console.log((new B).value);

For some reason I haven't yet determined the InlineVariables pass decides it should inline $jscomp$global if removeMe() isn't there.

Then the Object.defineProperties() call looks like this:

(function(maybeGlobal) {
  return typeof window != "undefined" && window === maybeGlobal ? maybeGlobal : typeof global != "undefined" && global != null ? global : maybeGlobal;
})(this).Object.defineProperties(A.prototype, {someValue:{configurable:true, enumerable:true, /**
 @this {A}
 */
set:function(value$jscomp$84) {
  this.value = value$jscomp$84;
}}});

Now RemoveUnusedClassProperties no longer recognizes the call as a form of Object.defineProperties, so it just sees the someValue key within it as an indicator that it isn't safe to mess with the someValue property of anything, so it leaves both the setter and the assignment in place.

brad4d avatar Jan 09 '18 01:01 brad4d

Possible workarounds:

  1. Disable the RemoveUnusedClassProperties pass for your code.
  2. (Best) Always have getters and setters in pairs and don't have side-effects in setters that are observable in any way other than using the corresponding getter.

brad4d avatar Jan 09 '18 01:01 brad4d

Given that the primary reason you probably have a setter is to have side-effects, perhaps we should just avoid code removal entirely for a property name for which a setter exists.

brad4d avatar Jan 09 '18 18:01 brad4d

I've just finished implementing the features of RemoveUnusedClassProperties in RemoveUnusedCode and am about to drop RemoveUnusedClassProperties entirely. That will be done before addressing this bug.

Discussed this with @concavelenz and we've agreed the correct fix will be to decide a property is unremovable once we determine both that it has a setter and the setter is actually invoked.

brad4d avatar Jan 09 '18 19:01 brad4d

@brad4d Thanks for the information! I have a complex app and I use getters/setters with a decorator pattern. So there are many setters invoked without getters. I've noticed that some code was disappeared unexpectedly recently. I think that property should be unremovable once it has a a setter and it is invoked too.

dimanux avatar Jan 10 '18 06:01 dimanux

Upon further discussion, we've realized that a lot more work is needed to really safely support getters and setters in ADVANCED mode. The fix I had in mind would only have handled a portion of the problem.

Thumbnail sketch:

  1. Compiler will keep a lists of all property names known to have getters and setters.
  2. These lists will be used by NodeUtil methods that need to determine side effects and similar information. Basically, they will make reads and writes to properties with those names be considered to cause unknown side effects.
  3. RemoveUnusedCode will update these lists after it removes code.
  4. Passes that use the NodeUtil side-effects calculation methods but also intentionally ignore it in some cases (like RemoveUnusedCode does in order to remove assignments), will also need to be updated to take this list into account.
  5. We currently don't have a good way to represent getters and setters in externs. This feature needs to be added.
  6. (Optional) It may be helpful to have a pass that intentionally converts getters and setters into "normal" method calls when possible.

For now it just isn't safe to use ADVANCED optimizations if your code uses getters and setters. Even things that seem pretty safe, aren't really.

brad4d avatar Feb 03 '18 00:02 brad4d

FYI. If the list at step 1 is created before disambiguation, it needs to be updated with the new names after disambiguation.

dimvar avatar Feb 05 '18 19:02 dimvar

Steps 1 through 4 of the plan described above got done. re: 5 We can actually represent getters and setters in externs using Object.defineProperty

brad4d avatar Feb 14 '20 22:02 brad4d