Skip to content

Commit 1543f98

Browse files
committed
[FOLD] add more tests, diagnose ambiguous member function specializations
1 parent 4e8a866 commit 1543f98

File tree

4 files changed

+122
-33
lines changed

4 files changed

+122
-33
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5437,6 +5437,11 @@ def note_function_template_spec_matched : Note<
54375437
def err_function_template_partial_spec : Error<
54385438
"function template partial specialization is not allowed">;
54395439

5440+
def err_function_member_spec_ambiguous : Error<
5441+
"ambiguous member function specialization of %q0">;
5442+
def note_function_member_spec_matched : Note<
5443+
"member function specialization matches %0">;
5444+
54405445
// C++ Template Instantiation
54415446
def err_template_recursion_depth_exceeded : Error<
54425447
"recursive template instantiation exceeded maximum depth of %0">,

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9739,6 +9739,9 @@ class Sema final : public SemaBase {
97399739
const PartialDiagnostic &CandidateDiag,
97409740
bool Complain = true, QualType TargetType = QualType());
97419741

9742+
FunctionDecl *getMoreConstrainedFunction(FunctionDecl *FD1,
9743+
FunctionDecl *FD2);
9744+
97429745
///@}
97439746

97449747
//

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10346,23 +10346,27 @@ bool Sema::CheckFunctionTemplateSpecialization(
1034610346
return false;
1034710347
}
1034810348

10349-
static bool IsMoreConstrainedFunction(Sema &S, FunctionDecl *FD1,
10350-
FunctionDecl *FD2) {
10349+
FunctionDecl *Sema::getMoreConstrainedFunction(FunctionDecl *FD1,
10350+
FunctionDecl *FD2) {
10351+
assert(!FD1->getDescribedTemplate() && !FD2->getDescribedTemplate() &&
10352+
"not for function templates");
10353+
FunctionDecl *F1 = FD1;
1035110354
if (FunctionDecl *MF = FD1->getInstantiatedFromMemberFunction())
10352-
FD1 = MF;
10355+
F1 = MF;
10356+
FunctionDecl *F2 = FD2;
1035310357
if (FunctionDecl *MF = FD2->getInstantiatedFromMemberFunction())
10354-
FD2 = MF;
10358+
F2 = MF;
1035510359
llvm::SmallVector<const Expr *, 3> AC1, AC2;
10356-
FD1->getAssociatedConstraints(AC1);
10357-
FD2->getAssociatedConstraints(AC2);
10360+
F1->getAssociatedConstraints(AC1);
10361+
F2->getAssociatedConstraints(AC2);
1035810362
bool AtLeastAsConstrained1, AtLeastAsConstrained2;
10359-
if (S.IsAtLeastAsConstrained(FD1, AC1, FD2, AC2, AtLeastAsConstrained1))
10360-
return false;
10361-
if (S.IsAtLeastAsConstrained(FD2, AC2, FD1, AC1, AtLeastAsConstrained2))
10362-
return false;
10363+
if (IsAtLeastAsConstrained(F1, AC1, F2, AC2, AtLeastAsConstrained1))
10364+
return nullptr;
10365+
if (IsAtLeastAsConstrained(F2, AC2, F1, AC1, AtLeastAsConstrained2))
10366+
return nullptr;
1036310367
if (AtLeastAsConstrained1 == AtLeastAsConstrained2)
10364-
return false;
10365-
return AtLeastAsConstrained1;
10368+
return nullptr;
10369+
return AtLeastAsConstrained1 ? FD1 : FD2;
1036610370
}
1036710371

1036810372
/// Perform semantic analysis for the given non-template member
@@ -10392,35 +10396,54 @@ Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
1039210396
if (Previous.empty()) {
1039310397
// Nowhere to look anyway.
1039410398
} else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
10399+
SmallVector<FunctionDecl *> Candidates;
10400+
bool Ambiguous = false;
1039510401
for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
1039610402
I != E; ++I) {
10397-
NamedDecl *D = (*I)->getUnderlyingDecl();
10398-
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
10399-
QualType Adjusted = Function->getType();
10400-
if (!hasExplicitCallingConv(Adjusted))
10401-
Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType());
10402-
if (!Context.hasSameType(Adjusted, Method->getType()))
10403-
continue;
10404-
if (Method->getTrailingRequiresClause()) {
10405-
ConstraintSatisfaction Satisfaction;
10406-
if (CheckFunctionConstraints(Method, Satisfaction,
10407-
/*UsageLoc=*/Member->getLocation(),
10408-
/*ForOverloadResolution=*/true) ||
10409-
!Satisfaction.IsSatisfied)
10410-
continue;
10411-
if (Instantiation &&
10412-
!IsMoreConstrainedFunction(*this, Method,
10413-
cast<CXXMethodDecl>(Instantiation)))
10414-
continue;
10415-
}
10416-
// This doesn't handle deduced return types, but both function
10417-
// declarations should be undeduced at this point.
10403+
CXXMethodDecl *Method =
10404+
dyn_cast<CXXMethodDecl>((*I)->getUnderlyingDecl());
10405+
if (!Method)
10406+
continue;
10407+
QualType Adjusted = Function->getType();
10408+
if (!hasExplicitCallingConv(Adjusted))
10409+
Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType());
10410+
// This doesn't handle deduced return types, but both function
10411+
// declarations should be undeduced at this point.
10412+
if (!Context.hasSameType(Adjusted, Function->getType()))
10413+
continue;
10414+
// FIXME: What if neither function is more constrained than the other?
10415+
if (ConstraintSatisfaction Satisfaction;
10416+
Method->getTrailingRequiresClause() &&
10417+
(CheckFunctionConstraints(Method, Satisfaction,
10418+
/*UsageLoc=*/Member->getLocation(),
10419+
/*ForOverloadResolution=*/true) ||
10420+
!Satisfaction.IsSatisfied))
10421+
continue;
10422+
Candidates.push_back(Method);
10423+
FunctionDecl *MoreConstrained =
10424+
Instantiation ? getMoreConstrainedFunction(
10425+
Method, cast<FunctionDecl>(Instantiation))
10426+
: Method;
10427+
if (!MoreConstrained) {
10428+
Ambiguous = true;
10429+
continue;
10430+
}
10431+
if (MoreConstrained == Method) {
10432+
Ambiguous = false;
1041810433
FoundInstantiation = *I;
1041910434
Instantiation = Method;
1042010435
InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
1042110436
MSInfo = Method->getMemberSpecializationInfo();
1042210437
}
1042310438
}
10439+
if (Ambiguous) {
10440+
Diag(Member->getLocation(), diag::err_function_member_spec_ambiguous)
10441+
<< Member;
10442+
for (FunctionDecl *Candidate : Candidates)
10443+
Diag(Candidate->getLocation(), diag::note_function_member_spec_matched)
10444+
<< Candidate;
10445+
return true;
10446+
}
1042410447
} else if (isa<VarDecl>(Member)) {
1042510448
VarDecl *PrevVar;
1042610449
if (Previous.isSingleResult() &&
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// RUN: %clang_cc1 -std=c++20 -verify %s
2+
3+
template<int I>
4+
concept C = I >= 4;
5+
6+
template<int I>
7+
concept D = I < 8;
8+
9+
template<int I>
10+
struct A {
11+
constexpr static int f() { return 0; }
12+
constexpr static int f() requires C<I> && D<I> { return 1; }
13+
constexpr static int f() requires C<I> { return 2; }
14+
15+
constexpr static int g() requires C<I> { return 0; } // expected-note {{member function specialization matches 'g'}}
16+
constexpr static int g() requires D<I> { return 1; } // expected-note {{member function specialization matches 'g'}}
17+
18+
constexpr static int h() requires C<I> { return 0; } // expected-note {{member declaration nearly matches}}
19+
};
20+
21+
template<>
22+
constexpr int A<2>::f() { return 3; }
23+
24+
template<>
25+
constexpr int A<4>::f() { return 4; }
26+
27+
template<>
28+
constexpr int A<8>::f() { return 5; }
29+
30+
static_assert(A<3>::f() == 0);
31+
static_assert(A<5>::f() == 1);
32+
static_assert(A<9>::f() == 2);
33+
static_assert(A<2>::f() == 3);
34+
static_assert(A<4>::f() == 4);
35+
static_assert(A<8>::f() == 5);
36+
37+
template<>
38+
constexpr int A<0>::g() { return 2; }
39+
40+
template<>
41+
constexpr int A<8>::g() { return 3; }
42+
43+
template<>
44+
constexpr int A<6>::g() { return 4; } // expected-error {{ambiguous member function specialization of 'A<6>::g'}}
45+
46+
static_assert(A<9>::g() == 0);
47+
static_assert(A<1>::g() == 1);
48+
static_assert(A<0>::g() == 2);
49+
static_assert(A<8>::g() == 3);
50+
51+
template<>
52+
constexpr int A<4>::h() { return 1; }
53+
54+
template<>
55+
constexpr int A<0>::h() { return 2; } // expected-error {{out-of-line definition of 'h' does not match any declaration in 'A<0>'}}
56+
57+
static_assert(A<5>::h() == 0);
58+
static_assert(A<4>::h() == 1);

0 commit comments

Comments
 (0)