Skip to content

Improve diagnostic situation for constructor tearoffs from deferred library #54611

@parlough

Description

@parlough

Consider:

other.dart:

class C {}

main.dart:

import 'other.dart' deferred as other;

void main() {
  test(other.C.new);
}

void test(Object Function() f) {
  print(f);
}

This provides no analyzer diagnostics (which is a known issue that applies to more situations), but the front end does error:

main.dart:4:16:
Error: 'new' can only be used as a constructor reference.
  test(other.C.new);
               ^^^
Error: Compilation failed.

Most deferred errors will happen at runtime if you haven't imported the library with an explanation as to why, but this is happening at compile time, I'm assuming because the tear off is constant?

It's nice for it to happen at compile time, but the error isn't helpful for developers trying to resolve it since .new is a constructor reference.

Side note: I discovered this issue since unnecessary_lambdas made this change automatically (fix here).

Metadata

Metadata

Assignees

No one assigned

    Labels

    legacy-area-front-endLegacy: Use area-dart-model instead.model-messagesPoor/undesirable messaging in errors/warnings emitted by the analyzer and/or CFE.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions