From 34020d39b8a116770aad0b5ad1a450e6e22da5cd Mon Sep 17 00:00:00 2001 From: Michael Wyman Date: Wed, 19 Oct 2022 13:11:41 -0700 Subject: [PATCH] Don't emit `-Wnullability-completeness` warnings on `weak` Objective-C properties. Zeroing weak references are by definition `nullable`, and adding `nonnull` or `_Nullable` yields a mutual-exclusivity error. When `-Wnullability-completeness` is enabled, however, non-audited header regions require adding the `nullable` property keyword to avoid a warning. This should be unnecessary, since it restates known nullability of the `weak` property. Additionally, the fix-it hints are both non-idiomatic Objective-C (adding `_Nullable` to the property's pointer type rather than in the `@property` attributes) and suggest the option of adding `_Nonnull`, which would be an error. Differential Revision: https://reviews.llvm.org/D128031 --- clang/lib/Sema/SemaType.cpp | 9 +++++++-- clang/test/SemaObjCXX/Inputs/nullability-consistency-2.h | 8 ++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 6bd973e..3fe85a2 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -4748,8 +4748,13 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, } // Weak properties are inferred to be nullable. - if (state.getDeclarator().isObjCWeakProperty() && inAssumeNonNullRegion) { - inferNullability = NullabilityKind::Nullable; + if (state.getDeclarator().isObjCWeakProperty()) { + // Weak properties cannot be nonnull, and should not complain about + // missing nullable attributes during completeness checks. + complainAboutMissingNullability = CAMN_No; + if (inAssumeNonNullRegion) { + inferNullability = NullabilityKind::Nullable; + } break; } diff --git a/clang/test/SemaObjCXX/Inputs/nullability-consistency-2.h b/clang/test/SemaObjCXX/Inputs/nullability-consistency-2.h index 84a1369..6d76799 100644 --- a/clang/test/SemaObjCXX/Inputs/nullability-consistency-2.h +++ b/clang/test/SemaObjCXX/Inputs/nullability-consistency-2.h @@ -19,13 +19,9 @@ void g3(const @property (retain,nullable) SomeClass *property2; - (nullable SomeClass *)method1; - (void)method2:(nonnull SomeClass *)param; -@property (readonly, weak) SomeClass *property3; // expected-warning{{missing a nullability type specifier}} -// expected-note@-1 {{insert '_Nullable' if the pointer may be null}} -// expected-note@-2 {{insert '_Nonnull' if the pointer should never be null}} +@property (readonly, weak) SomeClass *property3; @end @interface SomeClass () -@property (readonly, weak) SomeClass *property4; // expected-warning{{missing a nullability type specifier}} -// expected-note@-1 {{insert '_Nullable' if the pointer may be null}} -// expected-note@-2 {{insert '_Nonnull' if the pointer should never be null}} +@property (readonly, weak) SomeClass *property4; @end -- 2.7.4