zequanwu created this revision. zequanwu added reviewers: rsmith, hans. zequanwu requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
llvm.org/PR52014 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D110780 Files: clang/lib/Sema/SemaInit.cpp clang/test/SemaCXX/pointer-forward-declared-class-conversion.cpp Index: clang/test/SemaCXX/pointer-forward-declared-class-conversion.cpp =================================================================== --- clang/test/SemaCXX/pointer-forward-declared-class-conversion.cpp +++ clang/test/SemaCXX/pointer-forward-declared-class-conversion.cpp @@ -9,3 +9,7 @@ template <class C> class B2; B2<int> *b2; A2<int> *a2 = b2; // expected-error{{cannot initialize a variable of type 'A2<int> *' with an lvalue of type 'B2<int> *'}} + +typedef struct S s; +const s *f(); +s *g() { return f(); } // expected-error{{cannot initialize return object of type 's *' (aka 'S *') with an rvalue of type 'const s *' (aka 'const S *')}} Index: clang/lib/Sema/SemaInit.cpp =================================================================== --- clang/lib/Sema/SemaInit.cpp +++ clang/lib/Sema/SemaInit.cpp @@ -8914,12 +8914,16 @@ S.EmitRelatedResultTypeNoteForReturn(destType); } QualType fromType = op->getType(); - auto *fromDecl = fromType.getTypePtr()->getPointeeCXXRecordDecl(); - auto *destDecl = destType.getTypePtr()->getPointeeCXXRecordDecl(); + QualType fromPointeeType = fromType.getCanonicalType()->getPointeeType(); + QualType destPointeeType = destType.getCanonicalType()->getPointeeType(); + auto *fromDecl = fromType->getPointeeCXXRecordDecl(); + auto *destDecl = destType->getPointeeCXXRecordDecl(); if (fromDecl && destDecl && fromDecl->getDeclKind() == Decl::CXXRecord && destDecl->getDeclKind() == Decl::CXXRecord && !fromDecl->isInvalidDecl() && !destDecl->isInvalidDecl() && - !fromDecl->hasDefinition()) + !fromDecl->hasDefinition() && + destPointeeType.getQualifiers().compatiblyIncludes( + fromPointeeType.getQualifiers())) S.Diag(fromDecl->getLocation(), diag::note_forward_class_conversion) << S.getASTContext().getTagDeclType(fromDecl) << S.getASTContext().getTagDeclType(destDecl);
Index: clang/test/SemaCXX/pointer-forward-declared-class-conversion.cpp =================================================================== --- clang/test/SemaCXX/pointer-forward-declared-class-conversion.cpp +++ clang/test/SemaCXX/pointer-forward-declared-class-conversion.cpp @@ -9,3 +9,7 @@ template <class C> class B2; B2<int> *b2; A2<int> *a2 = b2; // expected-error{{cannot initialize a variable of type 'A2<int> *' with an lvalue of type 'B2<int> *'}} + +typedef struct S s; +const s *f(); +s *g() { return f(); } // expected-error{{cannot initialize return object of type 's *' (aka 'S *') with an rvalue of type 'const s *' (aka 'const S *')}} Index: clang/lib/Sema/SemaInit.cpp =================================================================== --- clang/lib/Sema/SemaInit.cpp +++ clang/lib/Sema/SemaInit.cpp @@ -8914,12 +8914,16 @@ S.EmitRelatedResultTypeNoteForReturn(destType); } QualType fromType = op->getType(); - auto *fromDecl = fromType.getTypePtr()->getPointeeCXXRecordDecl(); - auto *destDecl = destType.getTypePtr()->getPointeeCXXRecordDecl(); + QualType fromPointeeType = fromType.getCanonicalType()->getPointeeType(); + QualType destPointeeType = destType.getCanonicalType()->getPointeeType(); + auto *fromDecl = fromType->getPointeeCXXRecordDecl(); + auto *destDecl = destType->getPointeeCXXRecordDecl(); if (fromDecl && destDecl && fromDecl->getDeclKind() == Decl::CXXRecord && destDecl->getDeclKind() == Decl::CXXRecord && !fromDecl->isInvalidDecl() && !destDecl->isInvalidDecl() && - !fromDecl->hasDefinition()) + !fromDecl->hasDefinition() && + destPointeeType.getQualifiers().compatiblyIncludes( + fromPointeeType.getQualifiers())) S.Diag(fromDecl->getLocation(), diag::note_forward_class_conversion) << S.getASTContext().getTagDeclType(fromDecl) << S.getASTContext().getTagDeclType(destDecl);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits