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

Functions annotated with @nosideeffects are not removed when it is aliased

Open theseanl opened this issue 4 years ago • 8 comments

In the below example, Object.getOwnPropertyDescriptor is annotated with @nosideeffects in es5.js, but its calls are not removed.

closure-compiler-debugger

let a = Object.getOwnPropertyDescriptor;
let b = Object.getOwnPropertyDescriptor(Object.prototype, 'hasOwnProperty');
let c = a(Object.prototype,'__proto__').get;
'use strict'; var a = Object.getOwnPropertyDescriptor; Object.getOwnPropertyDescriptor(Object.prototype, "hasOwnProperty"); a(Object.prototype, "__proto__");

Originally from https://groups.google.com/g/closure-compiler-discuss/c/1ibhiOqVzHo/m/Y5TIDPbpAQAJ.

theseanl avatar Mar 07 '21 05:03 theseanl

@nreid260 Hi, I think this issue is not fixed in the version published in NPM.

It seems to be working in the closure-compiler-debugger, but can still be reproduced by using the latest NPM version (20210808.0.0) Here are the precise files and compiler flags that I'm using. I'd appreciate if this issue is revisited. a.js

let a = Object.getOwnPropertyDescriptor;
let b = Object.getOwnPropertyDescriptor(Object.prototype, 'hasOwnProperty');
let c = a(Object.prototype,'__proto__').get;

closure.conf

--compilation_level=ADVANCED
--js a.js
--js_output_file=out.js
--language_in=ECMASCRIPT_2020
--language_out=ECMASCRIPT_2020
--emit_use_strict=false
--assume_function_wrapper=true

command: google-closure-compiler --flagfile=closure.conf

output:

let a=Object.getOwnPropertyDescriptor;Object.getOwnPropertyDescriptor(Object.prototype,"hasOwnProperty");a(Object.prototype,"__proto__");

The expected output is an empty file, but as you can see, Object.getOwnPropertyDescriptor calls are not removed.

theseanl avatar Sep 01 '21 18:09 theseanl

There was a delay in the NPM release that we didn't realize. This might be fixed now.

nreid260 avatar Sep 01 '21 18:09 nreid260

On my end, the issue persists on the current nightly version 20210901.0.0-nightly which was published only 6 hours ago, so I think something that might be fixed now cannot be a cause.

theseanl avatar Sep 01 '21 18:09 theseanl

What externs are you using? Object.getOwnPropertyDescriptor needs to be defined in externs and annotated with @nosideeffects. The "canonical" externs do this, but I can't tell if those are part of your example.

nreid260 avatar Sep 02 '21 16:09 nreid260

Whatever externs that the NPM version feed by default. My understanding is that this includes "canonical" externs such as es3.js for example.

theseanl avatar Sep 04 '21 04:09 theseanl

@nreid260 The issue persists on 20210906.0.0-nightly.

theseanl avatar Sep 06 '21 22:09 theseanl

Friendly ping, this issue is still reproducible on [email protected].

theseanl avatar Mar 12 '22 16:03 theseanl

It appears that the issue now coming from a somewhere else, which reminds me of https://github.com/google/closure-compiler/issues/2132. The inclusion of the externs es6_proxy.js seems to be the culprit. It contains a @record which has a property getOwnPropertyDescriptor. Mere existence of it prevents closure compiler from removing a code that has no side effect. JSCompiler Debugger repro

theseanl avatar Mar 12 '22 17:03 theseanl