Skip to content

Commit 468e3d9

Browse files
committed
[Clang][Sema] Fix explicit specializations of member function templates with a deduced return type
1 parent f914e8e commit 468e3d9

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

clang-tools-extra/test/clang-tidy/infrastructure/diagnostic.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,4 @@ auto S<>::foo(auto)
6868
{
6969
return 1;
7070
}
71-
// CHECK8: error: conflicting types for 'foo' [clang-diagnostic-error]
72-
// CHECK8: note: previous declaration is here
7371
#endif

clang/lib/Sema/SemaDecl.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10119,23 +10119,6 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
1011910119
Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_auto_fn_virtual);
1012010120
}
1012110121

10122-
if (getLangOpts().CPlusPlus14 &&
10123-
(NewFD->isDependentContext() ||
10124-
(isFriend && CurContext->isDependentContext())) &&
10125-
NewFD->getReturnType()->isUndeducedType()) {
10126-
// If the function template is referenced directly (for instance, as a
10127-
// member of the current instantiation), pretend it has a dependent type.
10128-
// This is not really justified by the standard, but is the only sane
10129-
// thing to do.
10130-
// FIXME: For a friend function, we have not marked the function as being
10131-
// a friend yet, so 'isDependentContext' on the FD doesn't work.
10132-
const FunctionProtoType *FPT =
10133-
NewFD->getType()->castAs<FunctionProtoType>();
10134-
QualType Result = SubstAutoTypeDependent(FPT->getReturnType());
10135-
NewFD->setType(Context.getFunctionType(Result, FPT->getParamTypes(),
10136-
FPT->getExtProtoInfo()));
10137-
}
10138-
1013910122
// C++ [dcl.fct.spec]p3:
1014010123
// The inline specifier shall not appear on a block scope function
1014110124
// declaration.
@@ -12107,6 +12090,35 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD,
1210712090

1210812091
CheckConstPureAttributesUsage(*this, NewFD);
1210912092

12093+
// C++23 [dcl.spec.auto.general]p12:
12094+
// Return type deduction for a templated function with a placeholder in its
12095+
// declared type occurs when the definition is instantiated even if the
12096+
// function body contains a return statement with a non-type-dependent
12097+
// operand.
12098+
//
12099+
// C++23 [temp.dep.expr]p3:
12100+
// An id-expression is type-dependent if it is a template-id that is not a
12101+
// concept-id and is dependent; or if its terminal name is:
12102+
// - [...]
12103+
// - associated by name lookup with one or more declarations of member
12104+
// functions of a class that is the current instantiation declared with a
12105+
// return type that contains a placeholder type,
12106+
// - [...]
12107+
//
12108+
// If this is a templated function with a placeholder in its return type,
12109+
// make the placeholder type dependent since it won't be deduced until the
12110+
// definition is instantiated. We do this here because it needs to happen
12111+
// for implicitly instantiated member functions/member function templates.
12112+
if (getLangOpts().CPlusPlus14 &&
12113+
(NewFD->isDependentContext() &&
12114+
NewFD->getReturnType()->isUndeducedType())) {
12115+
const FunctionProtoType *FPT =
12116+
NewFD->getType()->castAs<FunctionProtoType>();
12117+
QualType NewReturnType = SubstAutoTypeDependent(FPT->getReturnType());
12118+
NewFD->setType(Context.getFunctionType(NewReturnType, FPT->getParamTypes(),
12119+
FPT->getExtProtoInfo()));
12120+
}
12121+
1211012122
// C++11 [dcl.constexpr]p8:
1211112123
// A constexpr specifier for a non-static member function that is not
1211212124
// a constructor declares that member function to be const.

clang/test/SemaCXX/deduced-return-type-cxx14.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,24 @@ namespace Templates {
237237
int (S::*(*p)())(double) = f;
238238
int (S::*(*q)())(double) = f<S, double>;
239239
}
240+
241+
template<typename T>
242+
struct MemberSpecialization {
243+
auto f();
244+
template<typename U> auto f(U);
245+
template<typename U> auto *f(U);
246+
};
247+
248+
template<>
249+
auto MemberSpecialization<int>::f();
250+
251+
template<>
252+
template<typename U>
253+
auto MemberSpecialization<int>::f(U);
254+
255+
template<>
256+
template<typename U>
257+
auto *MemberSpecialization<int>::f(U);
240258
}
241259

242260
auto fwd_decl_using();

0 commit comments

Comments
 (0)