Otherwise these functions are not instantiated and we end up with an undefined
symbol.
Fix #55560
Differential D128119
[clang] Enforce instantiation of constexpr template functions during non-constexpr evaluation serge-sans-paille on Jun 18 2022, 4:51 AM. Authored by
Details
Otherwise these functions are not instantiated and we end up with an undefined Fix #55560
Diff Detail
Event TimelineComment Actions I"m not 100% sure of the fix but it fixes bug #55560 and does not introduce regression :-/ Comment Actions It's a bummer that patch application failed on precommit CI for reasons unrelated to your patch (as best I can tell, anyway)... Also, please update the patch summary to have some more details about what your changing and why.
Comment Actions During the parsing of the assignment in main for the constexpr case we end up in Sema::MarkFunctionReferenced in particular this code: else if (Func->isConstexpr()) // Do not defer instantiations of constexpr functions, to avoid the // expression evaluator needing to call back into Sema if it sees a // call to such a function. InstantiateFunctionDefinition(PointOfInstantiation, Func); And so I experimented with modifying Sema::InstantiateFunctionDefinition in particular: else if (TSK == TSK_ExplicitInstantiationDefinition) { And modified in this way: else if (TSK == TSK_ExplicitInstantiationDefinition || (Function->isConstexpr() && !Recursive)) { This seems more targeted, reflects what I think is the intent and passes check-clang as well. I also looked at the non-constexpr case: template <typename T> T foo(T a); int main() { int k = foo<int>(5); } template <typename T> T foo(T a) { return a; } And in this case we hit Sema::InstantiateFunctionDefinition from Sema::PerformPendingInstantiations which is called by Sema::ActOnEndOfTranslationUnitFragment and so by this time we have seen the definition already. Comment Actions Pinging about updates to this, because if this really does fix https://github.com/llvm/llvm-project/issues/55560, I think we want to get that fix into Clang 15. The release branch point is coming up pretty soon (https://discourse.llvm.org/t/llvm-15-0-0-release-schedule/63495/10). Comment Actions @shafik: I've implemented your patch which indeed looks closer to the original intent |