Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyzer fails to use proper context type for conditions of conditional expressions #48199

Closed
stereotype441 opened this issue Jan 23, 2022 · 0 comments
Assignees
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.

Comments

@stereotype441
Copy link
Member

Consider the following code:

T castObject<T>(Object value) => value as T;

main() {
  print((castObject(true)..whatever()) ? 1 : 2);
}

The CFE analyzes the subexpression castObject(true)..whatever() using a context type of bool, so it interprets it as castObject<bool>(true)..whatever(). Since castObject<bool>(true) has type bool, the method lookup of whatever fails, and the CFE issues the compile-time error:

Error: The method 'whatever' isn't defined for the class 'bool'.
Try correcting the name to the name of an existing method, or defining a method named 'whatever'.
  print((castObject(true)..whatever()) ? 1 : 2);
                           ^^^^^^^^

The analyzer, on the other hand, analyzes the subexpression castObject(true)..whatever() with a null context type, so it interprets it as castObject<dynamic>(true)..whatever(). Since castObject<dynamic>(true) has type dynamic, the method lookup of whatever is not type checked, so the analyzer issues no error.

I believe the CFE's behavior is correct, and this is an oversight in the analyzer implementation.

@stereotype441 stereotype441 added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Jan 23, 2022
@stereotype441 stereotype441 self-assigned this Jan 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Projects
None yet
Development

No branches or pull requests

1 participant