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

Inferred variable/field types should not be used as context when doing full resolution #51137

Closed
stereotype441 opened this issue Jan 26, 2023 · 4 comments
Assignees
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@stereotype441
Copy link
Member

Consider the following code:

// @dart=2.17
T? inferFromArgs<T>(
        T Function(List<int>) fromBytes, List<int> Function(T) toBytes) =>
    null;
final x = inferFromArgs(String.fromCharCodes, (m) => m.codeUnits); // (1)
void main() {}

The // @dart=2.17 annotation disables horizontal inference. So, when analyzing (1), the analyzer should push down a context of List<int> Function(?) when analyzing the closure (m) => m.codeUnits. Therefore, the inferred type of the parameter m should be the greatest closure of ?, which is Object?. As a result, there should be a compile-time error, because the type Object? doesn't support the getter .codeUnits. The CFE produces the proper error message.

Here's what happens when the analyzer analyzes this code: since (1) is a top level variable declaration with no explicit type, the analyzer first performs top level type inference, which involves resolving the right hand side of the declaration. The type Object? is correctly assigned to m, however no error is reported because the analyzer doesn't report errors during top level type inference. The inferred type of the variable x is String?.

Then, after top level type inference is complete, the analyzer visits the entire file and resolves it, reporting errors. Which means that it visits the declaration of x a second time. On the second visit, the inferred type of String? is pushed down as a context. As a result, even though horizontal inference is disabled, the type String is inferred for m, and so no error is reported.

I believe the easiest way to fix this would be to change the analyzer so that when resolving a top level variable or field declaration, it only passes the context type down to the initializer if the type is explicit.

(Note that since the CFE already reports the correct error here, it should not be necessary to go through the breaking change process for this change).

@natebosch
Copy link
Member

Does this affect newer language versions?

@stereotype441
Copy link
Member Author

Does this affect newer language versions?

Yes, it applies to all language versions.

@vsmenon vsmenon added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Jan 27, 2023
@keertip
Copy link
Contributor

keertip commented Feb 1, 2023

/cc @scheglov

@keertip keertip added the P1 A high priority bug; for example, a single project is unusable or has many test failures label Feb 1, 2023
@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Feb 7, 2023
@scheglov
Copy link
Contributor

scheglov commented Feb 7, 2023

copybara-service bot pushed a commit that referenced this issue Feb 8, 2023
…lve the initializer.

Bug: #51137
Change-Id: I5979e2531b285d86141d60cd844919456ff17c47
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281462
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
@scheglov scheglov closed this as completed Feb 8, 2023
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. P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

6 participants