Skip to content

Analyzer: Incorrect function literal inference when in a legacy context  #42278

@leafpetersen

Description

@leafpetersen

Consider the following program:

out_of_order.dart

// @dart = 2.7

void foo(int Function(int) x) {}

in_order.dart

import 'out_of_order.dart';

void test() {
  int? s = null;
  foo((x) => s);
}

I believe that as specified, we should be inferring the argument to foo in a context where the return type is int* (that is, legacy int). The inferred return type of the body is int?, which is a subtype of int*, and so by my read we should be choosing int? as the inferred return type for the function literal. As a result, the function literal should have inferred type int? Function(int) which is assignable to int* Function(int*), and the body of the function should have no errors, since the return value and the return type are compatible.

The analyzer currently seems to be inferring the non-nullable type int as the return type for the function literal:

leafp-macbookpro:sdk leafp$ ~/src/dart-repo/sdk/xcodebuild/ReleaseX64/dart-sdk/bin/dartanalyzer --enable-experiment=non-nullable ~/tmp/opted_out.dart
Analyzing /Users/leafp/tmp/opted_out.dart...
  error • The return type 'int?' isn't a 'int', as required by the closure's context. • /Users/leafp/tmp/opted_in.dart:5:14 • return_of_invalid_type_from_closure

cc @yjbanov @scheglov

Metadata

Metadata

Assignees

Labels

NNBDIssues related to NNBD Releaselegacy-area-analyzerUse area-devexp instead.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions