mirror of
https://github.com/freebsd/freebsd-src
synced 2024-07-23 03:06:48 +00:00
Merge commit 158f4f30adb4 from llvm git (by Corentin Jabot):
[Clang] Do not change the type of captured vars when checking lambda constraints When checking the constraint of a lambda, we need to respect the constness of the call operator when establishing the type of capture variables. In D124351, this was done by adding const to the captured variable... However, that would change the type of the variable outside of the scope of the lambda, which is clearly not the desired outcome. Instead, to ensure const-correctness, we need to populate a LambdaScopeInfo with the capture variables before checking the constraints of a generic lambda. There is no changelog as I'd like to tentatively propose we backport this change to RC3 as it is a regression introduced in the Clang 17 cycle. Fixes #61267 Reviewed By: aaron.ballman, #clang-language-wg Differential Revision: https://reviews.llvm.org/D158433 Merge commit 3ed9e9e3ace6 from llvm git (by Corentin Jabot): [Clang] Add captures to the instantiation scope of lambda call operators Like concepts checking, a trailing return type of a lambda in a dependent context may refer to captures in which case they may need to be rebuilt, so the map of local decl should include captures. This patch reveal a pre-existing issue. `this` is always recomputed by TreeTransform. `*this` (like all captures) only become `const` after the parameter list. However, if try to recompute the value of `this` (in a parameter) during template instantiation while determining the type of the call operator, we will determine it to be const (unless the lambda is mutable). There is no good way to know at that point that we are in a parameter or not, the easiest/best solution is to transform the type of this. Note that doing so break a handful of HLSL tests. So this is a prototype at this point. Fixes #65067 Fixes #63675 Reviewed By: erichkeane Differential Revision: https://reviews.llvm.org/D159126 This fixes 'Assertion failed: (isa<LabelDecl>(D) && "declaration not instantiated in this scope"), function findInstantiationOf' error when building databases/mongodb44. PR: 273753 MFC after: 1 month
This commit is contained in:
parent
5c16e71d30
commit
feb5b0c76f
|
@ -7315,6 +7315,16 @@ class Sema final {
|
|||
CXXConversionDecl *Conv,
|
||||
Expr *Src);
|
||||
|
||||
sema::LambdaScopeInfo *RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator);
|
||||
|
||||
class LambdaScopeForCallOperatorInstantiationRAII
|
||||
: private FunctionScopeRAII {
|
||||
public:
|
||||
LambdaScopeForCallOperatorInstantiationRAII(
|
||||
Sema &SemasRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
|
||||
LocalInstantiationScope &Scope);
|
||||
};
|
||||
|
||||
/// Check whether the given expression is a valid constraint expression.
|
||||
/// A diagnostic is emitted if it is not, false is returned, and
|
||||
/// PossibleNonPrimary will be set to true if the failure might be due to a
|
||||
|
|
|
@ -13,12 +13,14 @@
|
|||
#include "clang/Sema/SemaConcept.h"
|
||||
#include "TreeTransform.h"
|
||||
#include "clang/AST/ASTLambda.h"
|
||||
#include "clang/AST/DeclCXX.h"
|
||||
#include "clang/AST/ExprConcepts.h"
|
||||
#include "clang/AST/RecursiveASTVisitor.h"
|
||||
#include "clang/Basic/OperatorPrecedence.h"
|
||||
#include "clang/Sema/EnterExpressionEvaluationContext.h"
|
||||
#include "clang/Sema/Initialization.h"
|
||||
#include "clang/Sema/Overload.h"
|
||||
#include "clang/Sema/ScopeInfo.h"
|
||||
#include "clang/Sema/Sema.h"
|
||||
#include "clang/Sema/SemaDiagnostic.h"
|
||||
#include "clang/Sema/SemaInternal.h"
|
||||
|
@ -540,11 +542,6 @@ bool Sema::addInstantiatedCapturesToScope(
|
|||
auto AddSingleCapture = [&](const ValueDecl *CapturedPattern,
|
||||
unsigned Index) {
|
||||
ValueDecl *CapturedVar = LambdaClass->getCapture(Index)->getCapturedVar();
|
||||
if (cast<CXXMethodDecl>(Function)->isConst()) {
|
||||
QualType T = CapturedVar->getType();
|
||||
T.addConst();
|
||||
CapturedVar->setType(T);
|
||||
}
|
||||
if (CapturedVar->isInitCapture())
|
||||
Scope.InstantiatedLocal(CapturedPattern, CapturedVar);
|
||||
};
|
||||
|
@ -603,11 +600,6 @@ bool Sema::SetupConstraintScope(
|
|||
if (addInstantiatedParametersToScope(FD, FromMemTempl->getTemplatedDecl(),
|
||||
Scope, MLTAL))
|
||||
return true;
|
||||
// Make sure the captures are also added to the instantiation scope.
|
||||
if (isLambdaCallOperator(FD) &&
|
||||
addInstantiatedCapturesToScope(FD, FromMemTempl->getTemplatedDecl(),
|
||||
Scope, MLTAL))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -632,11 +624,6 @@ bool Sema::SetupConstraintScope(
|
|||
// child-function.
|
||||
if (addInstantiatedParametersToScope(FD, InstantiatedFrom, Scope, MLTAL))
|
||||
return true;
|
||||
|
||||
// Make sure the captures are also added to the instantiation scope.
|
||||
if (isLambdaCallOperator(FD) &&
|
||||
addInstantiatedCapturesToScope(FD, InstantiatedFrom, Scope, MLTAL))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -714,6 +701,10 @@ bool Sema::CheckFunctionConstraints(const FunctionDecl *FD,
|
|||
Record = const_cast<CXXRecordDecl *>(Method->getParent());
|
||||
}
|
||||
CXXThisScopeRAII ThisScope(*this, Record, ThisQuals, Record != nullptr);
|
||||
|
||||
LambdaScopeForCallOperatorInstantiationRAII LambdaScope(
|
||||
*this, const_cast<FunctionDecl *>(FD), *MLTAL, Scope);
|
||||
|
||||
return CheckConstraintSatisfaction(
|
||||
FD, {FD->getTrailingRequiresClause()}, *MLTAL,
|
||||
SourceRange(UsageLoc.isValid() ? UsageLoc : FD->getLocation()),
|
||||
|
@ -900,12 +891,10 @@ bool Sema::CheckInstantiatedFunctionTemplateConstraints(
|
|||
ThisQuals = Method->getMethodQualifiers();
|
||||
Record = Method->getParent();
|
||||
}
|
||||
|
||||
CXXThisScopeRAII ThisScope(*this, Record, ThisQuals, Record != nullptr);
|
||||
FunctionScopeRAII FuncScope(*this);
|
||||
if (isLambdaCallOperator(Decl))
|
||||
PushLambdaScope();
|
||||
else
|
||||
FuncScope.disable();
|
||||
LambdaScopeForCallOperatorInstantiationRAII LambdaScope(
|
||||
*this, const_cast<FunctionDecl *>(Decl), *MLTAL, Scope);
|
||||
|
||||
llvm::SmallVector<Expr *, 1> Converted;
|
||||
return CheckConstraintSatisfaction(Template, TemplateAC, Converted, *MLTAL,
|
||||
|
|
|
@ -15172,14 +15172,17 @@ Sema::CheckForFunctionRedefinition(FunctionDecl *FD,
|
|||
FD->setInvalidDecl();
|
||||
}
|
||||
|
||||
static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator,
|
||||
Sema &S) {
|
||||
CXXRecordDecl *const LambdaClass = CallOperator->getParent();
|
||||
LambdaScopeInfo *Sema::RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator) {
|
||||
CXXRecordDecl *LambdaClass = CallOperator->getParent();
|
||||
|
||||
LambdaScopeInfo *LSI = S.PushLambdaScope();
|
||||
LambdaScopeInfo *LSI = PushLambdaScope();
|
||||
LSI->CallOperator = CallOperator;
|
||||
LSI->Lambda = LambdaClass;
|
||||
LSI->ReturnType = CallOperator->getReturnType();
|
||||
// This function in calls in situation where the context of the call operator
|
||||
// is not entered, so we set AfterParameterList to false, so that
|
||||
// `tryCaptureVariable` finds explicit captures in the appropriate context.
|
||||
LSI->AfterParameterList = false;
|
||||
const LambdaCaptureDefault LCD = LambdaClass->getLambdaCaptureDefault();
|
||||
|
||||
if (LCD == LCD_None)
|
||||
|
@ -15200,7 +15203,7 @@ static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator,
|
|||
if (C.capturesVariable()) {
|
||||
ValueDecl *VD = C.getCapturedVar();
|
||||
if (VD->isInitCapture())
|
||||
S.CurrentInstantiationScope->InstantiatedLocal(VD, VD);
|
||||
CurrentInstantiationScope->InstantiatedLocal(VD, VD);
|
||||
const bool ByRef = C.getCaptureKind() == LCK_ByRef;
|
||||
LSI->addCapture(VD, /*IsBlock*/false, ByRef,
|
||||
/*RefersToEnclosingVariableOrCapture*/true, C.getLocation(),
|
||||
|
@ -15217,6 +15220,7 @@ static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator,
|
|||
}
|
||||
++I;
|
||||
}
|
||||
return LSI;
|
||||
}
|
||||
|
||||
Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
|
||||
|
@ -15320,7 +15324,7 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
|
|||
assert(inTemplateInstantiation() &&
|
||||
"There should be an active template instantiation on the stack "
|
||||
"when instantiating a generic lambda!");
|
||||
RebuildLambdaScopeInfo(cast<CXXMethodDecl>(D), *this);
|
||||
RebuildLambdaScopeInfo(cast<CXXMethodDecl>(D));
|
||||
} else {
|
||||
// Enter a new function scope
|
||||
PushFunctionScope();
|
||||
|
|
|
@ -19619,13 +19619,6 @@ bool Sema::tryCaptureVariable(
|
|||
FunctionScopesIndex == MaxFunctionScopesIndex && VarDC == DC)
|
||||
return true;
|
||||
|
||||
// When evaluating some attributes (like enable_if) we might refer to a
|
||||
// function parameter appertaining to the same declaration as that
|
||||
// attribute.
|
||||
if (const auto *Parm = dyn_cast<ParmVarDecl>(Var);
|
||||
Parm && Parm->getDeclContext() == DC)
|
||||
return true;
|
||||
|
||||
// Only block literals, captured statements, and lambda expressions can
|
||||
// capture; other scopes don't work.
|
||||
DeclContext *ParentDC =
|
||||
|
@ -19653,6 +19646,14 @@ bool Sema::tryCaptureVariable(
|
|||
CSI->getCapture(Var).markUsed(BuildAndDiagnose);
|
||||
break;
|
||||
}
|
||||
|
||||
// When evaluating some attributes (like enable_if) we might refer to a
|
||||
// function parameter appertaining to the same declaration as that
|
||||
// attribute.
|
||||
if (const auto *Parm = dyn_cast<ParmVarDecl>(Var);
|
||||
Parm && Parm->getDeclContext() == DC)
|
||||
return true;
|
||||
|
||||
// If we are instantiating a generic lambda call operator body,
|
||||
// we do not want to capture new variables. What was captured
|
||||
// during either a lambdas transformation or initial parsing
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "clang/Sema/ScopeInfo.h"
|
||||
#include "clang/Sema/SemaInternal.h"
|
||||
#include "clang/Sema/SemaLambda.h"
|
||||
#include "clang/Sema/Template.h"
|
||||
#include "llvm/ADT/STLExtras.h"
|
||||
#include <optional>
|
||||
using namespace clang;
|
||||
|
@ -2222,3 +2223,34 @@ ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation,
|
|||
|
||||
return BuildBlock;
|
||||
}
|
||||
|
||||
Sema::LambdaScopeForCallOperatorInstantiationRAII::
|
||||
LambdaScopeForCallOperatorInstantiationRAII(
|
||||
Sema &SemasRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
|
||||
LocalInstantiationScope &Scope)
|
||||
: FunctionScopeRAII(SemasRef) {
|
||||
if (!isLambdaCallOperator(FD)) {
|
||||
FunctionScopeRAII::disable();
|
||||
return;
|
||||
}
|
||||
|
||||
if (FD->isTemplateInstantiation() && FD->getPrimaryTemplate()) {
|
||||
FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate();
|
||||
if (const auto *FromMemTempl =
|
||||
PrimaryTemplate->getInstantiatedFromMemberTemplate()) {
|
||||
SemasRef.addInstantiatedCapturesToScope(
|
||||
FD, FromMemTempl->getTemplatedDecl(), Scope, MLTAL);
|
||||
}
|
||||
}
|
||||
|
||||
else if (FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization ||
|
||||
FD->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate) {
|
||||
FunctionDecl *InstantiatedFrom =
|
||||
FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization
|
||||
? FD->getInstantiatedFromMemberFunction()
|
||||
: FD->getInstantiatedFromDecl();
|
||||
SemasRef.addInstantiatedCapturesToScope(FD, InstantiatedFrom, Scope, MLTAL);
|
||||
}
|
||||
|
||||
SemasRef.RebuildLambdaScopeInfo(cast<CXXMethodDecl>(FD));
|
||||
}
|
||||
|
|
|
@ -2426,6 +2426,9 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(
|
|||
cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
|
||||
LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
|
||||
|
||||
Sema::LambdaScopeForCallOperatorInstantiationRAII LambdaScope(
|
||||
SemaRef, const_cast<CXXMethodDecl *>(D), TemplateArgs, Scope);
|
||||
|
||||
// Instantiate enclosing template arguments for friends.
|
||||
SmallVector<TemplateParameterList *, 4> TempParamLists;
|
||||
unsigned NumTempParamLists = 0;
|
||||
|
|
|
@ -12285,7 +12285,16 @@ TreeTransform<Derived>::TransformCXXNullPtrLiteralExpr(
|
|||
template<typename Derived>
|
||||
ExprResult
|
||||
TreeTransform<Derived>::TransformCXXThisExpr(CXXThisExpr *E) {
|
||||
QualType T = getSema().getCurrentThisType();
|
||||
|
||||
// In lambdas, the qualifiers of the type depends of where in
|
||||
// the call operator `this` appear, and we do not have a good way to
|
||||
// rebuild this information, so we transform the type.
|
||||
//
|
||||
// In other contexts, the type of `this` may be overrided
|
||||
// for type deduction, so we need to recompute it.
|
||||
QualType T = getSema().getCurLambda() ?
|
||||
getDerived().TransformType(E->getType())
|
||||
: getSema().getCurrentThisType();
|
||||
|
||||
if (!getDerived().AlwaysRebuild() && T == E->getType()) {
|
||||
// Mark it referenced in the new context regardless.
|
||||
|
|
Loading…
Reference in a new issue