llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

lifetime_capture_by conflicts with user-defined identifiers

Open AaronBallman opened this issue 7 months ago • 9 comments

Consider:

struct S {
  void foo(int global) [[clang::lifetime_capture_by(global)]] {
  }
};

This code is rejected with parameter cannot be named 'global' while using 'lifetime_capture_by(global)': https://godbolt.org/z/YWhW1dnoe

The usual approach of adding underscores doesn't save you: https://godbolt.org/z/9oeMoPWfK

I think 1) the above code should be accepted with the global parameter being used to denote the capture, though a warning would be reasonable, 2) we should allow optional leading & trailing underscores so library authors have a way to protect against things like:

#define global 12
#include "some_header.h" // has a declaration using  [[clang::lifetime_capture_by(global)]]

AaronBallman avatar Apr 14 '26 18:04 AaronBallman

@llvm/issue-subscribers-clang-temporal-safety

Author: Aaron Ballman (AaronBallman)

Consider: ``` struct S { void foo(int global) [[clang::lifetime_capture_by(global)]] { } }; ``` This code is rejected with `parameter cannot be named 'global' while using 'lifetime_capture_by(global)'`: https://godbolt.org/z/YWhW1dnoe

The usual approach of adding underscores doesn't save you: https://godbolt.org/z/9oeMoPWfK

I think 1) the above code should be accepted with the global parameter being used to denote the capture, though a warning would be reasonable, 2) we should allow optional leading & trailing underscores so library authors have a way to protect against things like:

#define global 12
#include "some_header.h" // has a declaration using  [[clang::lifetime_capture_by(global)]]

llvmbot avatar Apr 14 '26 18:04 llvmbot

@llvm/issue-subscribers-clang-frontend

Author: Aaron Ballman (AaronBallman)

Consider: ``` struct S { void foo(int global) [[clang::lifetime_capture_by(global)]] { } }; ``` This code is rejected with `parameter cannot be named 'global' while using 'lifetime_capture_by(global)'`: https://godbolt.org/z/YWhW1dnoe

The usual approach of adding underscores doesn't save you: https://godbolt.org/z/9oeMoPWfK

I think 1) the above code should be accepted with the global parameter being used to denote the capture, though a warning would be reasonable, 2) we should allow optional leading & trailing underscores so library authors have a way to protect against things like:

#define global 12
#include "some_header.h" // has a declaration using  [[clang::lifetime_capture_by(global)]]

llvmbot avatar Apr 14 '26 18:04 llvmbot

Is it too late to change the design here? We shouldn't allow a context-sensitive "keyword" in the same grammatical position where we also accept an identifier.

We could replace the existing [[clang::lifetime_capture_by(global)]] and [[clang::lifetime_capture_by(unknown)]] with [[clang::lifetime_capture_by_global]] and [[clang::lifetime_capture_by_unknown]], for example, and could even do that with a deprecation period if needed. But maybe there's a better option.

zygoloid avatar Apr 14 '26 21:04 zygoloid

Is it too late to change the design here? We shouldn't allow a context-sensitive "keyword" in the same grammatical position where we also accept an identifier.

I hope not, because I think there are other design issues with these attributes too.

We could replace the existing [[clang::lifetime_capture_by(global)]] and [[clang::lifetime_capture_by(unknown)]] with [[clang::lifetime_capture_by_global]] and [[clang::lifetime_capture_by_unknown]], for example, and could even do that with a deprecation period if needed. But maybe there's a better option.

+1 to this, and while we're at it, [[clang::lifetime_capture_by_this]]; all of these should be declaration attributes, not type attributes.

CC @usx95

AaronBallman avatar Apr 15 '26 11:04 AaronBallman

Ping @usx95, this is still concerning.

AaronBallman avatar May 05 '26 13:05 AaronBallman

Ack. I can prioritise this with a deprecation period.

all of these should be declaration attributes, not type attributes.

We still need to be able to annotate function type (similar to lifetimebound). struct S { void foo() [[clang::lifetimebound]]; };

cc: @NeKon69

usx95 avatar May 07 '26 20:05 usx95

OK, after taking a deeper look at this, it looks kinda complex, I can try to work on this, but this looks more like a Sema change rather than a LifetimeSafety one, so this still might take a bit for me.

NeKon69 avatar May 07 '26 21:05 NeKon69

Ack. I can prioritise this with a deprecation period.

Thank you!

all of these should be declaration attributes, not type attributes.

We still need to be able to annotate function type (similar to lifetimebound). struct S { void foo() [[clang::lifetimebound]]; };

I do not see the argument why that's necessary. I think lifetime_capture_by_this is a significant improvement (the attribute has no type semantics, it seemingly only exists as such because of the desire to associate the annotation with the implicit this, but that doesn't require a type attribute to support).

AaronBallman avatar May 08 '26 13:05 AaronBallman

all of these should be declaration attributes, not type attributes.

We still need to be able to annotate function type (similar to lifetimebound). struct S { void foo() [[clang::lifetimebound]]; };

I do not see the argument why that's necessary. I think lifetime_capture_by_this is a significant improvement (the attribute has no type semantics, it seemingly only exists as such because of the desire to associate the annotation with the implicit this, but that doesn't require a type attribute to support).

I see. I was not aware that we can support annotating ImplicitObjectParameter without being a TypeAttr. IIUC this is what you suggest: https://github.com/llvm/llvm-project/pull/196549.

usx95 avatar May 08 '26 15:05 usx95