[llvm-branch-commits] [clang] release/20.x: [clang] Do not infer lifetimebound for functions with void return type (#131997) (PR #133998)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Apr 1 15:33:59 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (llvmbot)

<details>
<summary>Changes</summary>

Backport 65ee2813f9f9a8cd11c5e9ea372da7d12867b52f

Requested by: @<!-- -->cor3ntin

---
Full diff: https://github.com/llvm/llvm-project/pull/133998.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaAttr.cpp (+5) 
- (added) clang/test/Sema/GH126231.cpp (+18) 


``````````diff
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index 6907fa91e28c2..27b5eb5f2c773 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -14,6 +14,7 @@
 #include "CheckExprLifetime.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/Attr.h"
+#include "clang/AST/DeclCXX.h"
 #include "clang/AST/Expr.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Lex/Preprocessor.h"
@@ -219,6 +220,10 @@ void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) {
 void Sema::inferLifetimeBoundAttribute(FunctionDecl *FD) {
   if (FD->getNumParams() == 0)
     return;
+  // Skip void returning functions (except constructors). This can occur in
+  // cases like 'as_const'.
+  if (!isa<CXXConstructorDecl>(FD) && FD->getReturnType()->isVoidType())
+    return;
 
   if (unsigned BuiltinID = FD->getBuiltinID()) {
     // Add lifetime attribute to std::move, std::fowrard et al.
diff --git a/clang/test/Sema/GH126231.cpp b/clang/test/Sema/GH126231.cpp
new file mode 100644
index 0000000000000..d10fc79c3b628
--- /dev/null
+++ b/clang/test/Sema/GH126231.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -std=c++20 -Wno-ignored-attributes -Wno-unused-value -verify %s
+// expected-no-diagnostics
+namespace std {
+template <class T>
+constexpr const T& as_const(T&) noexcept;
+
+// We need two declarations to see the error for some reason.
+template <class T> void as_const(const T&&) noexcept = delete;
+template <class T> void as_const(const T&&) noexcept;
+}
+
+namespace GH126231 {
+
+void test() {
+    int a = 1;
+    std::as_const(a);
+}
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/133998


More information about the llvm-branch-commits mailing list