Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dart2js: type inference could strengthen types of arguments #17084

Open
rakudrama opened this issue Feb 24, 2014 · 2 comments
Open

dart2js: type inference could strengthen types of arguments #17084

rakudrama opened this issue Feb 24, 2014 · 2 comments
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

Comments

@rakudrama
Copy link
Member

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).

@floitschG
Copy link
Contributor

Added Optimization label.

@floitschG
Copy link
Contributor

cc @herhut-ggl.

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
@vsmenon vsmenon added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Jul 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

4 participants