Complain when we try to initialize an object of Objective-C class type

(which is ill-formed) with an initializer list. Also, change the
fallback from an assertion to a generic error message, which is far
friendlier. Fixes <rdar://problem/7730948>.

llvm-svn: 102930
This commit is contained in:
Douglas Gregor 2010-05-03 18:24:37 +00:00
parent 9f5200a122
commit 50ec46d4af
3 changed files with 16 additions and 3 deletions

View File

@ -1691,6 +1691,8 @@ def err_empty_scalar_initializer : Error<"scalar initializer cannot be empty">;
def err_illegal_initializer : Error< def err_illegal_initializer : Error<
"illegal initializer (only variables can be initialized)">; "illegal initializer (only variables can be initialized)">;
def err_illegal_initializer_type : Error<"illegal initializer type %0">; def err_illegal_initializer_type : Error<"illegal initializer type %0">;
def err_init_objc_class : Error<
"cannot initialize Objective-C class type %0">;
def err_implicit_empty_initializer : Error< def err_implicit_empty_initializer : Error<
"initializer for aggregate with no elements requires explicit braces">; "initializer for aggregate with no elements requires explicit braces">;
def err_bitfield_has_negative_width : Error< def err_bitfield_has_negative_width : Error<

View File

@ -624,10 +624,14 @@ void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity,
} else if (DeclType->isReferenceType()) { } else if (DeclType->isReferenceType()) {
CheckReferenceType(Entity, IList, DeclType, Index, CheckReferenceType(Entity, IList, DeclType, Index,
StructuredList, StructuredIndex); StructuredList, StructuredIndex);
} else if (DeclType->isObjCInterfaceType()) {
SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class)
<< DeclType;
hadError = true;
} else { } else {
// In C, all types are either scalars or aggregates, but SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
// additional handling is needed here for C++ (and possibly others?). << DeclType;
assert(0 && "Unsupported initializer type"); hadError = true;
} }
} }

View File

@ -0,0 +1,7 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
@interface A
@end
void f() {
(A){ 0 }; // expected-error{{cannot initialize Objective-C class type 'A'}}
}