Skip to content

Commit

Permalink
[Clang] Implement CWG2518 - static_assert(false)
Browse files Browse the repository at this point in the history
This allows `static_assert(false)` to not be ill-formed
in template definitions.

This change is applied as a DR in all C++ modes.

Of notes, a couple of tests were relying of the eager nature
of static_assert

* test/SemaTemplate/instantiation-dependence.cpp
* test/SemaTemplate/instantiate-var-template.cpp

I don't know if the changes to `static_assert`
still allow that sort of tests to be expressed.

Reviewed By: #clang-language-wg, erichkeane, aaron.ballman

Differential Revision: https://reviews.llvm.org/D144285

---

Fixes the build with newer versions of MSVC's STL, reported as
root-project#15321

(cherry picked from commit c767271)
  • Loading branch information
cor3ntin authored and hahnjo committed Jun 5, 2024
1 parent 450b24e commit 225de3d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions interpreter/llvm-project/clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ sections with improvements to Clang's support for those languages.

C++ Language Changes
--------------------
- Implemented `CWG2518 <https://wg21.link/CWG2518>`_ which allows ``static_assert(false)``
to not be ill-formed when its condition is evaluated in the context of a template definition.

C++20 Feature Support
^^^^^^^^^^^^^^^^^^^^^
Expand Down
12 changes: 10 additions & 2 deletions interpreter/llvm-project/clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16802,7 +16802,14 @@ Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc,
FoldKind).isInvalid())
Failed = true;

if (!Failed && !Cond) {
// CWG2518
// [dcl.pre]/p10 If [...] the expression is evaluated in the context of a
// template definition, the declaration has no effect.
bool InTemplateDefinition =
getLangOpts().CPlusPlus && CurContext->isDependentContext();

if (!Failed && !Cond && !InTemplateDefinition) {

SmallString<256> MsgBuffer;
llvm::raw_svector_ostream Msg(MsgBuffer);
if (AssertMessage) {
Expand Down Expand Up @@ -16833,7 +16840,8 @@ Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc,
DiagnoseStaticAssertDetails(InnerCond);
} else {
Diag(StaticAssertLoc, diag::err_static_assert_failed)
<< !AssertMessage << Msg.str() << AssertExpr->getSourceRange();
<< !AssertMessage << Msg.str() << AssertExpr->getSourceRange();
PrintContextStack();
}
Failed = true;
}
Expand Down
2 changes: 1 addition & 1 deletion interpreter/llvm-project/clang/www/cxx_dr_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -14915,7 +14915,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td><a href="https://wg21.link/cwg2518">2518</a></td>
<td>review</td>
<td>Conformance requirements and <TT>#error</TT>/<TT>#warning</TT></td>
<td align="center">Not resolved</td>
<td class="unreleased" align="center">Clang 17</td>
</tr>
<tr class="open" id="2519">
<td><a href="https://wg21.link/cwg2519">2519</a></td>
Expand Down
2 changes: 1 addition & 1 deletion interpreter/llvm-project/llvm-project.tag
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ROOT-llvm16-20240402-01
ROOT-llvm16-20240604-01

0 comments on commit 225de3d

Please sign in to comment.