[clang] [clang] Fix crash on invalid out-of-line enum definition with template parameters (PR #188246)
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 24 06:34:13 PDT 2026
https://github.com/hokein created https://github.com/llvm/llvm-project/pull/188246
clang previously crashed when an invalid out-of-line enum definition was provided with template parameters. In these cases, clang would produce a dependent type within a non-dependent context, violating internal invariants.
The fix is to fallback the underlying type of the enum to `int` during error recovery in `Sema::ActOnTag` when `Invalid` is true, making it safe for downstream processing while still preserving the invalid declaration in the AST.
Fixes #187909
>From 90cd853d477f99fd0ca4a05fcc4357d0209f6a4b Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Tue, 24 Mar 2026 14:10:52 +0100
Subject: [PATCH] [clang] Fix crash on invalid out-of-line enum definition with
template parameters
---
clang/lib/Sema/SemaDecl.cpp | 3 ++-
clang/test/SemaTemplate/GH187909.cpp | 10 ++++++++++
2 files changed, 12 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaTemplate/GH187909.cpp
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 52946e0ca3cb1..9e2d24486314b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -17966,7 +17966,8 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
bool IsFixed = !UnderlyingType.isUnset() || ScopedEnum;
if (Kind == TagTypeKind::Enum) {
- if (UnderlyingType.isInvalid() || (!UnderlyingType.get() && ScopedEnum)) {
+ if (UnderlyingType.isInvalid() || (!UnderlyingType.get() && ScopedEnum) ||
+ Invalid) {
// No underlying type explicitly specified, or we failed to parse the
// type, default to int.
EnumUnderlying = Context.IntTy.getTypePtr();
diff --git a/clang/test/SemaTemplate/GH187909.cpp b/clang/test/SemaTemplate/GH187909.cpp
new file mode 100644
index 0000000000000..05e61058d4390
--- /dev/null
+++ b/clang/test/SemaTemplate/GH187909.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+template<typename T> struct A {
+ enum E : T;
+ E v = E{};
+};
+
+template<typename T> enum A<int>::E : T { e1 }; // expected-error {{template parameter list matching the non-templated nested type 'A<int>' should be empty ('template<>')}}
+
+A<int> a;
More information about the cfe-commits
mailing list