Clang error: static assertion failed due to requirement 'A'... (err_static_assert_requirement_failed)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error: static assertion failed due to requirement 'A'
: C
 

(since 15.0)

error: static_assert failed due to requirement 'A'
C
 

(6.0 - 14.0)

Type Error
Category Semantic Issue (since 6.0)
Internal Id err_static_assert_requirement_failed (since 6.0)
Internal Message static assertion failed due to requirement '%0'%select{: %2|}1 (since 15.0)
static_assert failed due to requirement '%0'%select{ %2|}1 (6.0 - 14.0)
Regular Expression (?:error|fatal error)\: static assertion failed due to requirement '(.*?)'(?:\: (.*?)|)
First Commit 2017-09-15 672281a5116d Diagnostic specific failed condition in a static_assert.

Description

The error is issued by the Clang compiler when a static assertion fails. Static assertions are compile-time checks that ensure certain conditions are true. If the condition specified in a static assertion is evaluated as false, this error is generated to indicate the unmet requirement specified in the assertion. Static assertions can be used to validate template arguments, check properties of types, or enforce constraints that must be satisfied for correct program execution. When the assertion fails, it means the program violates one or more of these constraints, leading to this error message. The error message details the specific requirement that was not met and may include a custom message if provided in the static assertion.  
AI Generated

Example

In the following example, a static assertion is used to verify whether the C++ type float is considered an integral type by the standard library type trait std::is_integral. This check is performed through the variable template std::is_integral_v, which offers a simplified syntax over using the trait's ::value member. The condition being asserted is that std::is_integral_v<float> must evaluate to true for the compilation to succeed without errors. However, since float is a floating-point type, not an integral type, this condition evaluates to false. Consequently, the static assertion fails, triggering a compilation error. This error includes the failed requirement 'std::is_integral_v<float>', alongside a custom message provided in the static assertion statement, indicating that float is not an integral type. This showcases the utility of static assertions for enforcing type traits constraints at compile time, ensuring that certain characteristics of types are as expected.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
#include <type_traits>

static_assert(std::is_integral_v<float>, "float is not an integral type");
Compiler Output
<source>:3:15: error: static assertion failed due to requirement 'std::is_integral_v<float>': float is not an integral type


Clang Internals (17.0.6)

Git Commit Message

Diagnostic specific failed condition in a static_assert.

When a static_assert fails, dig out a specific condition to diagnose,
using the same logic that we use to find the enable_if condition to
diagnose.

llvm-svn: 313315

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaDeclCXX.cpp (line 17149)

Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, Expr *AssertExpr, Expr *AssertMessage, SourceLocation RParenLoc, bool Failed) {
  // ...
  if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent() && (!AssertMessage || (!AssertMessage->isTypeDependent() && !AssertMessage->isValueDependent())) && !Failed) {
    // ...
    if (!Failed && !Cond && !InTemplateDefinition) {
      // ...
      if (InnerCond && isa<ConceptSpecializationExpr>(InnerCond)) {
      // ...
      } else if (InnerCond && !isa<CXXBoolLiteralExpr>(InnerCond) && !isa<IntegerLiteral>(InnerCond)) {
        Diag(InnerCond->getBeginLoc(), diag::err_static_assert_requirement_failed) << InnerCondDescription << !HasMessage << Msg.str() << InnerCond->getSourceRange();

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p3.cpp

  • clang/test/CXX/dcl.decl/dcl.meaning/dcl.array/p3.cpp:101:19: error: static assertion failed due to requirement 'sizeof (arr2) == 13':