Skip to content

[clang] Avoid inheriting [[noreturn]] in explicit function template specializations #150003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 24, 2025

Conversation

snarang181
Copy link
Contributor

@snarang181 snarang181 commented Jul 22, 2025

This patch fixes incorrect behavior in Clang where [[noreturn]] (either spelled or inferred) was being inherited by explicit specializations of function templates or member function templates, even when those specializations returned normally.

Follow up on #145166

@snarang181 snarang181 changed the title [Sema] Avoid attaching 'noreturn' attribute to explicit function temp… [NFC][clang] Avoid inheriting [[noreturn]] in explicit function template specializations Jul 22, 2025
@snarang181 snarang181 marked this pull request as ready for review July 22, 2025 13:17
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 22, 2025

@llvm/pr-subscribers-clang

Author: Samarth Narang (snarang181)

Changes

This patch fixes incorrect behavior in Clang where [[noreturn]] (either spelled or inferred) was being inherited by explicit specializations of function templates or member function templates, even when those specializations returned normally.

Follow up on #145166


Full diff: https://github.com/llvm/llvm-project/pull/150003.diff

3 Files Affected:

  • (modified) clang/lib/Sema/SemaDecl.cpp (+8)
  • (modified) clang/lib/Sema/SemaDeclAttr.cpp (+3)
  • (modified) clang/test/SemaCXX/wreturn-always-throws.cpp (+15)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 14403e65e8f42..bb412ef6788e7 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -3267,6 +3267,14 @@ void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old,
     if (isa<UsedAttr>(I) || isa<RetainAttr>(I))
       continue;
 
+    if (isa<InferredNoReturnAttr>(I)) {
+      if (auto *FD = dyn_cast<FunctionDecl>(New)) {
+        if (FD->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
+          continue; // Don't propagate inferred noreturn attributes to explicit
+                    // specializations.
+      }
+    }
+
     if (mergeDeclAttribute(*this, New, I, LocalAMK))
       foundAny = true;
   }
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 78f4804202ddc..a61d10c166578 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -1970,6 +1970,9 @@ void clang::inferNoReturnAttr(Sema &S, const Decl *D) {
   if (!FD)
     return;
 
+  if (FD->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
+    return; // Don't infer noreturn for explicit specializations.
+
   auto *NonConstFD = const_cast<FunctionDecl *>(FD);
   DiagnosticsEngine &Diags = S.getDiagnostics();
   if (Diags.isIgnored(diag::warn_falloff_nonvoid, FD->getLocation()) &&
diff --git a/clang/test/SemaCXX/wreturn-always-throws.cpp b/clang/test/SemaCXX/wreturn-always-throws.cpp
index addcadd1183dc..c307bc675c14b 100644
--- a/clang/test/SemaCXX/wreturn-always-throws.cpp
+++ b/clang/test/SemaCXX/wreturn-always-throws.cpp
@@ -44,3 +44,18 @@ void testTemplates() {
   throwErrorTemplate("ERROR");
   (void)ensureZeroTemplate(42);
 }
+
+// Ensure that explicit specialization of a member function does not inherit
+// the warning from the primary template.
+
+template<typename T>
+struct S {
+  void f();
+};
+
+template<typename T>
+void S<T>::f() { throw 0; } 
+template<>
+void S<int>::f() {} // expected-no-diagnostics
+
+

@zwuis zwuis changed the title [NFC][clang] Avoid inheriting [[noreturn]] in explicit function template specializations [clang] Avoid inheriting [[noreturn]] in explicit function template specializations Jul 22, 2025
@zwuis
Copy link
Contributor

zwuis commented Jul 22, 2025

I think we need to test partial specialization as well.

Copy link
Contributor

@alexfh alexfh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

Add -Winvalid-noreturn diagnostic in LIT test
@snarang181 snarang181 force-pushed the fix-always-throwing-return-templates branch from a2125f5 to 4db6e19 Compare July 22, 2025 23:17
@snarang181 snarang181 requested a review from alexfh July 22, 2025 23:18
Copy link
Contributor

@alexfh alexfh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good now.

We might still want to revisit the original patch though (#145166 (comment)).

@alexfh alexfh merged commit 22fef00 into llvm:main Jul 24, 2025
9 checks passed
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
…pecializations (llvm#150003)

This patch fixes incorrect behavior in Clang where [[noreturn]] (either
spelled or inferred) was being inherited by explicit specializations of
function templates or member function templates, even when those
specializations returned normally.

Follow up on llvm#145166
@mstorsjo
Copy link
Member

mstorsjo commented Aug 1, 2025

This fixes cases that are broken on the 21.x release branch - see #151742. Therefore, this should probably be backported there.

@mstorsjo
Copy link
Member

mstorsjo commented Aug 1, 2025

/cherry-pick 22fef00

@llvmbot
Copy link
Member

llvmbot commented Aug 1, 2025

/pull-request #151752

@llvmbot llvmbot moved this from Needs Triage to Done in LLVM Release Status Aug 1, 2025
snarang181 added a commit that referenced this pull request Aug 4, 2025
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Aug 4, 2025
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 5, 2025
…pecializations (llvm#150003)

This patch fixes incorrect behavior in Clang where [[noreturn]] (either
spelled or inferred) was being inherited by explicit specializations of
function templates or member function templates, even when those
specializations returned normally.

Follow up on llvm#145166

(cherry picked from commit 22fef00)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
Development

Successfully merging this pull request may close these issues.

6 participants