[cfe-dev] Comparison of pointers between Objc subclasses

Eli Friedman eli.friedman at gmail.com
Tue Jul 1 15:40:11 PDT 2008


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



More information about the cfe-dev mailing list