Skip to content

Commit 53141e4

Browse files
usx95tstellar
authored andcommitted
[clang] Do not infer lifetimebound for functions with void return type (llvm#131997)
Fixes: llvm#126231 Also found in : microsoft/STL#5271 (cherry picked from commit 65ee281)
1 parent cf7bb13 commit 53141e4

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

clang/lib/Sema/SemaAttr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CheckExprLifetime.h"
1515
#include "clang/AST/ASTConsumer.h"
1616
#include "clang/AST/Attr.h"
17+
#include "clang/AST/DeclCXX.h"
1718
#include "clang/AST/Expr.h"
1819
#include "clang/Basic/TargetInfo.h"
1920
#include "clang/Lex/Preprocessor.h"
@@ -219,6 +220,10 @@ void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) {
219220
void Sema::inferLifetimeBoundAttribute(FunctionDecl *FD) {
220221
if (FD->getNumParams() == 0)
221222
return;
223+
// Skip void returning functions (except constructors). This can occur in
224+
// cases like 'as_const'.
225+
if (!isa<CXXConstructorDecl>(FD) && FD->getReturnType()->isVoidType())
226+
return;
222227

223228
if (unsigned BuiltinID = FD->getBuiltinID()) {
224229
// Add lifetime attribute to std::move, std::fowrard et al.

clang/test/Sema/GH126231.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_cc1 -std=c++20 -Wno-ignored-attributes -Wno-unused-value -verify %s
2+
// expected-no-diagnostics
3+
namespace std {
4+
template <class T>
5+
constexpr const T& as_const(T&) noexcept;
6+
7+
// We need two declarations to see the error for some reason.
8+
template <class T> void as_const(const T&&) noexcept = delete;
9+
template <class T> void as_const(const T&&) noexcept;
10+
}
11+
12+
namespace GH126231 {
13+
14+
void test() {
15+
int a = 1;
16+
std::as_const(a);
17+
}
18+
}

0 commit comments

Comments
 (0)