dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.3k stars 1.59k forks source link

Extend kernel constant evaluator to handle constant expressions in non-constant contexts #34585

Open alexmarkov opened 6 years ago

alexmarkov commented 6 years ago

Consider the following example (extracted from _SecureFilterImpl.SIZE):

final x = 8 * 1024;

void main() {
  print(x);
}

When this example is compiled in AOT mode, there is still multiplication in kernel AST:

 [@vm.inferred-type.metadata=int?]  static final field core::int x = [@vm.direct-call.metadata=dart.core::_IntegerImplementation::*] [@vm.inferred-type.metadata=int?] 8.{core::num::*}(1024);

Constant evaluator can evaluate such expressions even though they are used in non-constant contexts. After constant evaluation, TFA could infer a concrete type (Smi) of a resulting constant.

/cc @mkustermann @mraleph

mkustermann commented 5 years ago

@sstrickl Since you have a very specific corner case with exactly the same problem: If you are interested you could look at this issue?