Skip to content

Analyzer fails to reject a constructor tearoff with type parameters when used as a const value #59762

@Quijx

Description

@Quijx

When using a constructor tearoff such as Foo.new or Foo.bar of a class with generic types in a const expression, the analyzer fails to produce an error message. The code is correctly rejected by the CFE, for example by using dart run.

Example:

class Foo<T> {}

Foo<T> defaultCreateFoo<T>() => Foo<T>();

void bar<T>({
  // No analyzer error.
  Foo<T> Function() createFoo = Foo.new,
}) {
  // No analyzer error.
  const Foo<T> Function() createFooVar1 = Foo.new;
  // Correct analyzer error.
  const Foo<T> Function() createFooVar2 = defaultCreateFoo;
  //                                      ^^^^^^^^^^^^^^^^
  // A constant function tearoff can't use a type parameter as a type argument.
}

void main() {
  bar();
}

I tested this with Dart SDK 3.7.0-243.0.dev on https://dartpad.dev/?channel=main.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions