ilya-biryukov created this revision. ilya-biryukov added a reviewer: ChuanqiXu. Herald added a project: All. ilya-biryukov requested review of this revision. Herald added a project: clang.
Otherwise we get invalid results for ODR checks. See changed test for an example: despite the fact that we merge the first concept, its **uses** were considered different by `Profile`, leading to redefinition errors. After this change, canonical decl for a concept can come from a different module and may not be visible. This behavior looks suspicious, but does not break any tests. We might want to add a mechanism to make the canonical concept declaration visible if we find code that relies on this invariant. Also change the order of includes in the test. Importing a moduralized header before its textual part causes the include guard macro to be exported and the corresponding `#include` becomes a no-op. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D130585 Files: clang/include/clang/AST/DeclTemplate.h clang/test/Modules/merge-concepts.cpp Index: clang/test/Modules/merge-concepts.cpp =================================================================== --- clang/test/Modules/merge-concepts.cpp +++ clang/test/Modules/merge-concepts.cpp @@ -47,7 +47,10 @@ #define SAME_AS_H template <class T, class U> -concept same_as = __is_same(T, U); +concept same_as_impl = __is_same(T, U); + +template <class T, class U> +concept same_as = same_as_impl<T, U> && same_as_impl<U, T>; #endif // SAME_AS_H @@ -55,11 +58,11 @@ #ifndef FORMAT_H #define FORMAT_H -#include "concepts.h" #include "same_as.h" +#include "concepts.h" template <class T> void foo() requires same_as<T, int> {} -#endif // FORMAT_H \ No newline at end of file +#endif // FORMAT_H Index: clang/include/clang/AST/DeclTemplate.h =================================================================== --- clang/include/clang/AST/DeclTemplate.h +++ clang/include/clang/AST/DeclTemplate.h @@ -3287,8 +3287,12 @@ return isa<TemplateTypeParmDecl>(getTemplateParameters()->getParam(0)); } - ConceptDecl *getCanonicalDecl() override { return getFirstDecl(); } - const ConceptDecl *getCanonicalDecl() const { return getFirstDecl(); } + ConceptDecl *getCanonicalDecl() override { + return cast<ConceptDecl>(getPrimaryMergedDecl(this)); + } + const ConceptDecl *getCanonicalDecl() const { + return const_cast<ConceptDecl *>(this)->getCanonicalDecl(); + } // Implement isa/cast/dyncast/etc. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Index: clang/test/Modules/merge-concepts.cpp =================================================================== --- clang/test/Modules/merge-concepts.cpp +++ clang/test/Modules/merge-concepts.cpp @@ -47,7 +47,10 @@ #define SAME_AS_H template <class T, class U> -concept same_as = __is_same(T, U); +concept same_as_impl = __is_same(T, U); + +template <class T, class U> +concept same_as = same_as_impl<T, U> && same_as_impl<U, T>; #endif // SAME_AS_H @@ -55,11 +58,11 @@ #ifndef FORMAT_H #define FORMAT_H -#include "concepts.h" #include "same_as.h" +#include "concepts.h" template <class T> void foo() requires same_as<T, int> {} -#endif // FORMAT_H \ No newline at end of file +#endif // FORMAT_H Index: clang/include/clang/AST/DeclTemplate.h =================================================================== --- clang/include/clang/AST/DeclTemplate.h +++ clang/include/clang/AST/DeclTemplate.h @@ -3287,8 +3287,12 @@ return isa<TemplateTypeParmDecl>(getTemplateParameters()->getParam(0)); } - ConceptDecl *getCanonicalDecl() override { return getFirstDecl(); } - const ConceptDecl *getCanonicalDecl() const { return getFirstDecl(); } + ConceptDecl *getCanonicalDecl() override { + return cast<ConceptDecl>(getPrimaryMergedDecl(this)); + } + const ConceptDecl *getCanonicalDecl() const { + return const_cast<ConceptDecl *>(this)->getCanonicalDecl(); + } // Implement isa/cast/dyncast/etc. static bool classof(const Decl *D) { return classofKind(D->getKind()); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits