[PATCH] D36790: [ObjC] Messages to 'self' in class methods that return 'instancetype' should use the pointer to the class as the result type of the message

Alex Lorenz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 24 17:33:45 PDT 2017


arphaman added a comment.

In https://reviews.llvm.org/D36790#843450, @arphaman wrote:

> @rjmccall Do you think that the rules for the return types in overridden methods that return `instancetype` should be strengthened first? For example, if we have the following code:
>
>   @interface Unrelated
>   - (void)method:(int)x;
>   @end
>  
>   @interface CallsSelfSuper: NSObject
>   + (void) target;
>   - (void) method:(CallsSelfSuper *)x;
>   @end
>  
>   @implementation CallsSelfSuper
>   + (void) target {
>     [[self alloc] method: 12]; // Can't assume 'method' is CallsSelfSuper's 'method' declaration unless we guarantee that OverrideAlloc's alloc actually returns `instancetype` and not id.
>   }
>   @end
>  
>   @interface OverrideAlloc: CallsSelfSuper
>   @end
>  
>   @implementation OverrideAlloc
>   + (id) alloc {
>     return [NSString alloc];
>   }
>   @end 
>
>
> We can't really make any assumptions about what `[self alloc]` will return. But in my opinion we could assume that `[self alloc]` will return a `CallsSelfSuper *` if we could ensure that methods that override methods that return `instancetype` will compile only if they return `instancetype` type as well.


I've looked at this again and it seems that Clang already has warnings in cases like this, so `instancetype` is already treated as somewhat of an inherited requirement. I now think it should be add the behavior in this patch without imposing additional restrictions.


Repository:
  rL LLVM

https://reviews.llvm.org/D36790





More information about the cfe-commits mailing list