r307642 - [ObjC] Check that a subscript methods is declared for a qualified id type

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 11 03:18:35 PDT 2017


Author: arphaman
Date: Tue Jul 11 03:18:35 2017
New Revision: 307642

URL: http://llvm.org/viewvc/llvm-project?rev=307642&view=rev
Log:
[ObjC] Check that a subscript methods is declared for a qualified id type

Objective-C subscript expressions report errors when a subscript method is not
declared in the base class. However, prior to this commit, qualified id types
were not checked. This commit ensures that an appropriate error is reported
when a subscript method is not declared in any of the protocols that are
included in the qualified id type.

rdar://33213924

Modified:
    cfe/trunk/lib/Sema/SemaPseudoObject.cpp
    cfe/trunk/test/SemaObjC/objc-container-subscripting-1.m
    cfe/trunk/test/SemaObjC/objc-container-subscripting-2.m

Modified: cfe/trunk/lib/Sema/SemaPseudoObject.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaPseudoObject.cpp?rev=307642&r1=307641&r2=307642&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaPseudoObject.cpp (original)
+++ cfe/trunk/lib/Sema/SemaPseudoObject.cpp Tue Jul 11 03:18:35 2017
@@ -1176,8 +1176,6 @@ bool ObjCSubscriptOpBuilder::findAtIndex
   
   AtIndexGetter = S.LookupMethodInObjectType(AtIndexGetterSelector, ResultType, 
                                              true /*instance*/);
-  bool receiverIdType = (BaseT->isObjCIdType() ||
-                         BaseT->isObjCQualifiedIdType());
   
   if (!AtIndexGetter && S.getLangOpts().DebuggerObjCLiteral) {
     AtIndexGetter = ObjCMethodDecl::Create(S.Context, SourceLocation(), 
@@ -1203,7 +1201,7 @@ bool ObjCSubscriptOpBuilder::findAtIndex
   }
 
   if (!AtIndexGetter) {
-    if (!receiverIdType) {
+    if (!BaseT->isObjCIdType()) {
       S.Diag(BaseExpr->getExprLoc(), diag::err_objc_subscript_method_not_found)
       << BaseExpr->getType() << 0 << arrayRef;
       return false;
@@ -1284,9 +1282,6 @@ bool ObjCSubscriptOpBuilder::findAtIndex
   }
   AtIndexSetter = S.LookupMethodInObjectType(AtIndexSetterSelector, ResultType, 
                                              true /*instance*/);
-  
-  bool receiverIdType = (BaseT->isObjCIdType() ||
-                         BaseT->isObjCQualifiedIdType());
 
   if (!AtIndexSetter && S.getLangOpts().DebuggerObjCLiteral) {
     TypeSourceInfo *ReturnTInfo = nullptr;
@@ -1321,7 +1316,7 @@ bool ObjCSubscriptOpBuilder::findAtIndex
   }
   
   if (!AtIndexSetter) {
-    if (!receiverIdType) {
+    if (!BaseT->isObjCIdType()) {
       S.Diag(BaseExpr->getExprLoc(), 
              diag::err_objc_subscript_method_not_found)
       << BaseExpr->getType() << 1 << arrayRef;

Modified: cfe/trunk/test/SemaObjC/objc-container-subscripting-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/objc-container-subscripting-1.m?rev=307642&r1=307641&r2=307642&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/objc-container-subscripting-1.m (original)
+++ cfe/trunk/test/SemaObjC/objc-container-subscripting-1.m Tue Jul 11 03:18:35 2017
@@ -16,8 +16,7 @@ id oldObject = array[10]; // expected-wa
 array[10] = 0; // expected-warning {{instance method '-setObject:atIndexedSubscript:' not found (return type defaults to 'id')}}
 
 id<P> p_array;
-oldObject = p_array[10]; // expected-warning {{instance method '-objectAtIndexedSubscript:' not found (return type defaults to 'id')}}
+oldObject = p_array[10]; // expected-error {{expected method to read array element not found on object of type 'id<P>'}}
 
-p_array[10] = 0; // expected-warning {{instance method '-setObject:atIndexedSubscript:' not found (return type defaults to 'id')}}
+p_array[10] = 0; // expected-error {{expected method to write array element not found on object of type 'id<P>'}}
 }
-

Modified: cfe/trunk/test/SemaObjC/objc-container-subscripting-2.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/objc-container-subscripting-2.m?rev=307642&r1=307641&r2=307642&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/objc-container-subscripting-2.m (original)
+++ cfe/trunk/test/SemaObjC/objc-container-subscripting-2.m Tue Jul 11 03:18:35 2017
@@ -28,3 +28,22 @@ void test_unused() {
   dict[array]; // expected-warning {{container access result unused - container access should not be used for side effects}}
 }
 
+void testQualifiedId(id<P> qualifiedId) {
+  id object = qualifiedId[10];   // expected-error {{expected method to read array element not found on object of type 'id<P>'}}
+  qualifiedId[10] = qualifiedId; // expected-error {{expected method to write array element not found on object of type 'id<P>'}}
+}
+
+void testUnqualifiedId(id unqualId) {
+  id object = unqualId[10];
+  unqualId[10] = object;
+}
+
+ at protocol Subscriptable
+- (id)objectAtIndexedSubscript:(size_t)index;
+- (void)setObject:(id)object atIndexedSubscript:(size_t)index;
+ at end
+
+void testValidQualifiedId(id<Subscriptable> qualifiedId) {
+  id object = qualifiedId[10];
+  qualifiedId[10] = object;
+}




More information about the cfe-commits mailing list