Remove warning for using a factory constructor on an abstract class.

http://code.google.com/p/dart/issues/detail?id=2282

Review URL: https://chromiumcodereview.appspot.com//10106016

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@6619 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
zundel@google.com 2012-04-17 09:30:21 +00:00
parent fdcd0cb5c4
commit 082190545e
4 changed files with 36 additions and 15 deletions

View file

@ -28,8 +28,6 @@ public enum TypeErrorCode implements ErrorCode {
FOR_IN_WITH_ITERATOR_FIELD("iterator is a field, expected an iterator() method"),
FOR_IN_WITH_INVALID_ITERATOR_RETURN_TYPE("iterator method's return type is not assignable to %s"),
INSTANTIATION_OF_ABSTRACT_CLASS("instantiation of an abstract class '%s'"),
INSTANTIATION_OF_ABSTRACT_CLASS_USING_FACTORY(
"instantiation of an abstract class '%s' using factory"),
INSTANTIATION_OF_CLASS_WITH_UNIMPLEMENTED_MEMBERS(
"instantiation of class %s with the inherited abstract members: %s"),
INTERFACE_HAS_NO_METHOD_NAMED("%s has no method named \"%s\""),

View file

@ -1218,12 +1218,8 @@ public class TypeAnalyzer implements DartCompilationPhase {
} else {
ClassElement cls = (ClassElement) constructorElement.getEnclosingElement();
// Add warning for instantiating abstract class.
if (cls.isAbstract()) {
ErrorCode errorCode =
constructorElement.getModifiers().isFactory()
? TypeErrorCode.INSTANTIATION_OF_ABSTRACT_CLASS_USING_FACTORY
: TypeErrorCode.INSTANTIATION_OF_ABSTRACT_CLASS;
typeError(typeName, errorCode, cls.getName());
if (cls.isAbstract() && !constructorElement.getModifiers().isFactory()) {
typeError(typeName, TypeErrorCode.INSTANTIATION_OF_ABSTRACT_CLASS, cls.getName());
} else {
List<Element> unimplementedMembers = findUnimplementedMembers(cls);
if (unimplementedMembers.size() > 0) {

View file

@ -3,9 +3,6 @@
// BSD-style license that can be found in the LICENSE file.
package com.google.dart.compiler.type;
import static com.google.dart.compiler.common.ErrorExpectation.assertErrors;
import static com.google.dart.compiler.common.ErrorExpectation.errEx;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
@ -42,6 +39,9 @@ import com.google.dart.compiler.resolver.NodeElement;
import com.google.dart.compiler.resolver.ResolverErrorCode;
import com.google.dart.compiler.resolver.TypeErrorCode;
import static com.google.dart.compiler.common.ErrorExpectation.assertErrors;
import static com.google.dart.compiler.common.ErrorExpectation.errEx;
import java.io.Reader;
import java.io.StringReader;
import java.net.URI;
@ -116,7 +116,7 @@ public class TypeAnalyzerCompilerTest extends CompilerTestCase {
MethodElement methodElement = (MethodElement) functionElement.getEnclosingElement();
assertNotNull(methodElement);
assertSame(ElementKind.METHOD, methodElement.getKind());
assertEquals("foo", ((MethodElement) methodElement).getName());
assertEquals("foo", methodElement.getName());
// use EnclosingElement methods implementations in MethodElement
assertEquals(false, methodElement.isInterface());
assertEquals(true, Iterables.isEmpty(methodElement.getMembers()));
@ -419,7 +419,7 @@ public class TypeAnalyzerCompilerTest extends CompilerTestCase {
* Factory constructor can instantiate any class and return it non-abstract class instance, but
* spec requires warnings, so we provide it, but using different constant.
*/
public void test_warnAbstract_onAbstractClass_whenInstantiate_factoryConstructor()
public void test_abstractClass_whenInstantiate_factoryConstructor()
throws Exception {
AnalyzeLibraryResult libraryResult =
analyzeLibrary(
@ -436,8 +436,7 @@ public class TypeAnalyzerCompilerTest extends CompilerTestCase {
" }",
"}"));
assertErrors(
libraryResult.getTypeErrors(),
errEx(TypeErrorCode.INSTANTIATION_OF_ABSTRACT_CLASS_USING_FACTORY, 8, 16, 1));
libraryResult.getTypeErrors());
}
/**

View file

@ -0,0 +1,28 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test program for constructors and initializers.
// Exercises issue 2282, factory constructors in abstract classes should
// not emit a static type warning
class B extends A1 {
B() {}
method() {}
}
class A1 {
A1() {}
abstract method();
factory A1.make() { return new B(); }
}
class A2 {
abstract method();
A2.make() {}
}
main() {
new A1.make();
new A2.make(); /// 01: static type warning
}