From 9d672b17b74ad1ad27290a6f3daf58defabc1fbc Mon Sep 17 00:00:00 2001
From: Akira Hatanaka <ahatanaka@apple.com>
Date: Mon, 25 Jul 2016 21:58:19 +0000
Subject: [PATCH] [Sema][ObjC] Compute the nullability of a conditional
 expression based on the nullabilities of its operands.

This commit is a follow-up to r276076 and enables
computeConditionalNullability to compute the merged nullability when
the operands are objective-c pointers.

rdar://problem/22074116


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@276696 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Sema/SemaExpr.cpp       |  2 +-
 test/SemaObjC/nullability.m | 28 +++++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 6322e15826a..42c44b183e5 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -7016,7 +7016,7 @@ static void DiagnoseConditionalPrecedence(Sema &Self,
 static QualType computeConditionalNullability(QualType ResTy, bool IsBin,
                                               QualType LHSTy, QualType RHSTy,
                                               ASTContext &Ctx) {
-  if (!ResTy->isPointerType())
+  if (!ResTy->isAnyPointerType())
     return ResTy;
 
   auto GetNullability = [&Ctx](QualType Ty) {
diff --git a/test/SemaObjC/nullability.m b/test/SemaObjC/nullability.m
index 36ac6b9ddea..fbf014c0f9c 100644
--- a/test/SemaObjC/nullability.m
+++ b/test/SemaObjC/nullability.m
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -fblocks -Woverriding-method-mismatch -Wno-nullability-declspec %s -verify
+// RUN: %clang_cc1 -fsyntax-only -fblocks -Woverriding-method-mismatch -Wno-nullability-declspec -Wnullable-to-nonnull-conversion %s -verify
 
 __attribute__((objc_root_class))
 @interface NSFoo
@@ -230,3 +230,29 @@ void testBlockLiterals() {
 
   int *x = (^ _Nullable id(void) { return 0; })(); // expected-warning{{incompatible pointer types initializing 'int *' with an expression of type 'id _Nullable'}}
 }
+
+// Check nullability of conditional expressions.
+void conditional_expr(int c) {
+  NSFoo * _Nonnull p;
+  NSFoo * _Nonnull nonnullP;
+  NSFoo * _Nullable nullableP;
+  NSFoo * _Null_unspecified unspecifiedP;
+  NSFoo *noneP;
+
+  p = c ? nonnullP : nonnullP;
+  p = c ? nonnullP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'NSFoo * _Nullable' to non-nullable pointer type 'NSFoo * _Nonnull'}}
+  p = c ? nonnullP : unspecifiedP;
+  p = c ? nonnullP : noneP;
+  p = c ? nullableP : nonnullP; // expected-warning{{implicit conversion from nullable pointer 'NSFoo * _Nullable' to non-nullable pointer type 'NSFoo * _Nonnull'}}
+  p = c ? nullableP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'NSFoo * _Nullable' to non-nullable pointer type 'NSFoo * _Nonnull'}}
+  p = c ? nullableP : unspecifiedP; // expected-warning{{implicit conversion from nullable pointer 'NSFoo * _Nullable' to non-nullable pointer type 'NSFoo * _Nonnull'}}
+  p = c ? nullableP : noneP; // expected-warning{{implicit conversion from nullable pointer 'NSFoo * _Nullable' to non-nullable pointer type 'NSFoo * _Nonnull'}}
+  p = c ? unspecifiedP : nonnullP;
+  p = c ? unspecifiedP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'NSFoo * _Nullable' to non-nullable pointer type 'NSFoo * _Nonnull'}}
+  p = c ? unspecifiedP : unspecifiedP;
+  p = c ? unspecifiedP : noneP;
+  p = c ? noneP : nonnullP;
+  p = c ? noneP : nullableP; // expected-warning{{implicit conversion from nullable pointer 'NSFoo * _Nullable' to non-nullable pointer type 'NSFoo * _Nonnull'}}
+  p = c ? noneP : unspecifiedP;
+  p = c ? noneP : noneP;
+}
-- 
GitLab