[cfe-dev] Comparison of pointers between Objc subclasses

Jean-Daniel Dupas devlists at shadowlab.org
Tue Jul 1 16:06:41 PDT 2008

Le 2 juil. 08 à 00:40, Eli Friedman a écrit :

> On Tue, Jul 1, 2008 at 2:33 PM, Thomas Clement <thomascl at free.fr>  
> wrote:
>> I noticed that clang (-fsyntax-only) produces the following warning:
>> MyClass.m:15:11: warning: comparison of distinct pointer types
>> ('NSMutableArray *' and 'NSArray *')
>> if (array != newArray)
>>    ~~~~~ ^  ~~~~~~~~
>> Since NSMutableArray is a subclass of NSArray, the warning doesn't
>> seem really appropriate.
> Probably caused by the fact that ASTContext::typesAreCompatible isn't
> commutative for ObjC types; clang currently considers (Base, Derived)
> to be compatible, while (Derived, Base) isn't.  I actually have a
> patch that will fix that, but I'm not sure about some of the
> expectations for type compatibility and ObjC.
> Considering two ObjC pointers compatible in the C99 sense makes a lot
> of things legal which probably shouldn't be legal: for example,
> redeclaring a pointer to a base type as a pointer to a derived type is
> legal (illegal in C++), assigning a base type pointer to a derived
> type pointer is legal (requires a static_cast in C++), and assigning a
> pointer to a derived type pointer to a pointer to a base type pointer
> is legal (requires a reinterpret_cast in C++).  Because of situations
> like these, C++ doesn't have type compatibility in the same sense;
> legal combinations of operands are defined on a case-by-case basis.
> What exactly are the rules supposed to be for ObjC?  (Or, if the rules
> aren't written down anywhere, what does gcc do?)
> -Eli

At least for GCC:

   id generic;
   NSObject *base = nil;
   NSArray *array = nil;
   NSMutableArray *subArray = nil;

   base = array; // OK
   array = base; // Warning: assignment from distinct Obj-C Type

   array = subArray; // OK
   subArray = array; // Warning:  assignment from distinct Obj-C Type

   generic = array; // OK
   array = generic; // OK

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2427 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20080702/10a4b56b/attachment.bin>

More information about the cfe-dev mailing list