Not removing calls with @nosideeffect functions

39 views
Skip to first unread message

sean.l

unread,
Mar 6, 2021, 12:13:13 PM3/6/21
to Closure Compiler Discuss
When I compile codes which contains some `Object.defineProperty` calls whose return value are not used, closure compiler does not remove the call entirely, although `Object.defineProperty` is annotated with `@nosideeffect` in `es3.js`. 

Here is a repro:

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

output:
```
'use strict'; var a = Object.getOwnPropertyDescriptor; Object.getOwnPropertyDescriptor(Object.prototype, "hasOwnProperty"); a(Object.prototype, "__proto__");
```


Is this an expected result? Is there something that I can tweak to let Closure Compiler to remove those calls?

sean.l

unread,
Mar 6, 2021, 12:17:26 PM3/6/21
to Closure Compiler Discuss
Correction: `Object.getOwnPropertyDescriptor` is in `es5.js`, not `es3.js`

Nick Reid

unread,
Mar 7, 2021, 12:01:13 AM3/7/21
to Closure Compiler Discuss
Hey Sean, thanks for the report.

This looks like a bug caused by PureFunctionIdentifier being too cautious with respect to function aliases. If you remove the `let a =` alias everything works as expected. Hopefully that's an acceptable workaround for now.

Can you file a GitHub issue for this? It should be a simple fix.

Nick

sean.l

unread,
Mar 7, 2021, 12:27:00 AM3/7/21
to Closure Compiler Discuss
Done here, thanks!

sean.l

unread,
Jul 23, 2021, 1:11:55 AM7/23/21
to Closure Compiler Discuss
Hi, the issue seems to be fixed in the Closure Compiler debugger, but it's not fixed with npm google-closure-compiler 20210601 version.

When I fed the same file
```
// a.js
let a = Object.getOwnPropertyDescriptor;
let b = Object.getOwnPropertyDescriptor(Object.prototype, 'hasOwnProperty');
let c = a(Object.prototype,'__proto__').get;
```
and CLI flags
```
--compilation_level=ADVANCED
--js a.js
--js_output_file=out.js
--language_in=ECMASCRIPT_2017
--language_out=ECMASCRIPT_2017
```
the output still retains `Object.getOwnPropertyDescriptor` calls. Is this an intended behavior, or the npm version hasn't kept up with the fix in March yet?
Reply all
Reply to author
Forward
0 new messages