Skip to content

CFE does not support instantiation of function .call tearoff #45857

Closed
@stereotype441

Description

@stereotype441

The following code is accepted by the analyzer but rejected by the CFE:

main() {
  S Function(S) specialize<S>(T Function<T>(T) f) => f.call;
  X id<X>(X value) => value;
  int Function(int) intId = specialize(id);
  X defaulter<X>(X? value, [X? defaultValue]) => value ?? defaultValue ?? (throw ArgumentError("no default"));
  intId = specialize(defaulter);
  print(intId.runtimeType);
}

The CFE says:

../../tmp/proj/test.dart:3:57: Error: A value of type 'T Function<T>(T)' can't be returned from a function with return type 'int Function(int)'.
  int Function(int) specialize(T Function<T>(T) f) => f.call;

After experimenting a bit, I think that the CFE only allows generic instantiation of .call if .call resolves to a method on an actual class; if it resolves to the implicit .call of a function type, that's not supported.

The language team is currently having a discussion about related topics at dart-lang/language#1604, and it's not yet clear to me what the best fix is. So for now I'm assigning this issue to area-language. We can re-assign it to area-front-end or area-analyzer once we decide what we want the correct behavior to be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions