[clang] [ObjC] Emit number, array, and dictionary literals as constants (PR #185130)

Oliver Hunt via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 6 16:55:11 PST 2026


================
@@ -875,6 +948,24 @@ ExprResult SemaObjC::BuildObjCArrayLiteral(SourceRange SR,
   QualType ObjectsType = ArrayWithObjectsMethod->parameters()[0]->getType();
   QualType RequiredType = ObjectsType->castAs<PointerType>()->getPointeeType();
 
+  const LangOptions &LangOpts = getLangOpts();
+
+  bool ExpressibleAsConstantInitLiteral = LangOpts.ConstantNSArrayLiterals;
+
+  // ExpressibleAsConstantInitLiteral isn't meaningful for dependent literals.
+  if (ExpressibleAsConstantInitLiteral &&
+      llvm::any_of(Elements,
+                   [](Expr *Elem) { return Elem->isValueDependent(); }))
+    ExpressibleAsConstantInitLiteral = false;
+
+  // We can stil emit a constant empty array
+  if (LangOpts.ObjCConstantLiterals && Elements.size() == 0) {
+    assert(LangOpts.ObjCRuntime.hasConstantEmptyCollections() &&
+           "The current ABI doesn't support an empty constant NSArray "
+           "singleton!");
----------------
ojhunt wrote:

I haven't yet looked at all this files, but is it possible for a user to hit this with "correct" code but incorrect config? (e.g. should this be an error rather than an assert)

Or a warning and set ExpressibleAsConstantInitLiteral to false?

https://github.com/llvm/llvm-project/pull/185130


More information about the cfe-commits mailing list