From b3a706870021df81ca678400746ed44d20b74f49 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Fri, 30 May 2014 16:35:53 +0000 Subject: [PATCH] Objective-C. Diagnose assigning a block pointer type to an Objective-C object type other than 'id'. // rdar://16739120 llvm-svn: 209906 --- clang/lib/Sema/SemaExpr.cpp | 4 ++-- clang/test/SemaObjC/block-type-safety.m | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 9b4c6382dcef..be00c0e8b37f 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -6435,8 +6435,8 @@ Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, return IncompatiblePointer; } - // T^ -> A* - if (RHSType->isBlockPointerType()) { + // T^ -> id; not T^ ->A* and not T^ -> id

+ if (RHSType->isBlockPointerType() && LHSType->isObjCIdType()) { maybeExtendBlockObject(*this, RHS); Kind = CK_BlockPointerToObjCPointerCast; return Compatible; diff --git a/clang/test/SemaObjC/block-type-safety.m b/clang/test/SemaObjC/block-type-safety.m index 56342baae528..45866704b56d 100644 --- a/clang/test/SemaObjC/block-type-safety.m +++ b/clang/test/SemaObjC/block-type-safety.m @@ -155,3 +155,25 @@ void f() { return NSOrderedSame; }]; } + +// rdar://16739120 +@protocol P1 @end +@protocol P2 @end + +void Test() { +void (^aBlock)(); +id anId = aBlock; // OK + +id anQualId = aBlock; // expected-error {{initializing 'id' with an expression of incompatible type 'void (^)()'}} + +NSArray* anArray = aBlock; // expected-error {{initializing 'NSArray *' with an expression of incompatible type 'void (^)()'}} + +aBlock = anId; // OK + +id anQualId1; +aBlock = anQualId1; // expected-error {{assigning to 'void (^)()' from incompatible type 'id'}} + +NSArray* anArray1; +aBlock = anArray1; // expected-error {{assigning to 'void (^)()' from incompatible type 'NSArray *'}} +} +