[clang] [Clang] Fix an incorrect assertion in `Sema::CheckAddressOfOperand` (PR #159314)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 17 03:22:57 PDT 2025


https://github.com/cor3ntin created https://github.com/llvm/llvm-project/pull/159314

Not all non-type template arguments are modeled as NonTypeTemplateParmDecl.

Fixes #151531

>From 7959f5ef8d3efb7bf742443454255420fa6b5a53 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Wed, 17 Sep 2025 12:20:50 +0200
Subject: [PATCH] [Clang] Fix an incorrect assertion in
 `Sema::CheckAddressOfOperand`

Not all non-type template arguments are modeled as
NonTypeTemplateParmDecl.

Fixes #151531
---
 clang/docs/ReleaseNotes.rst                       |  2 ++
 clang/lib/Sema/SemaExpr.cpp                       |  5 +++--
 .../test/SemaTemplate/temp_arg_nontype_cxx20.cpp  | 15 +++++++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f184eb1068f76..14f652c49c5b8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -379,6 +379,8 @@ Bug Fixes to C++ Support
   the function type.
 - Fix an assertion failure when a ``constexpr`` variable is only referenced through
   ``__builtin_addressof``, and related issues with builtin arguments. (#GH154034)
+- Fix an assertion failure when taking the address on a non-type template parameter argument of
+  object type. (#GH151531)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 73b16ae09e922..03def26fe53bd 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -14725,8 +14725,9 @@ QualType Sema::CheckAddressOfOperand(ExprResult &OrigOp, SourceLocation OpLoc) {
           return MPTy;
         }
       }
-    } else if (!isa<FunctionDecl, NonTypeTemplateParmDecl, BindingDecl,
-                    MSGuidDecl, UnnamedGlobalConstantDecl>(dcl))
+    } else if (!isa<FunctionDecl, TemplateParamObjectDecl,
+                    NonTypeTemplateParmDecl, BindingDecl, MSGuidDecl,
+                    UnnamedGlobalConstantDecl>(dcl))
       llvm_unreachable("Unknown/unexpected decl type");
   }
 
diff --git a/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp b/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp
index 56ceb7af4ccd9..8450ff037e184 100644
--- a/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp
+++ b/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp
@@ -371,3 +371,18 @@ namespace ReportedRegression2 {
     fn<str>();
   }
 }
+
+namespace GH151531 {
+struct w {
+    int n;
+};
+
+template <const w *X> void f() { static_assert(X->n == 42); }
+
+template <w X> void g() { f<&X>(); }
+
+void test() {
+    constexpr w X = {42};
+    g<X>();
+}
+}



More information about the cfe-commits mailing list