Skip to content

[dart2js] Unused fields are not tree-shaken off, sensitive to type annotations #11558

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
yjbanov opened this issue Jun 27, 2013 · 5 comments
Closed
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js

Comments

@yjbanov
Copy link

yjbanov commented Jun 27, 2013

INPUT:

main() {
  print(new Foo().hello);
}

class Bar {
  String txt = " World";
}

class Foo {
  String _hello;
  Bar _world; // never initialized
  String get hello {
    if (_hello == null) {
      _hello = "Hello";
    }
    return _hello;
  }
  String get world {
    if (_world == null) {
      _world = new Bar();
    }
    return _world.txt;
  }
}

OUTPUT (reformatted):

main(){
  print(new Foo().hello);
}

class Bar{} // Expected Bar to be gone.
             // However, it will be gone if I change type of _world to "var".
             // Nice to see txt tree-shaken off.

class Foo{
  String _hello; // Used, so it stays.
  Bar _world; // Not used, expected to be gone
  String get hello{
    if(_hello==null) {
      _hello="Hello";
    }
    return _hello;
  }
}

@anders-sandholm
Copy link
Contributor

Added Area-Dart2Dart, Triaged labels.

@sethladd sethladd added the closed-not-planned Closed as we don't intend to take action on the reported issue label Jul 10, 2015
@yjbanov
Copy link
Author

yjbanov commented Jul 10, 2015

This issue was misclassified. Reopening and moving to dart2js.

@yjbanov yjbanov reopened this Jul 10, 2015
@yjbanov yjbanov added web-dart2js Priority-Medium and removed Area-Dart2Dart closed-not-planned Closed as we don't intend to take action on the reported issue labels Jul 10, 2015
@yjbanov yjbanov changed the title [dart2dart] Unused fields are not tree-shaken off, sensitive to type annotations [dart2js] Unused fields are not tree-shaken off, sensitive to type annotations Jul 10, 2015
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed triaged labels Feb 29, 2016
@greglittlefield-wf
Copy link
Contributor

greglittlefield-wf commented Feb 15, 2018

I just ran into the issue, only I was seeing it where unused fields from the superclass overridden by getters/setters in a subclass are not completely tree-shaken, and are still present in the JS constructor initializer list.

For example,

class A {
  int a;
}
class B extends A {
  int get a => ...;
  set a(int value) => ...;
}

For more context on my use-case, see the "Leaving behind fields" section here: dart-lang/build#897 (comment)

I was thinking #21450 may mitigate this issue, but it would be nice to solve the issue and eliminate these fields completely.


Looks like the above example still reproduces on Dart 1.24.3, with some tweaks to prevent the Foo class from being fully optimized out:

main() {
  var foo = new Foo();
  print('$foo ${foo.hello}');
}

class Bar {
  String txt = " World";
}

class Foo {
  String _hello;
  Bar _world; // never initialized
  String get hello {
    if (_hello == null) {
      _hello = "Hello";
    }
    return _hello;
  }
  String get world {
    if (_world == null) {
      _world = new Bar();
    }
    return _world.txt;
  }
}

dart2js output

["", "reduced.dart",, T, {
  "^": "",
    main: function() {
    H.printString(H.Primitives_objectToHumanReadableString(new T.Foo(null, null)) + " hello");
  },
  Foo: {
    "^": "Object;_hello,_world"
   }
}, 1]

dart2js constructor retrieved by stepping into it at runtime

function Foo(p__hello, p__world) {
this._hello = p__hello;
this._world = p__world;
}
Foo.builtin$cls="Foo";
$desc=$collectedClasses.Foo[1];
Foo.prototype = $desc;

@yjbanov
Copy link
Author

yjbanov commented Feb 15, 2018

/cc @matanlurey who has been looking into tree-shaking issues not long ago

@vsmenon vsmenon added the area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. label Jul 20, 2019
@Markzipan
Copy link
Contributor

These examples no longe reproduce in Dart2JS. Closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web-js Issues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js
Projects
None yet
Development

No branches or pull requests

7 participants