[clang] [clang][nullability] allow _Nonnull etc on nullable class types (PR #82705)

James Y Knight via cfe-commits cfe-commits at lists.llvm.org
Thu May 9 09:24:49 PDT 2024


jyknight wrote:

I note that this commit (probably unintentionally) fixed an issue with the `-Wnonnull` warning for return values in Objective-C++ code.

Previously, `id _Nonnull foo() { return nullptr; }` does not warn under `clang -fobjc-arc -target x86_64-apple-macos11 -x objective-c++ -fsyntax-only`. It did warn with `-fno-objc-arc`, however. Now, it does warn in both cases. This seems like an improvement -- but the state of things is still pretty weird.

But, that only started working with `return nullptr`, not with `return 0`, though I'd expect those to be treated the same way. It also didn't start working for `-x objective-c` mode with a `return (void*)0;`.

(https://godbolt.org/z/1voxqhKbE)

I'd also have expected that this change in behavior would have required test changes to the objc nullability tests, but it didn't, because mostly tests don't enable `-fobjc-arc`. The test case `returnsNilObjCInstanceDirectly` in `clang/test/Analysis/nullability_nullonly.mm` does seem like it'd notice the change in behavior, but it has defined `nil` as `0`, not `nullptr`. (Also, that's primarily a test for Clang Static Analyzer's nullability checker, not Sema's nullability warning. The same function in `clang/test/Analysis/nullability-no-arc.mm` does see the Sema -Wnonnull warning too, however.)

Anyways, I don't think it's _bad_ that this commit has had this particular effect, but it's weird, and I wonder if this weirdness might affect other situations than ObjC ARC types, too.

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


More information about the cfe-commits mailing list