[llvm-branch-commits] [clang] 71886c5 - Where possible, don't try to ask whether a template argument is
Richard Smith via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Dec 17 23:27:25 PST 2020
Author: Richard Smith
Date: 2020-12-17T23:23:05-08:00
New Revision: 71886c56f336667969be4cac0b6a17a3f75b7555
URL: https://github.com/llvm/llvm-project/commit/71886c56f336667969be4cac0b6a17a3f75b7555
DIFF: https://github.com/llvm/llvm-project/commit/71886c56f336667969be4cac0b6a17a3f75b7555.diff
LOG: Where possible, don't try to ask whether a template argument is
dependent until it's been converted to match its parameter.
The type of a non-type template parameter can in general affect whether
the template argument is dependent.
Note that this is not always possible. For template arguments that name
static local variables in templates, the type of the template parameter
affects whether the argument is dependent, so the query is imprecise
until we know the parameter type. For example, in:
template<typename T> void f() {
static const int n = 5;
typename T::template X<n> x;
}
... we don't know whether 'n' is dependent until we know whether the
corresponding template parameter is of type 'int' or 'const int&'.
Added:
clang/test/SemaTemplate/instantiate-static-local.cpp
Modified:
clang/include/clang/AST/Type.h
clang/lib/AST/Type.cpp
clang/lib/Sema/SemaConcept.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaTemplate.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index a3059b83c1be..21c8bf79152e 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -5112,11 +5112,24 @@ class alignas(8) TemplateSpecializationType
public:
/// Determine whether any of the given template arguments are dependent.
- static bool anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
- bool &InstantiationDependent);
-
- static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &,
- bool &InstantiationDependent);
+ ///
+ /// The converted arguments should be supplied when known; whether an
+ /// argument is dependent can depend on the conversions performed on it
+ /// (for example, a 'const int' passed as a template argument might be
+ /// dependent if the parameter is a reference but non-dependent if the
+ /// parameter is an int).
+ ///
+ /// Note that the \p Args parameter is unused: this is intentional, to remind
+ /// the caller that they need to pass in the converted arguments, not the
+ /// specified arguments.
+ static bool
+ anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
+ ArrayRef<TemplateArgument> Converted);
+ static bool
+ anyDependentTemplateArguments(const TemplateArgumentListInfo &,
+ ArrayRef<TemplateArgument> Converted);
+ static bool anyInstantiationDependentTemplateArguments(
+ ArrayRef<TemplateArgumentLoc> Args);
/// True if this template specialization type matches a current
/// instantiation in the context in which it is found.
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index e5811dbc44c5..5dec80be9ccb 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -3590,24 +3590,24 @@ void SubstTemplateTypeParmPackType::Profile(llvm::FoldingSetNodeID &ID,
ID.AddPointer(P.getAsType().getAsOpaquePtr());
}
-bool TemplateSpecializationType::
-anyDependentTemplateArguments(const TemplateArgumentListInfo &Args,
- bool &InstantiationDependent) {
- return anyDependentTemplateArguments(Args.arguments(),
- InstantiationDependent);
+bool TemplateSpecializationType::anyDependentTemplateArguments(
+ const TemplateArgumentListInfo &Args, ArrayRef<TemplateArgument> Converted) {
+ return anyDependentTemplateArguments(Args.arguments(), Converted);
}
-bool TemplateSpecializationType::
-anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args,
- bool &InstantiationDependent) {
- for (const TemplateArgumentLoc &ArgLoc : Args) {
- if (ArgLoc.getArgument().isDependent()) {
- InstantiationDependent = true;
+bool TemplateSpecializationType::anyDependentTemplateArguments(
+ ArrayRef<TemplateArgumentLoc> Args, ArrayRef<TemplateArgument> Converted) {
+ for (const TemplateArgument &Arg : Converted)
+ if (Arg.isDependent())
return true;
- }
+ return false;
+}
+bool TemplateSpecializationType::anyInstantiationDependentTemplateArguments(
+ ArrayRef<TemplateArgumentLoc> Args) {
+ for (const TemplateArgumentLoc &ArgLoc : Args) {
if (ArgLoc.getArgument().isInstantiationDependent())
- InstantiationDependent = true;
+ return true;
}
return false;
}
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index ddd95faebe99..1ff7b1cdd515 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -1053,25 +1053,20 @@ ReturnTypeRequirement(TemplateParameterList *TPL) :
auto *Constraint =
cast_or_null<ConceptSpecializationExpr>(
TC->getImmediatelyDeclaredConstraint());
- bool Dependent = false;
- if (Constraint->getTemplateArgsAsWritten()) {
- for (auto &ArgLoc :
- Constraint->getTemplateArgsAsWritten()->arguments().drop_front(1)) {
- if (ArgLoc.getArgument().isDependent()) {
- Dependent = true;
- break;
- }
- }
- }
+ bool Dependent =
+ Constraint->getTemplateArgsAsWritten() &&
+ TemplateSpecializationType::anyInstantiationDependentTemplateArguments(
+ Constraint->getTemplateArgsAsWritten()->arguments().drop_front(1));
TypeConstraintInfo.setInt(Dependent ? 1 : 0);
}
concepts::TypeRequirement::TypeRequirement(TypeSourceInfo *T) :
- Requirement(RK_Type, T->getType()->isDependentType(),
+ Requirement(RK_Type, T->getType()->isInstantiationDependentType(),
T->getType()->containsUnexpandedParameterPack(),
// We reach this ctor with either dependent types (in which
// IsSatisfied doesn't matter) or with non-dependent type in
// which the existence of the type indicates satisfaction.
- /*IsSatisfied=*/true
- ), Value(T),
- Status(T->getType()->isDependentType() ? SS_Dependent : SS_Satisfied) {}
+ /*IsSatisfied=*/true),
+ Value(T),
+ Status(T->getType()->isInstantiationDependentType() ? SS_Dependent
+ : SS_Satisfied) {}
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f10055037269..2b6eb397b82b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -9516,12 +9516,10 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
// that either the specialized function type or the specialized
// template is dependent, and therefore matching will fail. In
// this case, don't check the specialization yet.
- bool InstantiationDependent = false;
if (isFunctionTemplateSpecialization && isFriend &&
(NewFD->getType()->isDependentType() || DC->isDependentContext() ||
- TemplateSpecializationType::anyDependentTemplateArguments(
- TemplateArgs,
- InstantiationDependent))) {
+ TemplateSpecializationType::anyInstantiationDependentTemplateArguments(
+ TemplateArgs.arguments()))) {
assert(HasExplicitTemplateArgs &&
"friend function specialization without template args");
if (CheckDependentFunctionTemplateSpecialization(NewFD, TemplateArgs,
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index d5d11dbdda20..dd361ec91abe 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -3661,7 +3661,6 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
QualType CanonType;
- bool InstantiationDependent = false;
if (TypeAliasTemplateDecl *AliasTemplate =
dyn_cast<TypeAliasTemplateDecl>(Template)) {
@@ -3724,7 +3723,7 @@ QualType Sema::CheckTemplateIdType(TemplateName Name,
}
} else if (Name.isDependent() ||
TemplateSpecializationType::anyDependentTemplateArguments(
- TemplateArgs, InstantiationDependent)) {
+ TemplateArgs, Converted)) {
// This class template specialization is a dependent
// type. Therefore, its canonical type is another class template
// specialization type that contains all of the converted
@@ -4315,11 +4314,9 @@ DeclResult Sema::ActOnVarTemplateSpecialization(
// FIXME: Move these checks to CheckTemplatePartialSpecializationArgs so we
// also do them during instantiation.
- bool InstantiationDependent;
if (!Name.isDependent() &&
- !TemplateSpecializationType::anyDependentTemplateArguments(
- TemplateArgs.arguments(),
- InstantiationDependent)) {
+ !TemplateSpecializationType::anyDependentTemplateArguments(TemplateArgs,
+ Converted)) {
Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
<< VarTemplate->getDeclName();
IsPartialSpecialization = false;
@@ -4480,10 +4477,9 @@ Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc,
return true;
// Produce a placeholder value if the specialization is dependent.
- bool InstantiationDependent = false;
if (Template->getDeclContext()->isDependentContext() ||
- TemplateSpecializationType::anyDependentTemplateArguments(
- TemplateArgs, InstantiationDependent))
+ TemplateSpecializationType::anyDependentTemplateArguments(TemplateArgs,
+ Converted))
return DeclResult();
// Find the variable template specialization declaration that
@@ -4669,22 +4665,16 @@ Sema::CheckConceptTemplateId(const CXXScopeSpec &SS,
return ExprError();
ConstraintSatisfaction Satisfaction;
- bool AreArgsDependent = false;
- for (TemplateArgument &Arg : Converted) {
- if (Arg.isDependent()) {
- AreArgsDependent = true;
- break;
- }
- }
+ bool AreArgsDependent =
+ TemplateSpecializationType::anyDependentTemplateArguments(*TemplateArgs,
+ Converted);
if (!AreArgsDependent &&
- CheckConstraintSatisfaction(NamedConcept,
- {NamedConcept->getConstraintExpr()},
- Converted,
- SourceRange(SS.isSet() ? SS.getBeginLoc() :
- ConceptNameInfo.getLoc(),
- TemplateArgs->getRAngleLoc()),
- Satisfaction))
- return ExprError();
+ CheckConstraintSatisfaction(
+ NamedConcept, {NamedConcept->getConstraintExpr()}, Converted,
+ SourceRange(SS.isSet() ? SS.getBeginLoc() : ConceptNameInfo.getLoc(),
+ TemplateArgs->getRAngleLoc()),
+ Satisfaction))
+ return ExprError();
return ConceptSpecializationExpr::Create(Context,
SS.isSet() ? SS.getWithLocInContext(Context) : NestedNameSpecifierLoc{},
@@ -8345,10 +8335,9 @@ DeclResult Sema::ActOnClassTemplateSpecialization(
// FIXME: Move this to CheckTemplatePartialSpecializationArgs so we
// also do it during instantiation.
- bool InstantiationDependent;
if (!Name.isDependent() &&
- !TemplateSpecializationType::anyDependentTemplateArguments(
- TemplateArgs.arguments(), InstantiationDependent)) {
+ !TemplateSpecializationType::anyDependentTemplateArguments(TemplateArgs,
+ Converted)) {
Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
<< ClassTemplate->getDeclName();
isPartialSpecialization = false;
diff --git a/clang/test/SemaTemplate/instantiate-static-local.cpp b/clang/test/SemaTemplate/instantiate-static-local.cpp
new file mode 100644
index 000000000000..dd139011ac6b
--- /dev/null
+++ b/clang/test/SemaTemplate/instantiate-static-local.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -std=c++2a -x c++ %s -verify
+
+namespace use_after_instantiation {
+ template<int &R> struct A { static constexpr int &value = R; };
+
+ template<typename = void> auto S() {
+ static int s;
+ return A<s>{};
+ }
+
+ auto &s = decltype(S())::value;
+
+ // This is ill-formed, but it should not crash.
+ // FIXME: Right now, it does crash.
+ // expected-no-diagnostics
+#if 0
+ template<typename = void> auto T() {
+ static int s;
+ struct A {
+ static constexpr int &value = s; // expected-error {{static}}
+ };
+ return A{};
+ }
+
+ auto &t = decltype(T())::value;
+#endif
+}
More information about the llvm-branch-commits
mailing list