From f6da0fd888382d274bbb0bfceab4724c701b4922 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209906 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExpr.cpp | 4 ++-- test/SemaObjC/block-type-safety.m | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 9b4c6382dc..be00c0e8b3 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/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/test/SemaObjC/block-type-safety.m b/test/SemaObjC/block-type-safety.m index 56342baae5..45866704b5 100644 --- a/test/SemaObjC/block-type-safety.m +++ b/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 *'}} +} + -- 2.40.0