[LLVMbugs] [Bug 11292] Objective-C - incompatible pointer type assignment not detected when used with init method

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Tue Nov 8 11:12:31 PST 2011


Douglas Gregor <dgregor at apple.com> changed:

           What    |Removed                     |Added
             Status|REOPENED                    |RESOLVED
                 CC|                            |dgregor at apple.com
         Resolution|                            |INVALID

--- Comment #4 from Douglas Gregor <dgregor at apple.com> 2011-11-08 13:12:31 CST ---
As noted by Eli, "-init" methods get a related result type, as documented here:


Clang is behaving correctly.

(In reply to comment #2)
> Expected Behavior:
> ===========
> The compiler should enforce that all init methods to have the return type as
> "id". (see documentation) otherwise should throw a warning / error.
> Enforcing this would mean that would mean that the init methods would no longer
> return an parent class pointer type.

Enforcing this would break lots of existing code. It's not feasible.

> Actual Behavior:
> ============
> Presently the compiler allows init methods to have the return type of the class
> (class pointer type) that implements it.

... which is perfectly fine, so long as the "-init" methods follow the
convention of returning 'self'. Related result type works on existing

> Documentation:
> ===========
> The Objective‐C Programming Language (Page 51)
> "The return type of an initializer method should be id.
> The return type should be id because id gives an indication that the class is
> purposely not considered—that the class is unspecified and subject to change,
> depending on context of invocation. For example, NSString provides the method
> initWithFormat:. When sent to an instance of NSMutableString (a subclass of
> NSString), however, the message returns an instance of NSMutableString, not
> NSString."
> Present Scenario:
> ===========
> Assignment of a parent class pointer to a derived class pointer variable is
> incorrect
> This is currently being allowed in clang when the init method returns a parent
> class pointer (as shown in example).
> The problem would be solved if the compiler should enforce that all init
> methods to have the return type as id. (see documentation) otherwise should
> throw a warning / error.

Note that there is nothing "wrong" in the code you've pasted. [B alloc] ensures
that we get an object of type "B". "initSomeLogic" returns "self", so when it
gets a "B" object it returns a "B" object. That the initSomeLogic method is
declared as A* doesn't matter here, due to related result type inference
(linked above).

Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.

More information about the llvm-bugs mailing list