https://github.com/pesdarshan updated https://github.com/llvm/llvm-project/pull/225487
>From 769bcd013f122f41723b1c32f45da97bb75ad76e Mon Sep 17 00:00:00 2001 From: Darshan N <[email protected]> Date: Wed, 16 Sep 2026 11:31:47 +0530 Subject: [PATCH] Added explain is trivially copy constructible --- .../clang/Basic/DiagnosticSemaKinds.td | 5 + clang/lib/Sema/SemaTypeTraits.cpp | 104 ++++++++++++++++-- .../type-traits-unsatisfied-diags-std.cpp | 79 +++++++++++++ 3 files changed, 181 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 5bf172b8f0b573..bd6ae0c1080e82 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -1858,6 +1858,7 @@ def note_unsatisfied_trait : Note<"%0 is not %enum_select<TraitName>{" "%TriviallyRelocatable{trivially relocatable}|" "%TriviallyCopyable{trivially copyable}|" + "%TriviallyCopyConstructible{trivially copy constructible}|" "%Empty{empty}|" "%StandardLayout{standard-layout}|" "%Aggregate{aggregate}|" @@ -1880,6 +1881,8 @@ def note_unsatisfied_trait_reason "%NTRField{has a non-trivially-relocatable member %1 of type %2}|" "%NTCBase{has a non-trivially-copyable base %1}|" "%NTCField{has a non-trivially-copyable member %1 of type %2}|" + "%NTCCBase{has a non-trivially-copy-constructible base %1}|" + "%NTCCField{has a non-trivially-copy-constructible member %1 of type %2}|" "%NonEmptyMember{has a non-static data member %1 of type %2}|" "%PolymorphicType{is a polymorphic type}|" "%VirtualFunction{has a virtual function %1}|" @@ -1898,6 +1901,8 @@ def note_unsatisfied_trait_reason "%InheritedCtr{has an inherited constructor}|" "%DeletedCtr{has a deleted %select{copy|move}1 " "constructor}|" + "%InaccessibleCtr{has an inaccessible %select{copy|move}1 " + "constructor}|" "%UserProvidedAssign{has a user provided %select{copy|move}1 " "assignment operator}|" "%DeletedAssign{has a deleted %select{copy|move}1 " diff --git a/clang/lib/Sema/SemaTypeTraits.cpp b/clang/lib/Sema/SemaTypeTraits.cpp index cb30ff9fd02da9..b7e0ca3a3f9b95 100644 --- a/clang/lib/Sema/SemaTypeTraits.cpp +++ b/clang/lib/Sema/SemaTypeTraits.cpp @@ -2007,14 +2007,26 @@ ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, SourceLocation KWLoc, } static std::optional<TypeTrait> StdNameToTypeTrait(StringRef Name) { + // std::is_trivially_copy_constructible<T> is implemented in terms of + // __is_trivially_constructible(T, const T &). + if (Name == "is_trivially_copy_constructible") + return TT_IsTriviallyConstructible; return llvm::StringSwitch<std::optional<TypeTrait>>(Name) #define EMIT_STD_NAME_CASES #include "clang/Basic/BuiltinTraits.inc" .Default(std::nullopt); } -using ExtractedTypeTraitInfo = - std::optional<std::pair<TypeTrait, llvm::SmallVector<QualType, 1>>>; +namespace { +struct TypeTraitInfo { + TypeTrait Trait; + llvm::SmallVector<QualType, 1> Args; + // The std:: name (without "_v") if the trait was spelled as a library trait, + // empty if it was spelled as a builtin. + StringRef StdName; +}; +using ExtractedTypeTraitInfo = std::optional<TypeTraitInfo>; +} // namespace // Recognize type traits that are builting type traits, or known standard // type traits in <type_traits>. Note that at this point we assume the @@ -2029,7 +2041,7 @@ static ExtractedTypeTraitInfo ExtractTypeTraitFromExpression(const Expr *E) { Trait = TraitExpr->getTrait(); for (const auto *Arg : TraitExpr->getArgs()) Args.push_back(Arg->getType()); - return {{Trait.value(), std::move(Args)}}; + return {{Trait.value(), std::move(Args), StringRef()}}; } const auto *Ref = dyn_cast<DeclRefExpr>(E); if (!Ref) @@ -2056,7 +2068,7 @@ static ExtractedTypeTraitInfo ExtractTypeTraitFromExpression(const Expr *E) { llvm_unreachable("Unexpected kind"); } } - return {{Trait.value(), std::move(Args)}}; + return {{Trait.value(), std::move(Args), Name}}; } // std::is_xxx<>::value @@ -2071,12 +2083,13 @@ static ExtractedTypeTraitInfo ExtractTypeTraitFromExpression(const Expr *E) { const TemplateDecl *D = Ts->getTemplateName().getAsTemplateDecl(); if (!D || !D->isInStdNamespace()) return std::nullopt; - Trait = StdNameToTypeTrait(D->getIdentifier()->getName()); + StringRef Name = D->getIdentifier()->getName(); + Trait = StdNameToTypeTrait(Name); if (!Trait) return std::nullopt; for (const auto &Arg : Ts->template_arguments()) Args.push_back(Arg.getAsType()); - return {{Trait.value(), std::move(Args)}}; + return {{Trait.value(), std::move(Args), Name}}; } return std::nullopt; } @@ -2312,6 +2325,78 @@ static void DiagnoseNonTriviallyCopyableReason(Sema &SemaRef, SemaRef.Diag(D->getLocation(), diag::note_defined_here) << D; } +static void +DiagnoseNonTriviallyCopyConstructibleReason(Sema &SemaRef, SourceLocation Loc, + const CXXRecordDecl *D) { + for (const CXXBaseSpecifier &B : D->bases()) { + if (B.isVirtual()) + SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) + << diag::TraitNotSatisfiedReason::VBase << B.getType() + << B.getSourceRange(); + else if (const CXXRecordDecl *BD = B.getType()->getAsCXXRecordDecl(); + BD && BD->hasNonTrivialCopyConstructor()) + SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) + << diag::TraitNotSatisfiedReason::NTCCBase << B.getType() + << B.getSourceRange(); + } + + for (const FieldDecl *F : D->fields()) { + // Use getBaseElementType to see through array types (e.g. NTBase arr[2]). + // Print the declared field type so the note matches the source. + QualType ElemTy = SemaRef.Context.getBaseElementType(F->getType()); + const CXXRecordDecl *FD = ElemTy->getAsCXXRecordDecl(); + if (FD && FD->hasNonTrivialCopyConstructor()) + SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) + << diag::TraitNotSatisfiedReason::NTCCField << F << F->getType() + << F->getSourceRange(); + } + + if (D->isPolymorphic()) + SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) + << diag::TraitNotSatisfiedReason::PolymorphicType; + + for (const CXXConstructorDecl *Ctor : D->ctors()) { + if (!Ctor->isCopyConstructor()) + continue; + if (Ctor->isDeleted()) + SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) + << diag::TraitNotSatisfiedReason::DeletedCtr << /*copy*/ 0 + << Ctor->getSourceRange(); + else if (Ctor->isUserProvided()) + SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) + << diag::TraitNotSatisfiedReason::UserProvidedCtr << /*copy*/ 0 + << Ctor->getSourceRange(); + else if (Ctor->getAccess() != AS_public) + SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) + << diag::TraitNotSatisfiedReason::InaccessibleCtr << /*copy*/ 0 + << Ctor->getSourceRange(); + } +} + +static void DiagnoseNonTriviallyCopyConstructibleReason(Sema &SemaRef, + SourceLocation Loc, + QualType T) { + SemaRef.Diag(Loc, diag::note_unsatisfied_trait) + << T << diag::TraitName::TriviallyCopyConstructible; + + if (T->isVoidType()) + SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) + << diag::TraitNotSatisfiedReason::CVVoidType; + else if (T->isFunctionType()) + SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) + << diag::TraitNotSatisfiedReason::FunctionType; + else if (T->isIncompleteArrayType()) + SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) + << diag::TraitNotSatisfiedReason::IncompleteArrayType; + + const CXXRecordDecl *D = T->getAsCXXRecordDecl(); + if (!D || D->isInvalidDecl() || !D->hasDefinition()) + return; + + DiagnoseNonTriviallyCopyConstructibleReason(SemaRef, Loc, D); + SemaRef.Diag(D->getLocation(), diag::note_defined_here) << D; +} + static void DiagnoseNonAssignableReason(Sema &SemaRef, SourceLocation Loc, QualType T, QualType U) { const CXXRecordDecl *D = T->getAsCXXRecordDecl(); @@ -2759,7 +2844,7 @@ void Sema::DiagnoseTypeTraitDetails(const Expr *E) { if (!TraitInfo) return; - const auto &[Trait, Args] = TraitInfo.value(); + const auto &[Trait, Args, StdName] = TraitInfo.value(); switch (Trait) { case UTT_IsCppTriviallyRelocatable: DiagnoseNonTriviallyRelocatableReason(*this, E->getBeginLoc(), Args[0]); @@ -2794,6 +2879,11 @@ void Sema::DiagnoseTypeTraitDetails(const Expr *E) { case UTT_IsAbstract: DiagnoseNonAbstractReason(*this, E->getBeginLoc(), Args[0]); break; + case TT_IsTriviallyConstructible: + if (Args.size() == 1 && StdName == "is_trivially_copy_constructible") + DiagnoseNonTriviallyCopyConstructibleReason(*this, E->getBeginLoc(), + Args[0]); + break; default: break; } diff --git a/clang/test/SemaCXX/type-traits-unsatisfied-diags-std.cpp b/clang/test/SemaCXX/type-traits-unsatisfied-diags-std.cpp index 8a9f3bc400d206..dc052adfe3b892 100644 --- a/clang/test/SemaCXX/type-traits-unsatisfied-diags-std.cpp +++ b/clang/test/SemaCXX/type-traits-unsatisfied-diags-std.cpp @@ -73,6 +73,14 @@ struct is_abstract { template <typename T> constexpr bool is_abstract_v = __is_abstract(T); +template <typename T> +struct is_trivially_copy_constructible { + static constexpr bool value = __is_trivially_constructible(T, const T&); +}; +template <typename T> +constexpr bool is_trivially_copy_constructible_v = + __is_trivially_constructible(T, const T&); + #endif #ifdef STD2 @@ -167,6 +175,16 @@ using is_abstract = __details_is_abstract<T>; template <typename T> constexpr bool is_abstract_v = __is_abstract(T); +template <typename T> +struct __details_is_trivially_copy_constructible { + static constexpr bool value = __is_trivially_constructible(T, const T&); +}; +template <typename T> +using is_trivially_copy_constructible = __details_is_trivially_copy_constructible<T>; +template <typename T> +constexpr bool is_trivially_copy_constructible_v = + __is_trivially_constructible(T, const T&); + #endif @@ -252,6 +270,16 @@ using is_abstract = __details_is_abstract<T>; template <typename T> constexpr bool is_abstract_v = is_abstract<T>::value; +template <typename T> +struct __details_is_trivially_copy_constructible + : bool_constant<__is_trivially_constructible(T, const T&)> {}; +template <typename T> +using is_trivially_copy_constructible = + __details_is_trivially_copy_constructible<T>; +template <typename T> +constexpr bool is_trivially_copy_constructible_v = + is_trivially_copy_constructible<T>::value; + #endif } @@ -277,6 +305,32 @@ static_assert(std::is_trivially_copyable_v<int&>); // expected-note@-1 {{'int &' is not trivially copyable}} \ // expected-note@-1 {{because it is a reference type}} +struct UserProvidedCopyCtor { // #tcc-std-UserProvided + UserProvidedCopyCtor(const UserProvidedCopyCtor&) {} +}; + +// std::is_trivially_copy_constructible<T>::value path +static_assert(std::is_trivially_copy_constructible<int>::value); + +static_assert(std::is_trivially_copy_constructible<UserProvidedCopyCtor>::value); +// expected-error-re@-1 {{static assertion failed due to requirement 'std::{{.*}}is_trivially_copy_constructible<{{.*}}UserProvidedCopyCtor>::value'}} \ +// expected-note@-1 {{'UserProvidedCopyCtor' is not trivially copy constructible}} \ +// expected-note@-1 {{because it has a user provided copy constructor}} \ +// expected-note@#tcc-std-UserProvided {{'UserProvidedCopyCtor' defined here}} + +// std::is_trivially_copy_constructible_v<T> path +static_assert(std::is_trivially_copy_constructible_v<int>); + +static_assert(std::is_trivially_copy_constructible_v<UserProvidedCopyCtor>); +// expected-error@-1 {{static assertion failed due to requirement 'std::is_trivially_copy_constructible_v<UserProvidedCopyCtor>'}} \ +// expected-note@-1 {{'UserProvidedCopyCtor' is not trivially copy constructible}} \ +// expected-note@-1 {{because it has a user provided copy constructor}} \ +// expected-note@#tcc-std-UserProvided {{'UserProvidedCopyCtor' defined here}} + + +// Positive: references are trivially copy constructible (must produce no diagnostic) +static_assert(std::is_trivially_copy_constructible<int&>::value); +static_assert(std::is_trivially_copy_constructible_v<int&>); // Direct tests static_assert(std::is_standard_layout<int>::value); @@ -493,6 +547,14 @@ concept C2 = std::is_trivially_copyable_v<T>; // #concept4 template <C2 T> void g2(); // #cand4 +template <typename T> +requires std::is_trivially_copy_constructible<T>::value void f6(); // #cand11 + +template <typename T> +concept C6 = std::is_trivially_copy_constructible_v<T>; // #concept11 + +template <C6 T> void g6(); // #cand12 + template <typename T, typename U> requires std::is_assignable<T, U>::value void f4(); // #cand7 @@ -549,6 +611,23 @@ void test() { // expected-note@#concept4 {{'int &' is not trivially copyable}} \ // expected-note@#concept4 {{because it is a reference type}} + f6<UserProvidedCopyCtor>(); + // expected-error@-1 {{no matching function for call to 'f6'}} \ + // expected-note@#cand11 {{candidate template ignored: constraints not satisfied [with T = UserProvidedCopyCtor]}} \ + // expected-note-re@#cand11 {{because '{{.*}}is_trivially_copy_constructible<{{.*}}UserProvidedCopyCtor>::value' evaluated to false}} \ + // expected-note@#cand11 {{'UserProvidedCopyCtor' is not trivially copy constructible}} \ + // expected-note@#cand11 {{because it has a user provided copy constructor}} \ + // expected-note@#tcc-std-UserProvided {{'UserProvidedCopyCtor' defined here}} + + g6<UserProvidedCopyCtor>(); + // expected-error@-1 {{no matching function for call to 'g6'}} \ + // expected-note@#cand12 {{candidate template ignored: constraints not satisfied [with T = UserProvidedCopyCtor]}} \ + // expected-note@#cand12 {{because 'UserProvidedCopyCtor' does not satisfy 'C6'}} \ + // expected-note@#concept11 {{because 'std::is_trivially_copy_constructible_v<UserProvidedCopyCtor>' evaluated to false}} \ + // expected-note@#concept11 {{'UserProvidedCopyCtor' is not trivially copy constructible}} \ + // expected-note@#concept11 {{because it has a user provided copy constructor}} \ + // expected-note@#tcc-std-UserProvided {{'UserProvidedCopyCtor' defined here}} + f4<int&, void>(); // expected-error@-1 {{no matching function for call to 'f4'}} \ // expected-note@#cand7 {{candidate template ignored: constraints not satisfied [with T = int &, U = void]}} \ _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
