[cfe-dev] Comparison of pointers between Objc subclasses

Ted Kremenek kremenek at apple.com
Tue Jul 1 16:22:32 PDT 2008


On Jul 1, 2008, at 3:40 PM, Eli Friedman wrote:

> 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?)

Are pointer type compatibility checks for assignments and conversions  
being handled in the same way?  I can understand why assigning a base  
type to a derived type is dangerous, but it seems to me that  
comparisons like these should be legal.  I'm only raising this point  
because the code snippet with the warning involved a comparison, not  
an assignment.




More information about the cfe-dev mailing list