Allow not only 'Iterator iterator()' but also 'Iterator iterator' (field).

R=brianwilkerson@google.com
BUG=

Review URL: https://codereview.chromium.org//11419186

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@15411 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
scheglov@google.com 2012-11-27 19:50:22 +00:00
parent 2f400a15d7
commit 4f5cbe2eff
3 changed files with 78 additions and 37 deletions

View file

@ -1785,9 +1785,15 @@ public class TypeAnalyzer implements DartCompilationPhase {
Member iteratorMember = lookupMember(iterableType, "iterator", iterableExpression);
Type elementType = null;
if (iteratorMember != null) {
Type memberReturnType = null;
if (TypeKind.of(iteratorMember.getType()) == TypeKind.FUNCTION) {
FunctionType iteratorMethod = (FunctionType) iteratorMember.getType();
InterfaceType asInstanceOf = types.asInstanceOf(iteratorMethod.getReturnType(),
memberReturnType = iteratorMethod.getReturnType();
} else if (ElementKind.of(iteratorMember.getElement()) == ElementKind.FIELD) {
memberReturnType = iteratorMember.getType();
}
if (memberReturnType != null) {
InterfaceType asInstanceOf = types.asInstanceOf(memberReturnType,
dynamicIteratorType.getElement());
if (asInstanceOf != null) {
elementType = asInstanceOf.getArguments().get(0);
@ -1795,8 +1801,7 @@ public class TypeAnalyzer implements DartCompilationPhase {
} else {
InterfaceType expectedIteratorType = dynamicIteratorType.subst(
Arrays.asList(variableType), dynamicIteratorType.getElement().getTypeParameters());
typeError(iterableExpression,
TypeErrorCode.FOR_IN_WITH_INVALID_ITERATOR_RETURN_TYPE,
typeError(iterableExpression, TypeErrorCode.FOR_IN_WITH_INVALID_ITERATOR_RETURN_TYPE,
expectedIteratorType);
}
} else {

View file

@ -5695,4 +5695,74 @@ public class TypeAnalyzerCompilerTest extends CompilerTestCase {
assertErrors(result.getErrors(),
errEx(ResolverErrorCode.VARIABLE_REFERENCES_SAME_NAME_IN_INITIALIZER, 3, 11, 1));
}
public void test_ForEachStatement_method() throws Exception {
AnalyzeLibraryResult result = analyzeLibrary(
"// filler filler filler filler filler filler filler filler filler filler",
"class A {",
" Iterator<int> iterator() {}",
"}",
"main() {",
" A a = new A();",
" for (int v in a) {}",
"}",
"");
assertErrors(result.getErrors());
}
public void test_ForEachStatement_negative_method_invalidReturnType() throws Exception {
AnalyzeLibraryResult result = analyzeLibrary(
"// filler filler filler filler filler filler filler filler filler filler",
"class A {",
" int iterator() {}",
"}",
"main() {",
" A a = new A();",
" for (int v in a) {}",
"}",
"");
assertErrors(
result.getErrors(),
errEx(TypeErrorCode.FOR_IN_WITH_INVALID_ITERATOR_RETURN_TYPE, 7, 17, 1));
}
/**
* It was negative test, but in the new <code>Iterator</code> field should be also supported.
*/
public void test_ForEachStatement_field() throws Exception {
AnalyzeLibraryResult result = analyzeLibrary(
"// filler filler filler filler filler filler filler filler filler filler",
"class A {",
" Iterator iterator;",
"}",
"main() {",
" A a = new A();",
" for (int v in a) {}",
"}",
"");
assertErrors(result.getErrors());
// Map<String, ClassNodeElement> fieldNotMethod = loadSource(
// "class A {",
// " int iterator;",
// "}",
// "class B {",
// " main() { for (int i in new A()) {}}",
// "}");
// analyzeClasses(fieldNotMethod, TypeErrorCode.FOR_IN_WITH_ITERATOR_FIELD);
}
public void test_ForEachStatement_negative_field_invalidReturnType() throws Exception {
AnalyzeLibraryResult result = analyzeLibrary(
"// filler filler filler filler filler filler filler filler filler filler",
"class A {",
" int iterator;",
"}",
"main() {",
" A a = new A();",
" for (int v in a) {}",
"}",
"");
assertErrors(result.getErrors(),
errEx(TypeErrorCode.FOR_IN_WITH_INVALID_ITERATOR_RETURN_TYPE, 7, 17, 1));
}
}

View file

@ -404,40 +404,6 @@ public class TypeAnalyzerTest extends TypeAnalyzerTestCase {
analyzeClass(classes.get("Bad"), 3);
}
public void testForEachStatement() {
Map<String, ClassNodeElement> invalidReturnType = loadSource(
"class A {",
" Iterator<int> iterator() {}",
"}",
"class B {",
" main() { for (int i in new A()) {}}",
"}");
analyzeClasses(invalidReturnType);
}
public void testForEachStatement_Negative1() {
Map<String, ClassNodeElement> fieldNotMethod = loadSource(
"class A {",
" int iterator;",
"}",
"class B {",
" main() { for (int i in new A()) {}}",
"}");
analyzeClasses(fieldNotMethod, TypeErrorCode.FOR_IN_WITH_ITERATOR_FIELD);
}
public void testForEachStatement_Negative2() {
Map<String, ClassNodeElement> invalidReturnType = loadSource(
"class A {",
" int iterator() {}",
"}",
"class B {",
" main() { for (int i in new A()) {}}",
"}");
analyzeClasses(invalidReturnType, TypeErrorCode.FOR_IN_WITH_INVALID_ITERATOR_RETURN_TYPE);
}
public void testForStatement() {
analyze("for (;true;) {}");
analyze("for (;null;) {}");