[Clang] Fix an incorrect assertion in `Sema::CheckAddressOfOperand` (#159314)
Not all non-type template arguments are modeled as
NonTypeTemplateParmDecl.
Fixes #151531
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 518ed9e..80f4e83 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -396,6 +396,8 @@
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 73b16ae..03def26 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -14725,8 +14725,9 @@
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 56ceb7a..8450ff0 100644
--- a/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp
+++ b/clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp
@@ -371,3 +371,18 @@
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>();
+}
+}