Skip to content

Tree-shaking unused superclass method fails with factory constructor #30963

@nex3

Description

@nex3

In the following code:

class Super {
  Super();

  factory Super.sub() = Sub;

  void foo() {
    throw "super foo";
  }

  void bar() {
    foo();
    // Throw to avoid inlining this method.
    throw "";
  }
}

class Sub extends Super {
  void foo() {
    throw "sub foo";
  }
}

main() async {
  new Super.sub().bar();
}

Super.foo() is unreachable, but dart2js (as of 0c005a3) fails to tree-shake it:

$ dart2js test.dart
$ grep -n "super error" out.js
4936:        throw H.wrapException("super foo");

This seems to only happen if Sub is constructed via a factory constructor on Super (redirecting or otherwise), and foo() is called from another method in Super (in this case, bar()).

This came up in practice when trying to reduce the code size of the http package and its dependencies.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)web-dart2js

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions