[PATCH] D39762: [ObjC] Boxed strings should use the nullability from stringWithUTF8String's return type

Alex Lorenz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 8 13:33:34 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL317727: [ObjC] Boxed strings should use the nullability from stringWithUTF8String's… (authored by arphaman).

Changed prior to commit:
  https://reviews.llvm.org/D39762?vs=121982&id=122148#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D39762

Files:
  cfe/trunk/lib/Sema/SemaExprObjC.cpp
  cfe/trunk/test/SemaObjC/transfer-boxed-string-nullability.m


Index: cfe/trunk/lib/Sema/SemaExprObjC.cpp
===================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp
@@ -564,6 +564,13 @@
       
       BoxingMethod = StringWithUTF8StringMethod;
       BoxedType = NSStringPointer;
+      // Transfer the nullability from method's return type.
+      Optional<NullabilityKind> Nullability =
+          BoxingMethod->getReturnType()->getNullability(Context);
+      if (Nullability)
+        BoxedType = Context.getAttributedType(
+            AttributedType::getNullabilityAttrKind(*Nullability), BoxedType,
+            BoxedType);
     }
   } else if (ValueType->isBuiltinType()) {
     // The other types we support are numeric, char and BOOL/bool. We could also
Index: cfe/trunk/test/SemaObjC/transfer-boxed-string-nullability.m
===================================================================
--- cfe/trunk/test/SemaObjC/transfer-boxed-string-nullability.m
+++ cfe/trunk/test/SemaObjC/transfer-boxed-string-nullability.m
@@ -0,0 +1,28 @@
+// RUN: %clang_cc1 -fblocks -fobjc-arc -Wnullable-to-nonnull-conversion -fsyntax-only -verify -Wno-objc-root-class %s
+// RUN: %clang_cc1 -fblocks -fobjc-arc -Wnullable-to-nonnull-conversion -fsyntax-only -verify -Wno-objc-root-class -DNOWARN %s
+
+ at interface NSString
+
++ (NSString*
+#ifndef NOWARN
+  _Nullable
+#else
+  _Nonnull
+#endif
+) stringWithUTF8String:(const char*)x;
+
+ at end
+
+void takesNonNull(NSString * _Nonnull ptr);
+
+void testBoxedString() {
+  const char *str = "hey";
+  takesNonNull([NSString stringWithUTF8String:str]);
+  takesNonNull(@(str));
+#ifndef NOWARN
+  // expected-warning at -3 {{implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull'}}
+  // expected-warning at -3 {{implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull'}}
+#else
+  // expected-no-diagnostics
+#endif
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39762.122148.patch
Type: text/x-patch
Size: 1998 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171108/aaecf0ef/attachment-0001.bin>


More information about the cfe-commits mailing list