Skip to content

Analyzer does not properly handle flow analysis of null-aware extension method invocations #59654

@stereotype441

Description

@stereotype441

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

extension E on Null {
  int f(dynamic d) => 0;
}

int test(int? i) {
  i = 0; // promotes `i` to `int`
  E(null)?.f(i = null); // doesn't demote because `i = null` is unreachable
  return i; // ok because `i` is promoted to `int`
}

main() {}

The analyzer's error message is:

error • test.dart:8:10 • A value of type 'int?' can't be returned from the function 'test' because it has a return type of 'int'. • return_of_invalid_type

I believe what's happening is that the CFE is (correctly) passing the type of the target of the extension override (Null in this case) to FlowAnalysis.nullAwareAccess_rightBegin. Therefore, flow analysis concludes that i = null is unreachable, so i is not demoted. Whereas the analyzer is passing the type dynamic to FlowAnalysis.nullAwareAccess_rightBegin, so flow analysis doesn't know that i = null is unreachable, and so it demotes i to int?, and the return statement is in error.

I'll work on a fix.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions