Skip to content

dart vm can't upcasting generic type of function getter #55427

Closed
@chunhtai

Description

@chunhtai

I am not sure if this is expected or not.

The following code will crash the dart vm

void main() {
  A<Object?> a = B<Data>();
  a.done(Data());
}

typedef Fn<T> = void Function(T a);

abstract class A<T> {

  Fn<T> get done;
  // This is ok
  // void done(T a);
}

class B<T> extends A<T> {
  B();

  @override
  Fn<T> get done => (T a) => print(a);
  // This is ok
  // @override
  // void done(T a) =>  print(a);
}
class Data {

}

If you run this code, it crashes with

Unhandled exception:
type '(Data) => void' is not a subtype of type '(Object?) => void'
#0      main (package:flutter/src/widgets/test.dart:4:5)
dart-lang/sdk#57147      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
dart-lang/sdk#57148      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

If I convert the function getter to a instance method, it will work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions