Skip to content

[clang] Allow delayed function instantiation at TU end if initial instantiation fails #117167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,8 @@ class Sema final : public SemaBase {
/// checks.
const TranslationUnitKind TUKind;

bool AtEndOfTU = false;

/// Translation Unit Scope - useful to Objective-C actions that need
/// to lookup file scope declarations in the "ordinary" C decl namespace.
/// For example, user-defined classes, built-in "id" type, etc.
Expand Down Expand Up @@ -13523,7 +13525,9 @@ class Sema final : public SemaBase {
S.PendingLocalImplicitInstantiations);
}

void perform() { S.PerformPendingInstantiations(/*LocalOnly=*/true); }
void perform() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated formatting change.

S.PerformPendingInstantiations(/*LocalOnly=*/true);
}

~LocalEagerInstantiationScope() {
assert(S.PendingLocalImplicitInstantiations.empty() &&
Expand Down Expand Up @@ -13571,7 +13575,7 @@ class Sema final : public SemaBase {
void perform() {
if (Enabled) {
S.DefineUsedVTables();
S.PerformPendingInstantiations();
S.PerformPendingInstantiations(false);
}
}

Expand Down Expand Up @@ -13682,8 +13686,7 @@ class Sema final : public SemaBase {
void InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
FunctionDecl *Function,
bool Recursive = false,
bool DefinitionRequired = false,
bool AtEndOfTU = false);
bool DefinitionRequired = false);
VarTemplateSpecializationDecl *BuildVarTemplateInstantiation(
VarTemplateDecl *VarTemplate, VarDecl *FromVar,
const TemplateArgumentList *PartialSpecArgs,
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/Sema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ void Sema::ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind) {

{
llvm::TimeTraceScope TimeScope("PerformPendingInstantiations");
PerformPendingInstantiations();
PerformPendingInstantiations(/*LocalOnly=*/false);
}

emitDeferredDiags();
Expand All @@ -1160,6 +1160,7 @@ void Sema::ActOnEndOfTranslationUnitFragment(TUFragmentKind Kind) {
void Sema::ActOnEndOfTranslationUnit() {
assert(DelayedDiagnostics.getCurrentPool() == nullptr
&& "reached end of translation unit with a pool attached?");
AtEndOfTU = true;

// If code completion is enabled, don't perform any end-of-translation-unit
// work.
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21054,8 +21054,7 @@ ExprResult Sema::CheckPlaceholderExpr(Expr *E) {
// precedes this use.
InstantiateFunctionDefinition(E->getBeginLoc(), FD,
/*Recursive=*/false,
/*DefinitionRequired=*/true,
/*AtEndOfTU=*/false);
/*DefinitionRequired=*/true);
}
// Produce a properly-typed reference to the function.
CXXScopeSpec SS;
Expand Down
11 changes: 6 additions & 5 deletions clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ static void instantiateOMPDeclareVariantAttr(
return;
S.InstantiateFunctionDefinition(
New->getLocation(), SubstFD, /* Recursive */ true,
/* DefinitionRequired */ false, /* AtEndOfTU */ false);
/* DefinitionRequired */ false);
SubstFD->setInstantiationIsPending(!SubstFD->isDefined());
E = DeclRefExpr::Create(S.Context, NestedNameSpecifierLoc(),
SourceLocation(), SubstFD,
Expand Down Expand Up @@ -4932,8 +4932,7 @@ FunctionDecl *Sema::InstantiateFunctionDeclaration(
void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
FunctionDecl *Function,
bool Recursive,
bool DefinitionRequired,
bool AtEndOfTU) {
bool DefinitionRequired) {
if (Function->isInvalidDecl() || isa<CXXDeductionGuideDecl>(Function))
return;

Expand Down Expand Up @@ -6470,15 +6469,17 @@ void Sema::PerformPendingInstantiations(bool LocalOnly) {
getASTContext().forEachMultiversionedFunctionVersion(
Function, [this, Inst, DefinitionRequired](FunctionDecl *CurFD) {
InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, CurFD, true,
DefinitionRequired, true);
DefinitionRequired);
if (CurFD->isDefined())
CurFD->setInstantiationIsPending(false);
});
} else {
InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, Function, true,
DefinitionRequired, true);
DefinitionRequired);
if (Function->isDefined())
Function->setInstantiationIsPending(false);
else if (!AtEndOfTU)
LateParsedInstantiations.push_back(Inst);
}
// Definition of a PCH-ed template declaration may be available only in the TU.
if (!LocalOnly && LangOpts.PCHInstantiateTemplates &&
Expand Down
26 changes: 26 additions & 0 deletions clang/test/SemaTemplate/instantiate-function-delayed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %clang_cc1 -std=c++20 -verify %s
// expected-no-diagnostics

template <typename T>
auto foo(T const& arg) -> T;

template <typename Fp, typename Vis>
auto dispatch(Fp fp, Vis vis) {
return fp(vis);
}

auto baz(int v) {
auto callable = []<typename Arg>(Arg const& arg) -> int {
return foo(arg);
};
return dispatch(callable, v);
}

template <typename T>
auto foo(T const& arg) -> T {
return arg;
}

int main() {
return baz(5);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline needed at the end of test.

Also, would like to see a codegen test that shows that we are properly generating the right instantiations.

Loading