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

Inlining values from an enum-getter

Open samouri opened this issue 5 years ago • 2 comments

summary I'm currently working on a PR for AMP in which we'd like to use a @const to assist with inlining a variable. An issue has come up where we are required to use an enum-getter as opposed to directly using the enum, but this seems to deopt.

For example, this code inlines as expected:

/** @enum {string} */
const a = { cx1: 'hello', cx2: 'world'};
console.log(a.cx1, a.cx2); // compiles down to: console.log('hello', 'world');

Whereas when a getter is introduced, it stops inlining:

/** @enum {string} */
const a = { cx1: 'hello', cx2: 'world'};

/** @const @function():  typeof a */
const getA = () => a;

console.log(getA().cx1, getA().cx2); // compiles down to: console.log(a.cx1)

Is this an intentional deopt? Is there a workaround?

samouri avatar Sep 22 '20 17:09 samouri

Can you share which flags your using to compile with? Thanks!

rrdelaney avatar Sep 23 '20 18:09 rrdelaney

This is just a limitation. "collapse properties" which is responsible for collapse global objects only runs once relatively early. When it runs, the "getA" hasn't been inlined and object can't be collapsed because "getA" 'escapes' the object. Once it was inlined it would be possible but it is too late. If it were contained within function "collapse object literals", which is responsible for local values, might do it depending on the conditions.

concavelenz avatar Sep 23 '20 19:09 concavelenz