r249896 - Fix inference of _Nullable for weak Objective-C properties.

Douglas Gregor via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 9 13:36:17 PDT 2015


Author: dgregor
Date: Fri Oct  9 15:36:17 2015
New Revision: 249896

URL: http://llvm.org/viewvc/llvm-project?rev=249896&view=rev
Log:
Fix inference of _Nullable for weak Objective-C properties.

The inference of _Nullable for weak Objective-C properties was broken
in several ways:

* It was back-patching the type information very late in the process
  of checking the attributes for an Objective-C property, which is
  just wrong.
* It was using ad hoc checks to try to suppress the warning about
  missing nullability specifiers (-Wnullability-completeness), which
  didn't actual work in all cases (rdar://problem/22985457)
* It was inferring _Nullable even outside of assumes-nonnull regions,
  which is wrong.

Putting the inference of _Nullable for weak Objective-C properties in
the same place as all of the other inference logic fixes all of these
ills.


Modified:
    cfe/trunk/lib/Sema/SemaObjCProperty.cpp
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/SemaObjC/arc-unavailable-for-weakref.m
    cfe/trunk/test/SemaObjC/nullable-weak-property.m
    cfe/trunk/test/SemaObjCXX/Inputs/nullability-consistency-2.h
    cfe/trunk/test/SemaObjCXX/Inputs/nullability-consistency-6.h

Modified: cfe/trunk/lib/Sema/SemaObjCProperty.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaObjCProperty.cpp?rev=249896&r1=249895&r2=249896&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaObjCProperty.cpp (original)
+++ cfe/trunk/lib/Sema/SemaObjCProperty.cpp Fri Oct  9 15:36:17 2015
@@ -181,7 +181,7 @@ Decl *Sema::ActOnProperty(Scope *S, Sour
   }
 
   // Validate the attributes on the @property.
-  CheckObjCPropertyAttributes(Res, AtLoc, Attributes, 
+  CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
                               (isa<ObjCInterfaceDecl>(ClassDecl) ||
                                isa<ObjCProtocolDecl>(ClassDecl)));
 
@@ -2301,13 +2301,6 @@ void Sema::CheckObjCPropertyAttributes(D
       if (*nullability == NullabilityKind::NonNull)
         Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
           << "nonnull" << "weak";
-    } else {
-        PropertyTy =
-          Context.getAttributedType(
-            AttributedType::getNullabilityAttrKind(NullabilityKind::Nullable),
-            PropertyTy, PropertyTy);
-        TypeSourceInfo *TSInfo = PropertyDecl->getTypeSourceInfo();
-        PropertyDecl->setType(PropertyTy, TSInfo);
     }
   }
 

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=249896&r1=249895&r2=249896&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Fri Oct  9 15:36:17 2015
@@ -3311,9 +3311,7 @@ static TypeSourceInfo *GetFullTypeForDec
 
   // Are we in an assume-nonnull region?
   bool inAssumeNonNullRegion = false;
-  if (S.PP.getPragmaAssumeNonNullLoc().isValid() &&
-      !state.getDeclarator().isObjCWeakProperty() &&
-      !S.deduceWeakPropertyFromType(T)) {
+  if (S.PP.getPragmaAssumeNonNullLoc().isValid()) {
     inAssumeNonNullRegion = true;
     // Determine which file we saw the assume-nonnull region in.
     FileID file = getNullabilityCompletenessCheckFileID(
@@ -3392,6 +3390,13 @@ static TypeSourceInfo *GetFullTypeForDec
         complainAboutMissingNullability = CAMN_No;
         break;
       }
+
+      // Weak properties are inferred to be nullable.
+      if (state.getDeclarator().isObjCWeakProperty() && inAssumeNonNullRegion) {
+        inferNullability = NullabilityKind::Nullable;
+        break;
+      }
+
       // fallthrough
 
     case Declarator::FileContext:

Modified: cfe/trunk/test/SemaObjC/arc-unavailable-for-weakref.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/arc-unavailable-for-weakref.m?rev=249896&r1=249895&r2=249896&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/arc-unavailable-for-weakref.m (original)
+++ cfe/trunk/test/SemaObjC/arc-unavailable-for-weakref.m Fri Oct  9 15:36:17 2015
@@ -56,7 +56,7 @@ __attribute__((objc_arc_weak_reference_u
 @interface I
 {
 }
- at property (weak) NSFont *font; // expected-error {{synthesizing __weak instance variable of type 'NSFont * _Nullable', which does not support weak references}}
+ at property (weak) NSFont *font; // expected-error {{synthesizing __weak instance variable of type 'NSFont *', which does not support weak references}}
 @end
 
 @implementation I // expected-note {{when implemented by class I}}
@@ -65,7 +65,7 @@ __attribute__((objc_arc_weak_reference_u
 
 // rdar://13676793
 @protocol MyProtocol
- at property (weak) NSFont *font; // expected-error {{synthesizing __weak instance variable of type 'NSFont * _Nullable', which does not support weak references}}
+ at property (weak) NSFont *font; // expected-error {{synthesizing __weak instance variable of type 'NSFont *', which does not support weak references}}
 @end
 
 @interface I1 <MyProtocol>
@@ -76,7 +76,7 @@ __attribute__((objc_arc_weak_reference_u
 @end
 
 @interface Super
- at property (weak) NSFont *font;  // expected-error {{synthesizing __weak instance variable of type 'NSFont * _Nullable', which does not support weak references}}
+ at property (weak) NSFont *font;  // expected-error {{synthesizing __weak instance variable of type 'NSFont *', which does not support weak references}}
 @end
 
 

Modified: cfe/trunk/test/SemaObjC/nullable-weak-property.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/nullable-weak-property.m?rev=249896&r1=249895&r2=249896&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/nullable-weak-property.m (original)
+++ cfe/trunk/test/SemaObjC/nullable-weak-property.m Fri Oct  9 15:36:17 2015
@@ -11,8 +11,16 @@ void foo (NSFoo * _Nonnull);
 @property(weak) NSFoo *property1;
 @end
 
+#pragma clang assume_nonnull begin
+ at interface NSBar ()
+ at property(weak) NSFoo *property2;
+ at end
+
+#pragma clang assume_nonnull end
+
 @implementation NSBar 
 - (void) Meth {
-   foo (self.property1); // expected-warning {{implicit conversion from nullable pointer 'NSFoo * _Nullable' to non-nullable pointer type 'NSFoo * _Nonnull'}}
+   foo (self.property1); // no warning because nothing is inferred
+   foo (self.property2); // expected-warning {{implicit conversion from nullable pointer 'NSFoo * _Nullable' to non-nullable pointer type 'NSFoo * _Nonnull'}}
 }
 @end

Modified: cfe/trunk/test/SemaObjCXX/Inputs/nullability-consistency-2.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/Inputs/nullability-consistency-2.h?rev=249896&r1=249895&r2=249896&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjCXX/Inputs/nullability-consistency-2.h (original)
+++ cfe/trunk/test/SemaObjCXX/Inputs/nullability-consistency-2.h Fri Oct  9 15:36:17 2015
@@ -13,4 +13,9 @@ void g3(const
 @property (retain,nullable) SomeClass *property2;
 - (nullable SomeClass *)method1;
 - (void)method2:(nonnull SomeClass *)param;
+ at property (readonly, weak) SomeClass *property3; // expected-warning{{missing a nullability type specifier}}
+ at end
+
+ at interface SomeClass ()
+ at property (readonly, weak) SomeClass *property4; // expected-warning{{missing a nullability type specifier}}
 @end

Modified: cfe/trunk/test/SemaObjCXX/Inputs/nullability-consistency-6.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/Inputs/nullability-consistency-6.h?rev=249896&r1=249895&r2=249896&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjCXX/Inputs/nullability-consistency-6.h (original)
+++ cfe/trunk/test/SemaObjCXX/Inputs/nullability-consistency-6.h Fri Oct  9 15:36:17 2015
@@ -4,5 +4,15 @@ int *ptr; // expected-warning {{missing
 
 extern void **blah; // expected-warning 2{{missing a nullability type specifier}}
 
+__attribute__((objc_root_class))
+ at interface ClassWithWeakProperties
+ at property (readonly, weak) ClassWithWeakProperties *prop1;
+ at property (readonly, weak, null_unspecified) ClassWithWeakProperties *prop2;
+ at end
+
+ at interface ClassWithWeakProperties ()
+ at property (readonly, weak) ClassWithWeakProperties *prop3;
+ at end
+
 #pragma clang assume_nonnull end
 




More information about the cfe-commits mailing list