-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[Clang] Defer the instantiation of explicit-specifier until constraint checking completes #70548
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
Merged
erichkeane
merged 8 commits into
llvm:main
from
LYP951018:deferred_explicit_specifier_instantiation
Nov 1, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
11ceaed
Defer the instantiation of explicit-specifier after constraint checking
LYP951018 d7db83b
fix CR comments
LYP951018 808802b
unify ES identifier
LYP951018 53c6c6a
add hasErrorOccurred check
LYP951018 fd03551
add `IsInvalid` check
LYP951018 fe4d5f1
remove braces for simple if
LYP951018 aaa687e
fix CR comments
LYP951018 93f962d
Merge branch 'main' into deferred_explicit_specifier_instantiation
erichkeane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3553,6 +3553,48 @@ static unsigned getPackIndexForParam(Sema &S, | |
llvm_unreachable("parameter index would not be produced from template"); | ||
} | ||
|
||
// if `Specialization` is a `CXXConstructorDecl` or `CXXConversionDecl`, | ||
// we'll try to instantiate and update its explicit specifier after constraint | ||
// checking. | ||
static Sema::TemplateDeductionResult instantiateExplicitSpecifierDeferred( | ||
Sema &S, FunctionDecl *Specialization, | ||
const MultiLevelTemplateArgumentList &SubstArgs, | ||
TemplateDeductionInfo &Info, FunctionTemplateDecl *FunctionTemplate, | ||
ArrayRef<TemplateArgument> DeducedArgs) { | ||
auto GetExplicitSpecifier = [](FunctionDecl *D) { | ||
return isa<CXXConstructorDecl>(D) | ||
? cast<CXXConstructorDecl>(D)->getExplicitSpecifier() | ||
: cast<CXXConversionDecl>(D)->getExplicitSpecifier(); | ||
}; | ||
auto SetExplicitSpecifier = [](FunctionDecl *D, ExplicitSpecifier ES) { | ||
isa<CXXConstructorDecl>(D) | ||
? cast<CXXConstructorDecl>(D)->setExplicitSpecifier(ES) | ||
: cast<CXXConversionDecl>(D)->setExplicitSpecifier(ES); | ||
}; | ||
|
||
ExplicitSpecifier ES = GetExplicitSpecifier(Specialization); | ||
Expr *ExplicitExpr = ES.getExpr(); | ||
if (!ExplicitExpr) | ||
return Sema::TDK_Success; | ||
if (!ExplicitExpr->isValueDependent()) | ||
return Sema::TDK_Success; | ||
|
||
Sema::InstantiatingTemplate Inst( | ||
S, Info.getLocation(), FunctionTemplate, DeducedArgs, | ||
Sema::CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); | ||
if (Inst.isInvalid()) | ||
return Sema::TDK_InstantiationDepth; | ||
Sema::SFINAETrap Trap(S); | ||
const ExplicitSpecifier InstantiatedES = | ||
S.instantiateExplicitSpecifier(SubstArgs, ES); | ||
if (InstantiatedES.isInvalid() || Trap.hasErrorOccurred()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have a test that checks this case at all? Same for the other failures, if we know how to test them we should write a test to verify them. |
||
Specialization->setInvalidDecl(true); | ||
return Sema::TDK_SubstitutionFailure; | ||
} | ||
SetExplicitSpecifier(Specialization, InstantiatedES); | ||
return Sema::TDK_Success; | ||
} | ||
|
||
/// Finish template argument deduction for a function template, | ||
/// checking the deduced template arguments for completeness and forming | ||
/// the function template specialization. | ||
|
@@ -3682,6 +3724,17 @@ Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( | |
} | ||
} | ||
|
||
// We skipped the instantiation of the explicit-specifier during the | ||
// substitution of `FD` before. So, we try to instantiate it back if | ||
// `Specialization` is either a constructor or a conversion function. | ||
if (isa<CXXConstructorDecl, CXXConversionDecl>(Specialization)) { | ||
if (TDK_Success != instantiateExplicitSpecifierDeferred( | ||
*this, Specialization, SubstArgs, Info, | ||
FunctionTemplate, DeducedArgs)) { | ||
return TDK_SubstitutionFailure; | ||
} | ||
} | ||
|
||
if (OriginalCallArgs) { | ||
// C++ [temp.deduct.call]p4: | ||
// In general, the deduction process attempts to find template argument | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s | ||
|
||
template <typename T1, typename T2> struct is_same { | ||
static constexpr bool value = false; | ||
}; | ||
|
||
template <typename T> struct is_same<T, T> { | ||
static constexpr bool value = true; | ||
}; | ||
|
||
template <class T, class U> | ||
concept SameHelper = is_same<T, U>::value; | ||
template <class T, class U> | ||
concept same_as = SameHelper<T, U> && SameHelper<U, T>; | ||
|
||
namespace deferred_instantiation { | ||
template <class X> constexpr X do_not_instantiate() { return nullptr; } | ||
|
||
struct T { | ||
template <same_as<float> X> explicit(do_not_instantiate<X>()) T(X) {} | ||
|
||
T(int) {} | ||
}; | ||
|
||
T t(5); | ||
// expected-error@17{{cannot initialize}} | ||
// expected-note@20{{in instantiation of function template specialization}} | ||
// expected-note@30{{while substituting deduced template arguments}} | ||
// expected-note@30{{in instantiation of function template specialization}} | ||
T t2(5.0f); | ||
} // namespace deferred_instantiation |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.