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

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

Issue 1226823003: Improve propagation in binary expressions (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 5 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
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 20f3957c7675902823ab79f8d93ae59d87f4f8d0..36ff1fe315cc3b77e8f9976bd2c87c35b84fe4a2 100644
--- a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
+++ b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
@@ -253,12 +253,14 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
}
ExecutableElement staticMethodElement = node.staticElement;
DartType staticType = _computeStaticReturnType(staticMethodElement);
- staticType = _refineBinaryExpressionType(node, staticType);
+ staticType = _refineBinaryExpressionType(node, staticType, _getStaticType);
_recordStaticType(node, staticType);
MethodElement propagatedMethodElement = node.propagatedElement;
if (!identical(propagatedMethodElement, staticMethodElement)) {
DartType propagatedType =
_computeStaticReturnType(propagatedMethodElement);
+ propagatedType =
+ _refineBinaryExpressionType(node, propagatedType, _getBestType);
_resolver.recordPropagatedTypeIfBetter(node, propagatedType);
}
return null;
@@ -1381,6 +1383,17 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
}
/**
+ * Return the best type of the given [expression].
+ */
+ DartType _getBestType(Expression expression) {
+ DartType type = expression.bestType;
+ if (type == null) {
+ return _dynamicType;
scheglov 2015/07/07 22:14:58 AFAIK Expression.bestType already returns DynamicT
+ }
+ return type;
+ }
+
+ /**
* If the given element name can be mapped to the name of a class defined within the given
* library, return the type specified by the argument.
*
@@ -1507,10 +1520,7 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
}
/**
- * Return the static type of the given expression.
- *
- * @param expression the expression whose type is to be returned
- * @return the static type of the given expression
+ * Return the static type of the given [expression].
*/
DartType _getStaticType(Expression expression) {
DartType type = expression.staticType;
@@ -1697,15 +1707,15 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
}
/**
- * Attempts to make a better guess for the static type of the given binary expression.
- *
- * @param node the binary expression to analyze
- * @param staticType the static type of the expression as resolved
- * @return the better type guess, or the same static type as given
+ * Attempts to make a better guess for the type of the given binary
+ * [expression], given that resolution has so far produced the [currentType].
+ * The [typeAccessor] is used to access the corresponding type of the left
+ * and right operands.
*/
DartType _refineBinaryExpressionType(
- BinaryExpression node, DartType staticType) {
- sc.TokenType operator = node.operator.type;
+ BinaryExpression expression, DartType currentType,
+ [DartType typeAccessor(Expression node)]) {
+ sc.TokenType operator = expression.operator.type;
// bool
if (operator == sc.TokenType.AMPERSAND_AMPERSAND ||
operator == sc.TokenType.BAR_BAR ||
@@ -1714,14 +1724,14 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
return _typeProvider.boolType;
}
DartType intType = _typeProvider.intType;
- if (_getStaticType(node.leftOperand) == intType) {
+ if (typeAccessor(expression.leftOperand) == intType) {
// int op double
if (operator == sc.TokenType.MINUS ||
operator == sc.TokenType.PERCENT ||
operator == sc.TokenType.PLUS ||
operator == sc.TokenType.STAR) {
DartType doubleType = _typeProvider.doubleType;
- if (_getStaticType(node.rightOperand) == doubleType) {
+ if (typeAccessor(expression.rightOperand) == doubleType) {
return doubleType;
}
}
@@ -1731,13 +1741,13 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
operator == sc.TokenType.PLUS ||
operator == sc.TokenType.STAR ||
operator == sc.TokenType.TILDE_SLASH) {
- if (_getStaticType(node.rightOperand) == intType) {
- staticType = intType;
+ if (typeAccessor(expression.rightOperand) == intType) {
+ return intType;
}
}
}
// default
- return staticType;
+ return currentType;
}
/**
« no previous file with comments | « pkg/analysis_server/test/services/refactoring/extract_method_test.dart ('k') | pkg/analyzer/test/generated/resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698