Skip to content

Commit bcbe9d6

Browse files
authored
[Clang] Do not allow [[clang::lifetimebound]] on explicit object member functions (#96113)
Previously, `[[clang::lifetimebound]]` applied to an explicit object member function did nothing and was silently ignored. Now issue the error diagnostic `'lifetimebound' attribute cannot be applied; explicit object member function has no implicit object parameter`
1 parent de5ea2d commit bcbe9d6

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ Attribute Changes in Clang
193193
- The ``hybrid_patchable`` attribute is now supported on ARM64EC targets. It can be used to specify
194194
that a function requires an additional x86-64 thunk, which may be patched at runtime.
195195

196+
- ``[[clang::lifetimebound]]`` is now explicitly disallowed on explicit object member functions
197+
where they were previously silently ignored.
198+
196199
Improvements to Clang's diagnostics
197200
-----------------------------------
198201

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10066,8 +10066,9 @@ def warn_null_ret : Warning<
1006610066
InGroup<NonNull>;
1006710067

1006810068
def err_lifetimebound_no_object_param : Error<
10069-
"'lifetimebound' attribute cannot be applied; %select{static |non-}0member "
10070-
"function has no implicit object parameter">;
10069+
"'lifetimebound' attribute cannot be applied; "
10070+
"%select{non-|static |explicit object }0"
10071+
"member function has no implicit object parameter">;
1007110072
def err_lifetimebound_ctor_dtor : Error<
1007210073
"'lifetimebound' attribute cannot be applied to a "
1007310074
"%select{constructor|destructor}0">;

clang/lib/Sema/SemaDecl.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6939,9 +6939,16 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
69396939
// by applying it to the function type.
69406940
if (const auto *A = ATL.getAttrAs<LifetimeBoundAttr>()) {
69416941
const auto *MD = dyn_cast<CXXMethodDecl>(FD);
6942-
if (!MD || MD->isStatic()) {
6942+
int NoImplicitObjectError = -1;
6943+
if (!MD)
6944+
NoImplicitObjectError = 0;
6945+
else if (MD->isStatic())
6946+
NoImplicitObjectError = 1;
6947+
else if (MD->isExplicitObjectMemberFunction())
6948+
NoImplicitObjectError = 2;
6949+
if (NoImplicitObjectError != -1) {
69436950
S.Diag(A->getLocation(), diag::err_lifetimebound_no_object_param)
6944-
<< !MD << A->getRange();
6951+
<< NoImplicitObjectError << A->getRange();
69456952
} else if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) {
69466953
S.Diag(A->getLocation(), diag::err_lifetimebound_ctor_dtor)
69476954
<< isa<CXXDestructorDecl>(MD) << A->getRange();

clang/test/SemaCXX/attr-lifetimebound.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -std=c++2a -verify %s
1+
// RUN: %clang_cc1 -std=c++23 -verify %s
22

33
namespace usage_invalid {
44
// FIXME: Should we diagnose a void return type?
@@ -9,6 +9,7 @@ namespace usage_invalid {
99
A() [[clang::lifetimebound]]; // expected-error {{cannot be applied to a constructor}}
1010
~A() [[clang::lifetimebound]]; // expected-error {{cannot be applied to a destructor}}
1111
static int *static_class_member() [[clang::lifetimebound]]; // expected-error {{static member function has no implicit object parameter}}
12+
int *explicit_object(this A&) [[clang::lifetimebound]]; // expected-error {{explicit object member function has no implicit object parameter}}
1213
int not_function [[clang::lifetimebound]]; // expected-error {{only applies to parameters and implicit object parameters}}
1314
int [[clang::lifetimebound]] also_not_function; // expected-error {{cannot be applied to types}}
1415
};

0 commit comments

Comments
 (0)