Skip to content

Commit 02b6ed0

Browse files
[Clang] Added explanation why is_constructible evaluated to false. (#143309)
Added explanation why a is constructible evaluated to false. Also fixed problem with ```ExtractTypeTraitFromExpression```. In case ```std::is_xxx_v<>``` with variadic pack it tries to get template argument, but fails in expression ```Arg.getAsType()``` due to ```Arg.getKind() == TemplateArgument::ArgKind::Pack```, but not ```TemplateArgument::ArgKind::Type```.
1 parent cd3d234 commit 02b6ed0

File tree

6 files changed

+219
-10
lines changed

6 files changed

+219
-10
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,8 @@ def note_unsatisfied_trait
17671767
: Note<"%0 is not %enum_select<TraitName>{"
17681768
"%TriviallyRelocatable{trivially relocatable}|"
17691769
"%Replaceable{replaceable}|"
1770-
"%TriviallyCopyable{trivially copyable}"
1770+
"%TriviallyCopyable{trivially copyable}|"
1771+
"%Constructible{constructible with provided types}"
17711772
"}1">;
17721773

17731774
def note_unsatisfied_trait_reason
@@ -1797,7 +1798,10 @@ def note_unsatisfied_trait_reason
17971798
"%DeletedAssign{has a deleted %select{copy|move}1 "
17981799
"assignment operator}|"
17991800
"%UnionWithUserDeclaredSMF{is a union with a user-declared "
1800-
"%sub{select_special_member_kind}1}"
1801+
"%sub{select_special_member_kind}1}|"
1802+
"%FunctionType{is a function type}|"
1803+
"%CVVoidType{is a cv void type}|"
1804+
"%IncompleteArrayType{is an incomplete array type}"
18011805
"}0">;
18021806

18031807
def warn_consteval_if_always_true : Warning<

clang/lib/Sema/SemaTypeTraits.cpp

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "clang/AST/DeclCXX.h"
14+
#include "clang/AST/TemplateBase.h"
1415
#include "clang/AST/Type.h"
1516
#include "clang/Basic/DiagnosticParse.h"
1617
#include "clang/Basic/DiagnosticSema.h"
@@ -1947,6 +1948,7 @@ static std::optional<TypeTrait> StdNameToTypeTrait(StringRef Name) {
19471948
TypeTrait::UTT_IsCppTriviallyRelocatable)
19481949
.Case("is_replaceable", TypeTrait::UTT_IsReplaceable)
19491950
.Case("is_trivially_copyable", TypeTrait::UTT_IsTriviallyCopyable)
1951+
.Case("is_constructible", TypeTrait::TT_IsConstructible)
19501952
.Default(std::nullopt);
19511953
}
19521954

@@ -1983,8 +1985,16 @@ static ExtractedTypeTraitInfo ExtractTypeTraitFromExpression(const Expr *E) {
19831985
Trait = StdNameToTypeTrait(Name);
19841986
if (!Trait)
19851987
return std::nullopt;
1986-
for (const auto &Arg : VD->getTemplateArgs().asArray())
1987-
Args.push_back(Arg.getAsType());
1988+
for (const auto &Arg : VD->getTemplateArgs().asArray()) {
1989+
if (Arg.getKind() == TemplateArgument::ArgKind::Pack) {
1990+
for (const auto &InnerArg : Arg.pack_elements())
1991+
Args.push_back(InnerArg.getAsType());
1992+
} else if (Arg.getKind() == TemplateArgument::ArgKind::Type) {
1993+
Args.push_back(Arg.getAsType());
1994+
} else {
1995+
llvm_unreachable("Unexpected kind");
1996+
}
1997+
}
19881998
return {{Trait.value(), std::move(Args)}};
19891999
}
19902000

@@ -2257,6 +2267,60 @@ static void DiagnoseNonTriviallyCopyableReason(Sema &SemaRef,
22572267
}
22582268
}
22592269

2270+
static void DiagnoseNonConstructibleReason(
2271+
Sema &SemaRef, SourceLocation Loc,
2272+
const llvm::SmallVector<clang::QualType, 1> &Ts) {
2273+
if (Ts.empty()) {
2274+
return;
2275+
}
2276+
2277+
bool ContainsVoid = false;
2278+
for (const QualType &ArgTy : Ts) {
2279+
ContainsVoid |= ArgTy->isVoidType();
2280+
}
2281+
2282+
if (ContainsVoid)
2283+
SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason)
2284+
<< diag::TraitNotSatisfiedReason::CVVoidType;
2285+
2286+
QualType T = Ts[0];
2287+
if (T->isFunctionType())
2288+
SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason)
2289+
<< diag::TraitNotSatisfiedReason::FunctionType;
2290+
2291+
if (T->isIncompleteArrayType())
2292+
SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason)
2293+
<< diag::TraitNotSatisfiedReason::IncompleteArrayType;
2294+
2295+
const CXXRecordDecl *D = T->getAsCXXRecordDecl();
2296+
if (!D || D->isInvalidDecl() || !D->hasDefinition())
2297+
return;
2298+
2299+
llvm::BumpPtrAllocator OpaqueExprAllocator;
2300+
SmallVector<Expr *, 2> ArgExprs;
2301+
ArgExprs.reserve(Ts.size() - 1);
2302+
for (unsigned I = 1, N = Ts.size(); I != N; ++I) {
2303+
QualType ArgTy = Ts[I];
2304+
if (ArgTy->isObjectType() || ArgTy->isFunctionType())
2305+
ArgTy = SemaRef.Context.getRValueReferenceType(ArgTy);
2306+
ArgExprs.push_back(
2307+
new (OpaqueExprAllocator.Allocate<OpaqueValueExpr>())
2308+
OpaqueValueExpr(Loc, ArgTy.getNonLValueExprType(SemaRef.Context),
2309+
Expr::getValueKindForType(ArgTy)));
2310+
}
2311+
2312+
EnterExpressionEvaluationContext Unevaluated(
2313+
SemaRef, Sema::ExpressionEvaluationContext::Unevaluated);
2314+
Sema::ContextRAII TUContext(SemaRef,
2315+
SemaRef.Context.getTranslationUnitDecl());
2316+
InitializedEntity To(InitializedEntity::InitializeTemporary(T));
2317+
InitializationKind InitKind(InitializationKind::CreateDirect(Loc, Loc, Loc));
2318+
InitializationSequence Init(SemaRef, To, InitKind, ArgExprs);
2319+
2320+
Init.Diagnose(SemaRef, To, InitKind, ArgExprs);
2321+
SemaRef.Diag(D->getLocation(), diag::note_defined_here) << D;
2322+
}
2323+
22602324
static void DiagnoseNonTriviallyCopyableReason(Sema &SemaRef,
22612325
SourceLocation Loc, QualType T) {
22622326
SemaRef.Diag(Loc, diag::note_unsatisfied_trait)
@@ -2296,6 +2360,9 @@ void Sema::DiagnoseTypeTraitDetails(const Expr *E) {
22962360
case UTT_IsTriviallyCopyable:
22972361
DiagnoseNonTriviallyCopyableReason(*this, E->getBeginLoc(), Args[0]);
22982362
break;
2363+
case TT_IsConstructible:
2364+
DiagnoseNonConstructibleReason(*this, E->getBeginLoc(), Args);
2365+
break;
22992366
default:
23002367
break;
23012368
}

clang/test/CXX/drs/cwg18xx.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,12 @@ struct A {
564564
namespace ex2 {
565565
#if __cplusplus >= 201103L
566566
struct Bar {
567-
struct Baz {
567+
struct Baz { // #cwg1890-Baz
568568
int a = 0;
569569
};
570570
static_assert(__is_constructible(Baz), "");
571571
// since-cxx11-error@-1 {{static assertion failed due to requirement '__is_constructible(cwg1890::ex2::Bar::Baz)'}}
572+
// since-cxx11-note@#cwg1890-Baz {{'Baz' defined here}}
572573
};
573574
#endif
574575
} // namespace ex2

clang/test/SemaCXX/overload-resolution-deferred-templates.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,30 @@ struct ImplicitlyCopyable {
8080
static_assert(__is_constructible(ImplicitlyCopyable, const ImplicitlyCopyable&));
8181

8282

83-
struct Movable {
83+
struct Movable { // #Movable
8484
template <typename T>
8585
requires __is_constructible(Movable, T) // #err-self-constraint-1
86-
explicit Movable(T op) noexcept; // #1
87-
Movable(Movable&&) noexcept = default; // #2
86+
explicit Movable(T op) noexcept; // #Movable1
87+
Movable(Movable&&) noexcept = default; // #Movable2
8888
};
8989
static_assert(__is_constructible(Movable, Movable&&));
9090
static_assert(__is_constructible(Movable, const Movable&));
91-
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(Movable, const Movable &)'}}
91+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(Movable, const Movable &)'}} \
92+
// expected-error@-1 {{call to implicitly-deleted copy constructor of 'Movable'}} \
93+
// expected-note@#Movable {{'Movable' defined here}} \
94+
// expected-note@#Movable {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const Movable' for 1st argument}} \
95+
// expected-note@#Movable2 {{copy constructor is implicitly deleted because 'Movable' has a user-declared move constructor}} \
96+
// expected-note@#Movable2 {{candidate constructor not viable: no known conversion from 'int' to 'Movable' for 1st argument}} \
97+
// expected-note@#Movable1 {{candidate template ignored: constraints not satisfied [with T = int]}}
98+
9299

93100
static_assert(__is_constructible(Movable, int));
94-
// expected-error@-1{{static assertion failed due to requirement '__is_constructible(Movable, int)'}} \
101+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(Movable, int)'}} \
102+
// expected-error@-1 {{no matching constructor for initialization of 'Movable'}} \
95103
// expected-note@-1 2{{}}
96104
// expected-error@#err-self-constraint-1{{satisfaction of constraint '__is_constructible(Movable, T)' depends on itself}}
97105
// expected-note@#err-self-constraint-1 4{{}}
106+
// expected-note@#Movable {{'Movable' defined here}}
98107

99108
template <typename T>
100109
struct Members {

clang/test/SemaCXX/type-traits-unsatisfied-diags-std.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ struct is_trivially_copyable {
2020

2121
template <typename T>
2222
constexpr bool is_trivially_copyable_v = __is_trivially_copyable(T);
23+
24+
template <typename... Args>
25+
struct is_constructible {
26+
static constexpr bool value = __is_constructible(Args...);
27+
};
28+
29+
template <typename... Args>
30+
constexpr bool is_constructible_v = __is_constructible(Args...);
2331
#endif
2432

2533
#ifdef STD2
@@ -44,6 +52,17 @@ using is_trivially_copyable = __details_is_trivially_copyable<T>;
4452

4553
template <typename T>
4654
constexpr bool is_trivially_copyable_v = __is_trivially_copyable(T);
55+
56+
template <typename... Args>
57+
struct __details_is_constructible{
58+
static constexpr bool value = __is_constructible(Args...);
59+
};
60+
61+
template <typename... Args>
62+
using is_constructible = __details_is_constructible<Args...>;
63+
64+
template <typename... Args>
65+
constexpr bool is_constructible_v = __is_constructible(Args...);
4766
#endif
4867

4968

@@ -73,6 +92,15 @@ using is_trivially_copyable = __details_is_trivially_copyable<T>;
7392

7493
template <typename T>
7594
constexpr bool is_trivially_copyable_v = is_trivially_copyable<T>::value;
95+
96+
template <typename... Args>
97+
struct __details_is_constructible : bool_constant<__is_constructible(Args...)> {};
98+
99+
template <typename... Args>
100+
using is_constructible = __details_is_constructible<Args...>;
101+
102+
template <typename... Args>
103+
constexpr bool is_constructible_v = is_constructible<Args...>::value;
76104
#endif
77105

78106
}
@@ -100,6 +128,15 @@ static_assert(std::is_trivially_copyable_v<int&>);
100128
// expected-note@-1 {{because it is a reference type}}
101129

102130

131+
static_assert(std::is_constructible<int, int>::value);
132+
133+
static_assert(std::is_constructible<void>::value);
134+
// expected-error-re@-1 {{static assertion failed due to requirement 'std::{{.*}}is_constructible<void>::value'}} \
135+
// expected-note@-1 {{because it is a cv void type}}
136+
static_assert(std::is_constructible_v<void>);
137+
// expected-error@-1 {{static assertion failed due to requirement 'std::is_constructible_v<void>'}} \
138+
// expected-note@-1 {{because it is a cv void type}}
139+
103140
namespace test_namespace {
104141
using namespace std;
105142
static_assert(is_trivially_relocatable<int&>::value);
@@ -119,6 +156,13 @@ namespace test_namespace {
119156
// expected-error@-1 {{static assertion failed due to requirement 'is_trivially_copyable_v<int &>'}} \
120157
// expected-note@-1 {{'int &' is not trivially copyable}} \
121158
// expected-note@-1 {{because it is a reference type}}
159+
160+
static_assert(is_constructible<void>::value);
161+
// expected-error-re@-1 {{static assertion failed due to requirement '{{.*}}is_constructible<void>::value'}} \
162+
// expected-note@-1 {{because it is a cv void type}}
163+
static_assert(is_constructible_v<void>);
164+
// expected-error@-1 {{static assertion failed due to requirement 'is_constructible_v<void>'}} \
165+
// expected-note@-1 {{because it is a cv void type}}
122166
}
123167

124168

@@ -139,6 +183,15 @@ concept C2 = std::is_trivially_copyable_v<T>; // #concept4
139183

140184
template <C2 T> void g2(); // #cand4
141185

186+
template <typename... Args>
187+
requires std::is_constructible<Args...>::value void f3(); // #cand5
188+
189+
template <typename... Args>
190+
concept C3 = std::is_constructible_v<Args...>; // #concept6
191+
192+
template <C3 T> void g3(); // #cand6
193+
194+
142195
void test() {
143196
f<int&>();
144197
// expected-error@-1 {{no matching function for call to 'f'}} \
@@ -169,6 +222,19 @@ void test() {
169222
// expected-note@#concept4 {{because 'std::is_trivially_copyable_v<int &>' evaluated to false}} \
170223
// expected-note@#concept4 {{'int &' is not trivially copyable}} \
171224
// expected-note@#concept4 {{because it is a reference type}}
225+
226+
f3<void>();
227+
// expected-error@-1 {{no matching function for call to 'f3'}} \
228+
// expected-note@#cand5 {{candidate template ignored: constraints not satisfied [with Args = <void>]}} \
229+
// expected-note-re@#cand5 {{because '{{.*}}is_constructible<void>::value' evaluated to false}} \
230+
// expected-note@#cand5 {{because it is a cv void type}}
231+
232+
g3<void>();
233+
// expected-error@-1 {{no matching function for call to 'g3'}} \
234+
// expected-note@#cand6 {{candidate template ignored: constraints not satisfied [with T = void]}} \
235+
// expected-note@#cand6 {{because 'void' does not satisfy 'C3'}} \
236+
// expected-note@#concept6 {{because 'std::is_constructible_v<void>' evaluated to false}} \
237+
// expected-note@#concept6 {{because it is a cv void type}}
172238
}
173239
}
174240

clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,65 @@ static_assert(__is_trivially_copyable(S12));
488488
// expected-note@-1 {{'S12' is not trivially copyable}} \
489489
// expected-note@#tc-S12 {{'S12' defined here}}
490490
}
491+
492+
namespace constructible {
493+
494+
struct S1 { // #c-S1
495+
S1(int); // #cc-S1
496+
};
497+
static_assert(__is_constructible(S1, char*));
498+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(constructible::S1, char *)'}} \
499+
// expected-error@-1 {{no matching constructor for initialization of 'S1'}} \
500+
// expected-note@#c-S1 {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'char *' to 'const S1' for 1st argument}} \
501+
// expected-note@#c-S1 {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'char *' to 'S1' for 1st argument}} \
502+
// expected-note@#cc-S1 {{candidate constructor not viable: no known conversion from 'char *' to 'int' for 1st argument; dereference the argument with *}} \
503+
// expected-note@#c-S1 {{'S1' defined here}}
504+
505+
struct S2 { // #c-S2
506+
S2(int, float, double); // #cc-S2
507+
};
508+
static_assert(__is_constructible(S2, float));
509+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(constructible::S2, float)'}} \
510+
// expected-note@#c-S2 {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'float' to 'const S2' for 1st argument}} \
511+
// expected-note@#c-S2 {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'float' to 'S2' for 1st argument}} \
512+
// expected-error@-1 {{no matching constructor for initialization of 'S2'}} \
513+
// expected-note@#cc-S2 {{candidate constructor not viable: requires 3 arguments, but 1 was provided}} \
514+
// expected-note@#c-S2 {{'S2' defined here}}
515+
516+
static_assert(__is_constructible(S2, float, void));
517+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(constructible::S2, float, void)'}} \
518+
// expected-note@#c-S2 {{candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided}} \
519+
// expected-note@#c-S2 {{candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided}} \
520+
// expected-note@-1{{because it is a cv void type}} \
521+
// expected-error@-1 {{no matching constructor for initialization of 'S2'}} \
522+
// expected-note@#cc-S2 {{candidate constructor not viable: requires 3 arguments, but 2 were provided}} \
523+
// expected-note@#c-S2 {{'S2' defined here}}
524+
525+
static_assert(__is_constructible(int[]));
526+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(int[])'}} \
527+
// expected-note@-1 {{because it is an incomplete array type}}
528+
529+
static_assert(__is_constructible(void));
530+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(void)'}} \
531+
// expected-note@-1 {{because it is a cv void type}}
532+
533+
static_assert(__is_constructible(void, void));
534+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(void, void)'}} \
535+
// expected-note@-1 {{because it is a cv void type}}
536+
537+
static_assert(__is_constructible(const void));
538+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(const void)'}} \
539+
// expected-note@-1 {{because it is a cv void type}}
540+
541+
static_assert(__is_constructible(volatile void));
542+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(volatile void)'}} \
543+
// expected-note@-1 {{because it is a cv void type}}
544+
545+
static_assert(__is_constructible(int ()));
546+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(int ())'}} \
547+
// expected-note@-1 {{because it is a function type}}
548+
549+
static_assert(__is_constructible(void (int, float)));
550+
// expected-error@-1 {{static assertion failed due to requirement '__is_constructible(void (int, float))'}} \
551+
// expected-note@-1 {{because it is a function type}}
552+
}

0 commit comments

Comments
 (0)