[PATCH] D58729: Emit boxed expression as a compile-time constant if the expression inside the parentheses is a string literal

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 27 18:35:52 PST 2019

ahatanak marked an inline comment as done.
ahatanak added inline comments.

Comment at: lib/Sema/SemaExprObjC.cpp:534
+            NSStringPointer, NSStringPointer);
+        return new (Context) ObjCBoxedExpr(SL, BoxedType, nullptr, SR);
+      }
rjmccall wrote:
> You're implicitly dropping sugar from the source expression here; I really think you should preserve that, and if it means you end up having to look through array-decay expressions, that's not the end of the world.
> The need for a special case here is just to allow us to compile these even if there isn't a boxing method available?
> Do you need to restrict the type of the string literal so that it doesn't apply to e.g. wide strings?
Line 516 checks that the pointee type has the same unqualified type as 'char'. If the string literal inside the parentheses is of a wider type (e.g., `@(u"abc")`), this piece of code won't be executed, and a diagnostic is emitted later ("illegal type 'const char16_t *' used in a boxed expression").

The original motivation for special-casing string literals inside boxed expressions was to silence the `-Wnullable-to-nonnull-conversion` warning mentioned here: https://oleb.net/2018/@keypath/. The warning is issued because the return type of `stringWithUTF8String` is nullable.

+ (nullable instancetype)stringWithUTF8String:(const char *)nullTerminatedCString;

NSString * _Nonnull ptr = @("abc");  // expected-error {{implicit conversion from nullable pointer 'NSString * _Nullable' to non-nullable pointer type 'NSString * _Nonnull'}}

  rC Clang



More information about the cfe-commits mailing list