Skip to content

dart2js: Imprecise type inference and type propagation on || condition #26835

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

Closed
rakudrama opened this issue Jul 7, 2016 · 2 comments
Closed
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. dart2js-optimization web-dart2js

Comments

@rakudrama
Copy link
Member

The code below, compiled with

sdk/bin/dart2js --package-root=out/ReleaseX64/packages ~/play1/bug6j1a.dart \
 --trust-type-annotations --dump-info

contains an interceptor call for externalUrl.startsWith('#'), i.e.

J.startsWith$1$s(externalUrl, "#")

The dumpinfo output shows AA.x is [null|exact=JSString], so global type inference does not determine externalUrl cannot be null.
Further, SSA type propagation does not clean up the type.
So both 'passes' fail to be precise enough.
Changing the first 'if' to two 'if' statements generates the expected code for the return expression:

  if (externalUrl == null) return '';
  if (externalUrl.isEmpty) return '';

out.js:

encodeSecureHref: [function(externalUrl) {
  if (externalUrl == null || externalUrl.length === 0)
    return "";
  T.AA(externalUrl);
  return J.startsWith$1$s(externalUrl, "#") ||
         C.JSString_methods.startsWith$1(externalUrl.toLowerCase(), "mailto:")
      ? externalUrl
      : "http://app/" +
          P._Uri__uriEncode(C.List_nxB, externalUrl, C.Utf8Codec_false, true);
}, "call$1", "bug6j1a__encodeSecureHref$closure", 2, 0, 0]

bug6j1a.dart:

import 'package:expect/expect.dart';

const String _SECURE_LINK_PREFIX = 'http://app/';

String encodeSecureHref(String externalUrl) {
  if (externalUrl == null || externalUrl.isEmpty) {
    return '';
  }
  AA(externalUrl);
  return (externalUrl.startsWith('#') ||
      externalUrl.toLowerCase().startsWith('mailto:')) ?
          externalUrl :
          _SECURE_LINK_PREFIX + Uri.encodeQueryComponent(externalUrl);
}

@NoInline()
AA(x) => x;

var f = (x) => x * 2;

main() {
  print(encodeSecureHref('#foo'));
  print(encodeSecureHref('foo'));
  print(encodeSecureHref(null));
  print(encodeSecureHref(''));
  print(f(100));
  f = encodeSecureHref;
  print(encodeSecureHref);
}
@rakudrama
Copy link
Member Author

#28330 is one way of solving this for types_propagation.
Something similar should be incorporated into global type analysis, which should be easier since the graph has no structural constraints (i.e. need not be reducible).

@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
copybara-service bot pushed a commit that referenced this issue Mar 24, 2025
Bug: #26835 #28330
Change-Id: Ic3bc05f1b5525e58173c335c5f3f4f91dd09e2f3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417329
Reviewed-by: Mayank Patke <fishythefish@google.com>
Commit-Queue: Stephen Adams <sra@google.com>
copybara-service bot pushed a commit that referenced this issue Mar 26, 2025
Bug: #26835 #28330
Change-Id: I2ea5d3022901a67b10fdb7ca0a6233a22ca233bf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417328
Commit-Queue: Stephen Adams <sra@google.com>
Reviewed-by: Mayank Patke <fishythefish@google.com>
@rakudrama
Copy link
Member Author

#9505e95 pushes refinements past the join point in cases like the above example

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 web-dart2js
Projects
None yet
Development

No branches or pull requests

2 participants