Skip to content

Commit 342b06c

Browse files
committed
[FOLD] add more tests, diagnose ambiguous member function specializations
1 parent f47395a commit 342b06c

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
@@ -10354,23 +10354,27 @@ bool Sema::CheckFunctionTemplateSpecialization(
1035410354
return false;
1035510355
}
1035610356

10357-
static bool IsMoreConstrainedFunction(Sema &S, FunctionDecl *FD1,
10358-
FunctionDecl *FD2) {
10357+
FunctionDecl *Sema::getMoreConstrainedFunction(FunctionDecl *FD1,
10358+
FunctionDecl *FD2) {
10359+
assert(!FD1->getDescribedTemplate() && !FD2->getDescribedTemplate() &&
10360+
"not for function templates");
10361+
FunctionDecl *F1 = FD1;
1035910362
if (FunctionDecl *MF = FD1->getInstantiatedFromMemberFunction())
10360-
FD1 = MF;
10363+
F1 = MF;
10364+
FunctionDecl *F2 = FD2;
1036110365
if (FunctionDecl *MF = FD2->getInstantiatedFromMemberFunction())
10362-
FD2 = MF;
10366+
F2 = MF;
1036310367
llvm::SmallVector<const Expr *, 3> AC1, AC2;
10364-
FD1->getAssociatedConstraints(AC1);
10365-
FD2->getAssociatedConstraints(AC2);
10368+
F1->getAssociatedConstraints(AC1);
10369+
F2->getAssociatedConstraints(AC2);
1036610370
bool AtLeastAsConstrained1, AtLeastAsConstrained2;
10367-
if (S.IsAtLeastAsConstrained(FD1, AC1, FD2, AC2, AtLeastAsConstrained1))
10368-
return false;
10369-
if (S.IsAtLeastAsConstrained(FD2, AC2, FD1, AC1, AtLeastAsConstrained2))
10370-
return false;
10371+
if (IsAtLeastAsConstrained(F1, AC1, F2, AC2, AtLeastAsConstrained1))
10372+
return nullptr;
10373+
if (IsAtLeastAsConstrained(F2, AC2, F1, AC1, AtLeastAsConstrained2))
10374+
return nullptr;
1037110375
if (AtLeastAsConstrained1 == AtLeastAsConstrained2)
10372-
return false;
10373-
return AtLeastAsConstrained1;
10376+
return nullptr;
10377+
return AtLeastAsConstrained1 ? FD1 : FD2;
1037410378
}
1037510379

1037610380
/// Perform semantic analysis for the given non-template member
@@ -10400,35 +10404,54 @@ Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
1040010404
if (Previous.empty()) {
1040110405
// Nowhere to look anyway.
1040210406
} else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
10407+
SmallVector<FunctionDecl *> Candidates;
10408+
bool Ambiguous = false;
1040310409
for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
1040410410
I != E; ++I) {
10405-
NamedDecl *D = (*I)->getUnderlyingDecl();
10406-
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
10407-
QualType Adjusted = Function->getType();
10408-
if (!hasExplicitCallingConv(Adjusted))
10409-
Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType());
10410-
if (!Context.hasSameType(Adjusted, Method->getType()))
10411-
continue;
10412-
if (Method->getTrailingRequiresClause()) {
10413-
ConstraintSatisfaction Satisfaction;
10414-
if (CheckFunctionConstraints(Method, Satisfaction,
10415-
/*UsageLoc=*/Member->getLocation(),
10416-
/*ForOverloadResolution=*/true) ||
10417-
!Satisfaction.IsSatisfied)
10418-
continue;
10419-
if (Instantiation &&
10420-
!IsMoreConstrainedFunction(*this, Method,
10421-
cast<CXXMethodDecl>(Instantiation)))
10422-
continue;
10423-
}
10424-
// This doesn't handle deduced return types, but both function
10425-
// declarations should be undeduced at this point.
10411+
CXXMethodDecl *Method =
10412+
dyn_cast<CXXMethodDecl>((*I)->getUnderlyingDecl());
10413+
if (!Method)
10414+
continue;
10415+
QualType Adjusted = Function->getType();
10416+
if (!hasExplicitCallingConv(Adjusted))
10417+
Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType());
10418+
// This doesn't handle deduced return types, but both function
10419+
// declarations should be undeduced at this point.
10420+
if (!Context.hasSameType(Adjusted, Function->getType()))
10421+
continue;
10422+
// FIXME: What if neither function is more constrained than the other?
10423+
if (ConstraintSatisfaction Satisfaction;
10424+
Method->getTrailingRequiresClause() &&
10425+
(CheckFunctionConstraints(Method, Satisfaction,
10426+
/*UsageLoc=*/Member->getLocation(),
10427+
/*ForOverloadResolution=*/true) ||
10428+
!Satisfaction.IsSatisfied))
10429+
continue;
10430+
Candidates.push_back(Method);
10431+
FunctionDecl *MoreConstrained =
10432+
Instantiation ? getMoreConstrainedFunction(
10433+
Method, cast<FunctionDecl>(Instantiation))
10434+
: Method;
10435+
if (!MoreConstrained) {
10436+
Ambiguous = true;
10437+
continue;
10438+
}
10439+
if (MoreConstrained == Method) {
10440+
Ambiguous = false;
1042610441
FoundInstantiation = *I;
1042710442
Instantiation = Method;
1042810443
InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
1042910444
MSInfo = Method->getMemberSpecializationInfo();
1043010445
}
1043110446
}
10447+
if (Ambiguous) {
10448+
Diag(Member->getLocation(), diag::err_function_member_spec_ambiguous)
10449+
<< Member;
10450+
for (FunctionDecl *Candidate : Candidates)
10451+
Diag(Candidate->getLocation(), diag::note_function_member_spec_matched)
10452+
<< Candidate;
10453+
return true;
10454+
}
1043210455
} else if (isa<VarDecl>(Member)) {
1043310456
VarDecl *PrevVar;
1043410457
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)