]> granicus.if.org Git - clang/commit
[ObjC] Diagnose implicit type coercion from ObjC 'Class' to object
authorJames Y Knight <jyknight@google.com>
Thu, 17 Oct 2019 15:27:04 +0000 (15:27 +0000)
committerJames Y Knight <jyknight@google.com>
Thu, 17 Oct 2019 15:27:04 +0000 (15:27 +0000)
commit3b8d539899d6651504edfbaaa2ea68eb9d7aa6ac
treecebec8b73d1d3ba1a8e41dfd889d75f437c2eb12
parentb15c08d32c072399e59c388bbf6a814831a0bb92
[ObjC] Diagnose implicit type coercion from ObjC 'Class' to object
pointer types.

For example, in Objective-C mode, the initialization of 'x' in:
```
  @implementation MyType
  + (void)someClassMethod {
    MyType *x = self;
  }
  @end
```
is correctly diagnosed with an incompatible-pointer-types warning, but
in Objective-C++ mode, it is not diagnosed at all -- even though
incompatible pointer conversions generally become an error in C++.

This patch fixes that oversight, allowing implicit conversions
involving Class only to/from unqualified-id, and between qualified and
unqualified Class, where the protocols are compatible.

Note that this does change some behaviors in Objective-C, as well, as
shown by the modified tests.

Of particular note is that assignment from from 'Class<MyProtocol>' to
'id<MyProtocol>' now warns. (Despite appearances, those are not
compatible types. 'Class<MyProtocol>' is not expected to have instance
methods defined by 'MyProtocol', while 'id<MyProtocol>' is.)

Differential Revision: https://reviews.llvm.org/D67983

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@375125 91177308-0d34-0410-b5e6-96231b3b80d8
lib/AST/ASTContext.cpp
lib/Sema/SemaExpr.cpp
test/SemaObjC/comptypes-1.m
test/SemaObjCXX/class-method-self.mm
test/SemaObjCXX/comptypes-1.mm
test/SemaObjCXX/comptypes-7.mm
test/SemaObjCXX/instancetype.mm