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

[LifetimeSafety] Avoid suggesting lifetimebound on primary templates

Open NeKon69 opened this issue 6 months ago • 2 comments

After #197753, we suggest adding lifetimebound to a primary template when the annotation appears on an explicit specialization.

This can be misleading because adding the attribute to the primary template affects all instantiations/specializations, even if only one specialization needs the annotation.

We should either avoid making this suggestion in these cases, or add a more specific diagnostic/suggestion for explicit specializations.

template <typename T>
MyObj &spec_func(T &obj); // suggests adding lifetimbound attribute here!
template <>
MyObj &spec_func<MyObj>(MyObj &obj [[clang::lifetimebound]]);
template <>
MyObj &spec_func<MyObj>(MyObj &obj) { return obj; }

NeKon69 avatar May 19 '26 20:05 NeKon69

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

Author: NeKon69

After #197753, we suggest adding `lifetimebound` to a primary template when the annotation appears on an explicit specialization.

This can be misleading because adding the attribute to the primary template affects all instantiations/specializations, even if only one specialization needs the annotation.

We should either avoid making this suggestion in these cases, or add a more specific diagnostic/suggestion for explicit specializations.

template &lt;typename T&gt;
MyObj &amp;spec_func(T &amp;obj); // suggests adding lifetimbound attribute here!
template &lt;&gt;
MyObj &amp;spec_func&lt;MyObj&gt;(MyObj &amp;obj [[clang::lifetimebound]]);
template &lt;&gt;
MyObj &amp;spec_func&lt;MyObj&gt;(MyObj &amp;obj) { return obj; }

I have a fix for this, will create a PR soon.

NeKon69 avatar Jun 15 '26 09:06 NeKon69