[clang] [ObjC] Emit number, array, and dictionary literals as constants (PR #185130)
James Y Knight via cfe-commits
cfe-commits at lists.llvm.org
Mon May 4 06:59:40 PDT 2026
jyknight wrote:
I think this isn't a bug in Foundation _or_ in the compiler, as AFAIK it has always acted this way since the beginning of timeā¢. Though the behavior is definitely weird.
There's two separate things:
Firstly, realize that `NSNumber isEqualToValue` doesn't do the same thing as `NSNumber isEqual` or `NSNumber isEqualToNumber`. The `isEqualToValue` method, uniquely, tests both the value-type, and the numeric value, while the others test numeric equality. Why? No idea...it seems pretty broken behavior, but it's been like that forever. So I think at this point it's "expected".
Secondly, the `numberWith<inttype>` has never guaranteed to return an object of the exact value-type you name. And in practice, it commonly does NOT. E.g. `NSNumber numberWithUnsignedLong` compares the value to `LLONG_MAX`; if it's less, it uses "q" (long long); if it's larger, it uses "Q" (unsigned long long). And for smaller unsigned types, it'll always prefer to return an NSNumber for the next-higher signed type (e.g. `numberWithUnsignedChar` -> "s" (short); `numberWithUnsignedShort` -> "i" (int), etc).
That means that, in practice, isEqualToValue is -- and has always been -- pretty much useless. Like, `[[NSNumber numberWithShort: 1] isEqualToValue: [NSNumber numberWithInt: 1]]` is false but `[[NSNumber numberWithShort: 1] isEqualToValue: [NSNumber numberWithUnsignedChar: 1]]` is true. That makes no sense, unless you know implementation details.
I suppose the new compiler feature _could_ attempt to recreate the exact behavior of which value type each `numberWithX` uses in Apple Foundation. But I don't think that's really a useful thing to do.
I'd recommend, instead, that the `-fobjc-constant-literals` flag be re-enabled as-is, and the impacted code should switch away from the weirdly-behaving `isEqualToValue` call, to instead call `isEqual` or `isEqualToNumber`.
https://github.com/llvm/llvm-project/pull/185130
More information about the cfe-commits
mailing list