[PATCH] D67983: [ObjC] Diagnose implicit type coercion from ObjC 'Class' to object pointer types.

James Y Knight via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 28 08:23:00 PDT 2019


jyknight added a comment.

In D67983#1723376 <https://reviews.llvm.org/D67983#1723376>, @thakis wrote:

> After this, Class can no longer be used as a key type in an Obj-C dictionary literal. Is that intentional?


Such code was already an on by default incompatible-pointer-types warning in ObjC mode. That it worked in ObjC++ was accidental.

For example:

foo.m:

  #import <Foundation/Foundation.h>
  
  int main() {
    NSDictionary* d = @{[NSArray class] : @"bar"};
  }

Compiling:

  $ clang -framework Foundation -o foo foo.m
  foo.m:4:23: warning: incompatible pointer types passing 'Class' to parameter of type 'id<NSCopying> _Nonnull' [-Wincompatible-pointer-types]
    NSDictionary* d = @{[NSArray class] : @"bar"};
                        ^~~~~~~~~~~~~~~
  1 warning generated.

While the default metaclass does in fact implement the one method NSCopying declares, it's not possible within the language to declare that the Class -- itself, as an instance -- implements the instance methods from the NSCopying protocol.

You can fix the code just by doing `@{(id)clz : val}`, since id is freely castable to anything.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D67983/new/

https://reviews.llvm.org/D67983





More information about the cfe-commits mailing list