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

Unified Diff: compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java

Issue 10537043: Issue 3308. Support for operator equals (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 6 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: compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
diff --git a/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java b/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
index 277a7c0eb8717d97a2ec8a4ba74fc8b300e02201..0a9b613ef078a7e92482c8a187a781ab504b9b02 100644
--- a/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
+++ b/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
@@ -296,6 +296,9 @@ public class TypeAnalyzer implements DartCompilationPhase {
}
private String methodNameForBinaryOperator(Token operator) {
+ if (operator == Token.EQ) {
+ return "operator equals";
+ }
return "operator " + operator.getSyntax();
}
@@ -429,7 +432,17 @@ public class TypeAnalyzer implements DartCompilationPhase {
case GTE:
return analyzeBinaryOperator(node, lhs, operator, lhsNode, rhsNode);
- case EQ:
+ case EQ: {
+ // try to resolve "==" to "operator equals()", but don't complain if can not find it
+ String methodName = methodNameForBinaryOperator(operator);
+ InterfaceType itype = types.getInterfaceType(lhs);
+ if (itype != null) {
+ Member member = itype.lookupMember(methodName);
+ if (member != null) {
+ node.setElement(member.getElement());
+ }
+ }
+ }
case NE:
case EQ_STRICT:
case NE_STRICT:
@@ -1575,13 +1588,21 @@ public class TypeAnalyzer implements DartCompilationPhase {
}
}
}
+ // operator "equals" should return "bool"
+ if (modifiers.isOperator() && methodElement.getName().equals("equals")
+ && returnTypeNode != null) {
+ Type returnType = node.getElement().getFunctionType().getReturnType();
+ if (!Objects.equal(returnType, boolType)) {
+ typeError(returnTypeNode, TypeErrorCode.OPERATOR_EQUALS_BOOL_RETURN_TYPE);
+
+ }
+ }
// operator "negate" should return numeric type
if (modifiers.isOperator() && methodElement.getName().equals("negate")
&& returnTypeNode != null) {
Type returnType = node.getElement().getFunctionType().getReturnType();
if (!types.isSubtype(returnType, numType)) {
typeError(returnTypeNode, TypeErrorCode.OPERATOR_NEGATE_NUM_RETURN_TYPE);
-
}
}
// done

Powered by Google App Engine
This is Rietveld 408576698