Clang error: cannot catch variably modified type A (err_catch_variably_modified)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: cannot catch variably modified type A
Type Error
Category Semantic Issue
Internal Id err_catch_variably_modified
Internal Message cannot catch variably modified type %0
Regular Expression (?:error|fatal error)\: cannot catch variably modified type (.*?)
First Commit 2016-06-08 e56d1a0d5000 [Sema] Don't permit catching variably modified types

Description

The error is issued by the Clang compiler when an attempt is made to catch an exception of a variably modified type. A variably modified type (VMT) refers to any type that is or contains a variable length array (VLA) or a pointer to a VLA, where the size of the array depends on a value determined at runtime. Since the size and layout of such types are not fixed at compile time, attempting to use them in a catch clause violates C++ language rules, which expect types in catch clauses to be known and fixed at compile time. This restriction ensures that the exception handling mechanism can correctly allocate storage and copy the thrown exception object into a suitable location where it can be caught and handled.  
AI Generated

Example

In the following example, an attempt is made to catch an exception using a variably modified type, specifically a pointer to a variable length array whose size is determined at runtime based on the value of n. The program begins by declaring a variable n and initializing it with a value of 10. Subsequently, a typedef creates a as a pointer to an array of int with n elements, making a a variably modified type since its size depends on the runtime value of n. In the try block, an exception is thrown. The catch clause attempts to catch this exception with the variably modified type a, which leads to the error because C++ standards require that types used in catch clauses have a known and fixed size at compile time. This is to ensure that the exception handling mechanism can allocate storage and manage the exception object properly, which is not feasible with a type whose size is not known until runtime.  
AI Generated


Flags -xc++ -fexceptions

[Try out in Compiler Explorer]

Source
int main() {
    int n = 10; // n modifies type variably

    typedef int (*a)[n]; // a is a pointer to a VLA

    try {
        throw 20; // Throwing exception
    } catch(a) { // Cannot catch a VLA pointer
        // handle
    }
    return 0;
}
Compiler Output
<source>:8:14: error: cannot catch variably modified type 'a' (aka 'int (*)[n]')


Clang Internals (17.0.6)

Git Commit Message

[Sema] Don't permit catching variably modified types

Variably modified types shouldn't be permitted in catch clauses.

This fixes PR28047.

llvm-svn: 272159

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 16627)

/// Perform semantic analysis for the variable declaration that
/// occurs within a C++ catch clause, returning the newly-created
/// variable.
VarDecl *Sema::BuildExceptionDeclaration(Scope *S, TypeSourceInfo *TInfo, SourceLocation StartLoc, SourceLocation Loc, IdentifierInfo *Name) {
  // ...
  if (ExDeclType->isVariablyModifiedType()) {
    Diag(Loc, diag::err_catch_variably_modified) << ExDeclType;

Triggered in Clang Tests

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

clang/test/SemaCXX/exceptions.cpp

  • clang/test/SemaCXX/exceptions.cpp:281:17: error: cannot catch variably modified type 'int (*)[i]'
  • clang/test/SemaCXX/exceptions.cpp:287:17: error: cannot catch variably modified type 'int (*)[i]'