[clang] [Clang] disallow selectany on non-global-variable declarations (PR #189641)
Martin Storsjö via cfe-commits
cfe-commits at lists.llvm.org
Fri May 8 03:58:58 PDT 2026
mstorsjo wrote:
> @mstorsjo @SquallATF, Thanks for reporting. To clarify, these changes were made after the spec clarification at [#189641 (comment)](https://github.com/llvm/llvm-project/pull/189641#discussion_r3146591253). As I understand it, the current behavior aligns with the clarified `selectany` rules. If we want to restore the previous, non-conforming behavior for compatibility with existing code, I can prepare a revert.
No, you are misunderstanding what it applies to.
The linked discussion (https://developercommunity.visualstudio.com/t/__declspecselectany-acceptance-across/11081724) is about when a static (or non-static) class member is declared - as part of that class. MSVC does reject that, as noted in the Compiler Explorer example linked from that issue (https://godbolt.org/z/5zr9q75eK).
But this is not about selectany on the declaration of the member in the class, it's about selectany on the concrete definition of that static member. MSVC does not diagnose on that. See https://godbolt.org/z/5hTc5r7cY for an updated version of that example, posted inline here as well:
```c++
#ifdef D
#define SA __declspec(selectany)
#else
#define SA [[gnu::selectany]]
#endif
struct S {
SA static int i;
SA int j;
};
struct T {
static int i;
};
SA int T::i = 42;
```
MSVC only diagnoses on the declaration of `SA static int i;` (and the non-static `SA int j;` as well - GCC also diagnoses that one).
But MSVC does not diagnose any error on `SA int T::i = 42;`, neither does GCC.
https://github.com/llvm/llvm-project/pull/189641
More information about the cfe-commits
mailing list