From: Akira Hatanaka Date: Thu, 26 Jul 2018 17:51:13 +0000 (+0000) Subject: [Sema][ObjC] Do not propagate the nullability specifier on the receiver X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dcd0309c08ba6c3c39bb75ed975fcf95ca63ad7f;p=clang [Sema][ObjC] Do not propagate the nullability specifier on the receiver to the result type of a message send if the result type cannot have a nullability specifier. Previously, clang would print the following message when the code in nullability.m was compiled: "incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int _Nullable'" This is wrong as 'int' isn't supposed to have any nullability specifiers. rdar://problem/40830514 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@338048 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index bf9d081bb9..bf0ffeba06 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -1357,6 +1357,11 @@ QualType Sema::getMessageSendResultType(QualType ReceiverType, if (isClassMessage) return resultType; + // There is nothing left to do if the result type cannot have a nullability + // specifier. + if (!resultType->canHaveNullability()) + return resultType; + // Map the nullability of the result into a table index. unsigned receiverNullabilityIdx = 0; if (auto nullability = ReceiverType->getNullability(Context)) diff --git a/test/SemaObjC/nullability.m b/test/SemaObjC/nullability.m index cbfe132d14..93f4d1d499 100644 --- a/test/SemaObjC/nullability.m +++ b/test/SemaObjC/nullability.m @@ -279,3 +279,14 @@ void test(ArraysInMethods *obj) { [obj simpleSugar:0]; // expected-warning {{null passed to a callee that requires a non-null argument}} [obj sugarWithTypedef:0]; // expected-warning {{null passed to a callee that requires a non-null argument}} } + +// Check that we don't propagate the nullability specifier on the receiver to +// the result type of a message send if the result type cannot have a +// nullability specifier. +@interface C0 +-(int) count; +@end + +void testMessageSendResultType(C0 * _Nullable c0) { + int *p = [c0 count]; // expected-warning {{incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int'}} +}