Skip to content

Clang 13-15 segfault during template instantiation with ill-formed nested type and RTTI operation #149479

@Dong-hui-li

Description

@Dong-hui-li

Description

Clang versions 13-15 crash with segmentation fault when compiling C++20 code that:

  1. Declares a local class with the same name as an outer scoped enum
  2. Attempts to initialize that class with an enum value
  3. Uses typeid operator on the invalid object

Steps to Reproduce

  1. Save the following code as crash.cpp:
#include <typeinfo>

struct scoped {
    enum class scoped2 { RED };
};

template<auto>
struct S {
    void f() {
        class scoped2 e = scoped::scoped2::RED;
        typeid(e);
    }
};

int main() {
    S<scoped::scoped2::RED>{}.f();
}
  1. Compile with affected Clang versions:
clang++-13 -std=c++20 crash.cpp
clang++-14 -std=c++20 crash.cpp
clang++-15 -std=c++20 crash.cpp

Expected Behavior

Compiler should report type errors without crashing.

Actual Behavior

Compiler crashes with segmentation fault during template instantiation.

Affected Versions

  • Clang 13.0.1
  • Clang 14.0.0
  • Clang 15.0.7

Environment

  • OS: Ubuntu 22.04 LTS
  • Architecture: x86_64

Notes

Clang 11 behaves correctly (reports errors without crashing).

Expected Behavior

Compiler should report type errors without crashing

Actual Behavior

Compiler crashes with segmentation fault during template instantiation

Affected Versions

  • Clang 13.0.1
  • Clang 14.0.0
  • Clang 15.0.7

Additional Info

  • Clang 11 behaves correctly (reports errors without crashing)
  • Crash occurs in Sema::SubstStmt and Sema::InstantiateFunctionDefinition
  • Stack trace and preprocessed files generated automatically by compiler

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions