[clang] [clang-tools-extra] [clang] Do not cache variable linkage before type deduction (PR #221474)

Vedran Miletić via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 12:50:53 PST 2026


vedranmiletic wrote:

> I don't think this fix is right. This PR avoids caching the linkage while the type is undeduced, but the real question to which we need the answer is: why is linkage being queried on an incomplete, undeduced declaration?

Thanks for the feedback, that's a fair point. I went through the places where the linkage gets queried before the type is deduced. It turns out three of them don't actually need it, so I removed the early query from each:

- `NamedDecl::isReserved` checks for C language linkage. As far as I understand, in C++, only a variable first declared in an `extern "C"` context can have it, so it now checks the context first.
- `isOutOfScopePreviousDeclaration` is the path from #22203 (auto g = [] { void g(); };). Whether the variable has linkage at all doesn't depend on its type, so for an undeduced variable it now decides that from the context and `[[clang::internal_linkage]]` attribute.
- langd code completion was also triggering it: looking up the documentation needs the symbol ID, and computing that computes the linkage. Declarations whose linkage isn't stable yet are now skipped there, like the other completion paths already do.

There's one case where I think asking early is legitimate: a lambda in the initializer of a variable in a C++20 module. Clang has to decide whether the lambda needs a mangling number, and for that it asks whether the variable is externally visible. It asks this while creating the lambda, so before auto is deduced. As a fallback, `getLVForDecl()` doesn't cache the linkage while the type is still auto. The new test in lambda-in-module-purview.cppm covers this.

I'm not sure that's the nicest way to handle that case, so if you have a better idea, e.g. a type-independent visibility check there, I'm happy to try it.

https://github.com/llvm/llvm-project/pull/221474


More information about the cfe-commits mailing list