Skip to content

front-end issues an error when returning supertype A from async function returning Future<B>, where B is subclass of A #32185

@mraleph

Description

@mraleph
import 'dart:async';

class A { }
class B extends A { }

A f() => null;

Future<B> g() async => f();

void main() {

}

results in

$ pkg/vm/tool/dart2 /tmp/yyy.dart                                     254 ↵
file:///tmp/yyy.dart:8:24: Error: A value of type '#lib1::A' can't be assigned to a variable of type 'dart.async::FutureOr<#lib1::B>'.
Try changing the type of the left hand side, or casting the right hand side to 'dart.async::FutureOr<#lib1::B>'.
Future<B> g() async => f();
                       ^
file:///tmp/yyy.dart:8:24: Error: A value of type '#lib1::A' can't be assigned to a variable of type 'dart.async::FutureOr<#lib1::B>'.
Try changing the type of the left hand side, or casting the right hand side to 'dart.async::FutureOr<#lib1::B>'.
Future<B> g() async => f();
   

Analyzer does not report an error here unless I run it with --no-implicit-casts.

@leafpetersen @eernstg @lrhn can you confirm which behavior is correct?

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions