From nobody Fri Dec 08 17:39:22 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Smz0W08dxz5360g; Fri, 8 Dec 2023 17:39:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Smz0V4T9sz3StP; Fri, 8 Dec 2023 17:39:22 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702057162; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8RyR78qkpHm6NJEwmPTuPLPEgQzvkr9Qez1FSG6oHfM=; b=F2t+bCZPpjaeMxpc/VJo6/qqCuAPYJLedXyi2C1zkEcsmbyBMa/GKEBw3t7oj8hOaoSk0n v+M5CksJdIkIJfamFvn1ZFwRrF+eiKHCRvDntfxQxlgfn9+otpjYIpORIbRiluyJs7HndP j7rbM5xj4K3Hy5/kLK9n1CMCJ/+ZtdK8rqvJNPIo5mV+IXkzEziRIy1jKdYU83B3BWOiOn iYsNUZQu9AGPPvCABUq7mzVUYHngz3gclZCn7l0N0CQf25ZAEAz08lyXPcdFiR6F5Xl/8m xsaJi4KHacoY5ETgPEUioRR/Er/p/iaKyIwooNfr2xY4v2Cn6uR/seFoZkLv9w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1702057162; a=rsa-sha256; cv=none; b=f5pBZxVRODAb/23E5QU87jjHSOVa4lP3HiLVuu0zhZjWS3CGUstZllOLvAgSayWiQc4nod snHKZ98JpZRBRUrTjZyD5BrVEGM1ej0fkSbhZ2ofR9S1UMElgJiSdvTi6Os1Z7y5Y9P7BW F6XI7wa4k0IoanlDzvdaAPY2TJY5tKMWYBcDLhwOvuUTZvPenaP/Tt1x6D05VYkCOCNIQy 1hjFmEgmAtxBH6ll4eS8y24ZEmdC6JL17BTF52BJSPBufa0D3+Li8xYwEGaibSTvas9Yu1 L+h0OIu5lZukTcMY+p1WKEuPMW7WSZTOMGyfK/nBEOY4LJClfaxlfhZP6/P2ZA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702057162; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8RyR78qkpHm6NJEwmPTuPLPEgQzvkr9Qez1FSG6oHfM=; b=FrSYbKAdvW/D62Ugik9B4r1vaaDtmvoT5LKwOVVl6uvEggnxxV1+fpyWZ6pjp/AoDmzxzZ JLG9Iin5AC1Kz2LMFTirod4DUr4xRAbiYMtnWSCSdZZbKWwidRjkVW1k13cv5tsogL85bG 0qha5P4QDmVStmUBACQ78fEO3Le3fBJt3oWImqXF15MVnnNERrsH7VOrIPB7LFZKDfcbdb odQwFjjWAnbyzUL79j5v65UfklyDLJoMuOK5tvNaiM+QumkeIsq/bsgds3XzSfdqX+mSYn pf+Xj5Myt2j+u1DYghNh36bTqQ18jBei/r8tBDua9yxnSi8cbze+Mf17wzZmXQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Smz0V3YzKzWLX; Fri, 8 Dec 2023 17:39:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3B8HdMwU068133; Fri, 8 Dec 2023 17:39:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3B8HdMOd068130; Fri, 8 Dec 2023 17:39:22 GMT (envelope-from git) Date: Fri, 8 Dec 2023 17:39:22 GMT Message-Id: <202312081739.3B8HdMOd068130@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dimitry Andric Subject: git: feb5b0c76f9c - main - Merge commit 158f4f30adb4 from llvm git (by Corentin Jabot): List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dim X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: feb5b0c76f9c7b510583b0489918300cbf966e0f Auto-Submitted: auto-generated The branch main has been updated by dim: URL: https://cgit.FreeBSD.org/src/commit/?id=feb5b0c76f9c7b510583b0489918300cbf966e0f commit feb5b0c76f9c7b510583b0489918300cbf966e0f Author: Dimitry Andric AuthorDate: 2023-12-07 12:47:54 +0000 Commit: Dimitry Andric CommitDate: 2023-12-08 17:36:06 +0000 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(D) && "declaration not instantiated in this scope"), function findInstantiationOf' error when building databases/mongodb44. PR: 273753 MFC after: 1 month --- .../llvm-project/clang/include/clang/Sema/Sema.h | 10 +++++++ .../llvm-project/clang/lib/Sema/SemaConcept.cpp | 29 ++++++-------------- contrib/llvm-project/clang/lib/Sema/SemaDecl.cpp | 16 +++++++---- contrib/llvm-project/clang/lib/Sema/SemaExpr.cpp | 15 +++++----- contrib/llvm-project/clang/lib/Sema/SemaLambda.cpp | 32 ++++++++++++++++++++++ .../clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 3 ++ .../llvm-project/clang/lib/Sema/TreeTransform.h | 11 +++++++- 7 files changed, 82 insertions(+), 34 deletions(-) diff --git a/contrib/llvm-project/clang/include/clang/Sema/Sema.h b/contrib/llvm-project/clang/include/clang/Sema/Sema.h index 3752a23faa85..28a5f17d0dd5 100644 --- a/contrib/llvm-project/clang/include/clang/Sema/Sema.h +++ b/contrib/llvm-project/clang/include/clang/Sema/Sema.h @@ -7315,6 +7315,16 @@ public: 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 diff --git a/contrib/llvm-project/clang/lib/Sema/SemaConcept.cpp b/contrib/llvm-project/clang/lib/Sema/SemaConcept.cpp index f24b549dd2ef..d1fa8e783122 100755 --- a/contrib/llvm-project/clang/lib/Sema/SemaConcept.cpp +++ b/contrib/llvm-project/clang/lib/Sema/SemaConcept.cpp @@ -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(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(Method->getParent()); } CXXThisScopeRAII ThisScope(*this, Record, ThisQuals, Record != nullptr); + + LambdaScopeForCallOperatorInstantiationRAII LambdaScope( + *this, const_cast(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(Decl), *MLTAL, Scope); llvm::SmallVector Converted; return CheckConstraintSatisfaction(Template, TemplateAC, Converted, *MLTAL, diff --git a/contrib/llvm-project/clang/lib/Sema/SemaDecl.cpp b/contrib/llvm-project/clang/lib/Sema/SemaDecl.cpp index 21b5781a71cd..fab2865ec5a1 100644 --- a/contrib/llvm-project/clang/lib/Sema/SemaDecl.cpp +++ b/contrib/llvm-project/clang/lib/Sema/SemaDecl.cpp @@ -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(D), *this); + RebuildLambdaScopeInfo(cast(D)); } else { // Enter a new function scope PushFunctionScope(); diff --git a/contrib/llvm-project/clang/lib/Sema/SemaExpr.cpp b/contrib/llvm-project/clang/lib/Sema/SemaExpr.cpp index 3a5e302cc03a..63b00d640a9c 100644 --- a/contrib/llvm-project/clang/lib/Sema/SemaExpr.cpp +++ b/contrib/llvm-project/clang/lib/Sema/SemaExpr.cpp @@ -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(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(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 diff --git a/contrib/llvm-project/clang/lib/Sema/SemaLambda.cpp b/contrib/llvm-project/clang/lib/Sema/SemaLambda.cpp index 06fc53591a76..ccc5111d1e31 100644 --- a/contrib/llvm-project/clang/lib/Sema/SemaLambda.cpp +++ b/contrib/llvm-project/clang/lib/Sema/SemaLambda.cpp @@ -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 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(FD)); +} diff --git a/contrib/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/contrib/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index f78d46f59503..332004055b58 100644 --- a/contrib/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/contrib/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2426,6 +2426,9 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl( cast(Owner)->isDefinedOutsideFunctionOrMethod()); LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); + Sema::LambdaScopeForCallOperatorInstantiationRAII LambdaScope( + SemaRef, const_cast(D), TemplateArgs, Scope); + // Instantiate enclosing template arguments for friends. SmallVector TempParamLists; unsigned NumTempParamLists = 0; diff --git a/contrib/llvm-project/clang/lib/Sema/TreeTransform.h b/contrib/llvm-project/clang/lib/Sema/TreeTransform.h index 097e81ea7d45..b51741d5e8b2 100644 --- a/contrib/llvm-project/clang/lib/Sema/TreeTransform.h +++ b/contrib/llvm-project/clang/lib/Sema/TreeTransform.h @@ -12285,7 +12285,16 @@ TreeTransform::TransformCXXNullPtrLiteralExpr( template ExprResult TreeTransform::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.