closure-compiler
closure-compiler copied to clipboard
Inlining values from an enum-getter
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?
Can you share which flags your using to compile with? Thanks!
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.