dart2js: type inference could strengthen types of arguments #17084
Labels
area-web
Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop.
dart2js-optimization
P2
A bug or feature request we're likely to work on
type-enhancement
A request for a change that isn't a bug
web-dart2js
We already strengthen the type of the receiver at a call:
a.foo();
After the call, a can be restricted to a those types which respond to 'foo'.
We can do the same for arguments.
Consider:
import 'dart:math';
get never => new Random().nextDouble() > 2;
main() {
var a = 1;
var b = 2;
if (never) a = 'a'; // inferred type {int, String}
if (never) b = 'b';
var m = min(a, b); // min throws if either argument is not a number, returned value, m is number.
var s = m + a;
print('m = $m s = $s');
}
The generated code:
main: function() {
var a, m, line;
a = C.C__JSRandom.nextDouble$0() > 2 ? "a" : 1;
m = P.min(a, C.C__JSRandom.nextDouble$0() > 2 ? "b" : 2);
if (typeof a !== "number") // argument check
return H.iae(a);
line = "m = " + m + " s = " + (m + a);
H.printString(line);
}
We could avoid the argument check if the model for 'min' included the strengthened argument types.
This might be an expensive suggestion since the analysis would need fresh constraint variables for argument values after each call (but only those that are used later).
The text was updated successfully, but these errors were encountered: