Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(413)

Unified Diff: pkg/analyzer/lib/src/generated/static_type_analyzer.dart

Issue 1717803003: partial fix for #25220, handles inference for list/map literals (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/src/context/mock_sdk.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/generated/static_type_analyzer.dart
diff --git a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
index 4a2362f7558f8eee6c78c44139bf845d6d7ce445..c067bcbd9434790434fc783d3e2f0bea07a9532b 100644
--- a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
+++ b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
@@ -626,14 +626,18 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
staticType = argumentType;
}
}
- } else {
+ } else if (_strongMode) {
DartType contextType = InferenceContext.getType(node);
- if (_strongMode &&
- contextType is InterfaceType &&
+ if (contextType is InterfaceType &&
contextType.typeArguments.length == 1 &&
contextType.element == _typeProvider.listType.element) {
staticType = contextType.typeArguments[0];
_resolver.inferenceContext.recordInference(node, contextType);
+ } else if (node.elements.isNotEmpty) {
+ // Infer the list type from the arguments.
+ staticType =
+ node.elements.map((e) => e.staticType).reduce(_leastUpperBound);
+ // TODO(jmesserly): record inference here?
}
}
_recordStaticType(
@@ -672,15 +676,22 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
staticValueType = entryValueType;
}
}
- } else {
+ } else if (_strongMode) {
DartType contextType = InferenceContext.getType(node);
- if (_strongMode &&
- contextType is InterfaceType &&
+ if (contextType is InterfaceType &&
contextType.typeArguments.length == 2 &&
contextType.element == _typeProvider.mapType.element) {
staticKeyType = contextType.typeArguments[0] ?? staticKeyType;
staticValueType = contextType.typeArguments[1] ?? staticValueType;
_resolver.inferenceContext.recordInference(node, contextType);
+ } else if (node.entries.isNotEmpty) {
+ // Infer the list type from the arguments.
+ staticKeyType =
+ node.entries.map((e) => e.key.staticType).reduce(_leastUpperBound);
+ staticValueType = node.entries
+ .map((e) => e.value.staticType)
+ .reduce(_leastUpperBound);
+ // TODO(jmesserly): record inference here?
}
}
_recordStaticType(
@@ -1535,8 +1546,8 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
}
}
- // TODO(vsm): Use leafp's matchType here?
DartType _findIteratedType(DartType type, DartType targetType) {
+ // TODO(vsm): Use leafp's matchType here?
// Set by _find if match is found
DartType result;
// Elements we've already visited on a given inheritance path.
@@ -2056,6 +2067,14 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
}
/**
+ * Computes the least upper bound between two types.
+ *
+ * See [TypeSystem.getLeastUpperBound].
+ */
+ DartType _leastUpperBound(DartType s, DartType t) =>
+ _typeSystem.getLeastUpperBound(_typeProvider, s, t);
+
+ /**
* Record that the propagated type of the given node is the given type.
*
* @param expression the node whose type is to be recorded
« no previous file with comments | « no previous file | pkg/analyzer/test/src/context/mock_sdk.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698