[clang] Do not infer lifetimebound for functions with void return type (#131997)

Fixes: https://github.com/llvm/llvm-project/issues/126231
Also found in : https://github.com/microsoft/STL/issues/5271
This commit is contained in:
Utkarsh Saxena
2025-03-24 17:42:33 +01:00
committed by GitHub
parent e2c5b95da1
commit 65ee2813f9
2 changed files with 23 additions and 0 deletions

View File

@@ -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.

View File

@@ -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);
}
}