[PATCH] D85390: [Clang] Add note for bad conversion error when expression is of forward-declared type

Zequan Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 6 14:24:43 PDT 2020


zequanwu added inline comments.


================
Comment at: clang/lib/Sema/SemaInit.cpp:8710
   }
+  QualType fromType = op->getType();
+  auto *fromDecl = fromType.getTypePtr()->getPointeeCXXRecordDecl();
----------------
hans wrote:
> Can the reverse situation happen, where it's destType that's forward declared?
If we only consider normal class without template, I think it's not necessary.
Here is an example of forward-declared destType.
```
class B{};
class A;
B *b;
A *a= b;
```
Even if A is defined as `class A: public B{};`, `A *a = b` is still not valid, initializing pointer to derived class with pointer to base class.


================
Comment at: clang/lib/Sema/SemaInit.cpp:8713
+  if (fromDecl && !fromDecl->hasDefinition() && !fromDecl->isInvalidDecl() &&
+      fromDecl->getDeclKind() == Decl::CXXRecord)
+    S.Diag(fromDecl->getLocation(), diag::note_forward_declaration)
----------------
hans wrote:
> Is the getDeclKind() still necessary even though you're doing getPointeeCXXRecordDecl() above and fromDecl is os type CXXRecordDecl*?
Because I was thinking about excluding class template. getPointeeCXXRecordDecl() might return a `ClassTemplateSpecializationDecl` or `ClassTemplatePartialSpecializationDecl`.
For following example, it triggers the same error. Should the new note also be emitted for this case? 
```
template<class C> class A{};
A<int> *a1;
A<char> *a2 = a1;
```
And the following code compiles:
```
template<class C> class A{};
template<> class A<char>{};
template<> class A<int> : public A<char>{};
A<int> *a1;
A<char> *a2 = a1;
```
This seems like the same error as normal class, could resolved by inheritance.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85390



More information about the cfe-commits mailing list