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

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

Issue 1873913002: Refine compound assignment type. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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/generated/static_type_analyzer_test.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 12c9aac90526d7bcebf62e9e4836f66603e720c4..f76cf73f3abadec5d36778d6537953ee285b7ad2 100644
--- a/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
+++ b/pkg/analyzer/lib/src/generated/static_type_analyzer.dart
@@ -257,6 +257,8 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
} else {
ExecutableElement staticMethodElement = node.staticElement;
DartType staticType = _computeStaticReturnType(staticMethodElement);
+ staticType =
+ _refineAssignmentExpressionType(node, staticType, _getStaticType);
_recordStaticType(node, staticType);
MethodElement propagatedMethodElement = node.propagatedElement;
if (!identical(propagatedMethodElement, staticMethodElement)) {
@@ -2095,6 +2097,44 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
}
/**
+ * Attempts to make a better guess for the type of the given assignment
+ * [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 _refineAssignmentExpressionType(AssignmentExpression expression,
+ DartType currentType, DartType typeAccessor(Expression node)) {
+ Expression leftHandSize = expression.leftHandSide;
+ Expression rightHandSide = expression.rightHandSide;
+ TokenType operator = expression.operator.type;
+ DartType intType = _typeProvider.intType;
+ if (typeAccessor(leftHandSize) == intType) {
+ // int op= double
+ if (operator == TokenType.MINUS_EQ ||
+ operator == TokenType.PERCENT_EQ ||
+ operator == TokenType.PLUS_EQ ||
+ operator == TokenType.STAR_EQ) {
+ DartType doubleType = _typeProvider.doubleType;
+ if (typeAccessor(rightHandSide) == doubleType) {
+ return doubleType;
+ }
+ }
+ // int op= int
+ if (operator == TokenType.MINUS_EQ ||
+ operator == TokenType.PERCENT_EQ ||
+ operator == TokenType.PLUS_EQ ||
+ operator == TokenType.STAR_EQ ||
+ operator == TokenType.TILDE_SLASH_EQ) {
+ if (typeAccessor(rightHandSide) == intType) {
+ return intType;
+ }
+ }
+ }
+ // default
+ return currentType;
+ }
+
+ /**
* 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
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/static_type_analyzer_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698