Skip to content

Commit d6b6598

Browse files
thebrandrecor3ntin
andauthored
[clang] Fix implicit integer conversion for opaque enums declared in class templates (#121039)
This commit fixes issues with enumeration types instantiated from an opaque-enum-declarations (see [dcl.enum]) in class templates broke basic assumptions during parsing of arithmetic expressions due to absent (NULL TYPE) promotion types of instances of EnumDecl. To this end, we repeat the simple steps in `Sema::ActOnTag` to evaluate the promotion type of a fixed enumeration based on its underlying type (see C++11 [conv.prom] p4). Note that if, instead, a full *enum-specifier* (subsequent curly braces) is provided, `Sema::ActOnEnumBody` is re-invoked on template instantiation anyway overriding the promotion type and hiding the issue. This is analog to how enumerations declarations outside of template declarations are handled. Note that, in contrast to `Sema::ActOnEnumBody`, `Sema::ActOnTag` is *not* called again for the instantiated enumeration type. Fixes #117960. --------- Co-authored-by: cor3ntin <[email protected]>
1 parent 35a3925 commit d6b6598

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed

clang/docs/ReleaseNotes.rst

+3
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,9 @@ Miscellaneous Clang Crashes Fixed
958958
- Fixed internal assertion firing when a declaration in the implicit global
959959
module is found through ADL. (GH#109879)
960960

961+
- Fixed a crash when an unscoped enumeration declared by an opaque-enum-declaration within a class template
962+
with a dependent underlying type is subject to integral promotion. (#GH117960)
963+
961964
OpenACC Specific Changes
962965
------------------------
963966

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,19 @@ Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
15711571
Enum->setIntegerType(SemaRef.Context.IntTy);
15721572
else
15731573
Enum->setIntegerTypeSourceInfo(NewTI);
1574+
1575+
// C++23 [conv.prom]p4
1576+
// if integral promotion can be applied to its underlying type, a prvalue
1577+
// of an unscoped enumeration type whose underlying type is fixed can also
1578+
// be converted to a prvalue of the promoted underlying type.
1579+
//
1580+
// FIXME: that logic is already implemented in ActOnEnumBody, factor out
1581+
// into (Re)BuildEnumBody.
1582+
QualType UnderlyingType = Enum->getIntegerType();
1583+
Enum->setPromotionType(
1584+
SemaRef.Context.isPromotableIntegerType(UnderlyingType)
1585+
? SemaRef.Context.getPromotedIntegerType(UnderlyingType)
1586+
: UnderlyingType);
15741587
} else {
15751588
assert(!D->getIntegerType()->isDependentType()
15761589
&& "Dependent type without type source info");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
// RUN: %clang_cc1 -std=c++11 -Wredeclared-class-member -Wconstant-conversion -Wdeprecated-declarations -Wc++11-narrowing -fsyntax-only %s -verify
2+
// RUN: %clang_cc1 -std=c++14 -Wredeclared-class-member -Wconstant-conversion -Wdeprecated-declarations -Wc++11-narrowing -fsyntax-only %s -verify
3+
// RUN: %clang_cc1 -std=c++20 -Wredeclared-class-member -Wconstant-conversion -Wdeprecated-declarations -Wc++11-narrowing -fsyntax-only %s -verify
4+
5+
// Test that opaque-enum-declarations are handled correctly w.r.t integral promotions.
6+
// The key sections in the C++11 standard are:
7+
// C++11 [dcl.enum]p3: An enumeration declared by an opaque-enum-declaration
8+
// has a fixed underlying type and is a complete type.
9+
// C++11 [conv.prom]: A prvalue of an unscoped enumeration type whose underlying type
10+
// is fixed ([dcl.enum]) can be converted to a prvalue of its underlying type.
11+
12+
// This program causes clang 19 and earlier to crash because
13+
// EnumDecl::PromotionType has not been set on the instantiated enum.
14+
// See GitHub Issue #117960.
15+
namespace Issue117960 {
16+
template <typename T>
17+
struct A {
18+
enum E : T;
19+
};
20+
21+
int b = A<int>::E{} + 0;
22+
}
23+
24+
25+
namespace test {
26+
template <typename T1, typename T2>
27+
struct IsSame {
28+
static constexpr bool check() { return false; }
29+
};
30+
31+
template <typename T>
32+
struct IsSame<T, T> {
33+
static constexpr bool check() { return true; }
34+
};
35+
} // namespace test
36+
37+
38+
template <typename T>
39+
struct S1 {
40+
enum E : T;
41+
};
42+
// checks if EnumDecl::PromotionType is set
43+
int X1 = S1<int>::E{} + 0;
44+
int Y1 = S1<unsigned>::E{} + 0;
45+
static_assert(test::IsSame<decltype(S1<int>::E{}+0), int>::check(), "");
46+
static_assert(test::IsSame<decltype(S1<unsigned>::E{}+0), unsigned>::check(), "");
47+
char Z1 = S1<unsigned>::E(-1) + 0; // expected-warning{{implicit conversion from 'unsigned int' to 'char'}}
48+
49+
template <typename Traits>
50+
struct S2 {
51+
enum E : typename Traits::IntegerType;
52+
};
53+
54+
template <typename T>
55+
struct Traits {
56+
typedef T IntegerType;
57+
};
58+
59+
int X2 = S2<Traits<int>>::E{} + 0;
60+
int Y2 = S2<Traits<unsigned>>::E{} + 0;
61+
static_assert(test::IsSame<decltype(S2<Traits<int>>::E{}+0), int>::check(), "");
62+
static_assert(test::IsSame<decltype(S2<Traits<unsigned>>::E{}+0), unsigned>::check(), "");
63+
// C++11 [conv.prom]p4:
64+
// A prvalue of an unscoped enumeration type whose underlying type is fixed can be converted to a
65+
// prvalue of its underlying type. Moreover, if integral promotion can be applied to its underlying type, a
66+
// prvalue of an unscoped enumeration type whose underlying type is fixed can also be converted to a prvalue
67+
// of the promoted underlying type.
68+
static_assert(test::IsSame<decltype(S2<Traits<char>>::E{}+char(0)), int>::check(), "");
69+
70+
71+
template <typename T>
72+
struct S3 {
73+
enum E : unsigned;
74+
};
75+
76+
int X3 = S3<float>::E{} + 0;
77+
78+
// fails in clang 19 and earlier (see the discussion on GitHub Issue #117960):
79+
static_assert(test::IsSame<decltype(S3<float>::E{}+0), unsigned>::check(), "");
80+
81+
template <typename T>
82+
struct S4 {
83+
enum E1 : char;
84+
enum E2 : T;
85+
};
86+
87+
int X4 = S4<char>::E1{} + '\0';
88+
int Y4 = S4<char>::E2{} + '\0';
89+
90+
template <typename T>
91+
struct S5 {
92+
enum class E1 : char;
93+
enum class E2 : T;
94+
};
95+
96+
int X5 = S5<char>::E1{} + '\0'; // expected-error{{invalid operands to binary expression}}
97+
int Y5 = S5<char>::E2{} + '\0'; // expected-error{{invalid operands to binary expression}}
98+
99+
100+
template <typename T>
101+
struct S6 {
102+
enum E1 : T;
103+
enum E2 : E1; // expected-error{{invalid underlying type}}
104+
};
105+
106+
template struct S6<int>; // expected-note{{in instantiation of template class 'S6<int>' requested here}}
107+
108+
109+
template <typename T>
110+
struct S7 {
111+
enum E : T;
112+
enum E : T { X, Y, Z }; // expected-note{{previous declaration is here}}
113+
enum E : T; // expected-warning{{class member cannot be redeclared}}
114+
};
115+
116+
template struct S7<int>;
117+
118+
template <typename T>
119+
struct S8 {
120+
enum E : char;
121+
enum E : char { X, Y, Z }; // expected-note{{previous declaration is here}}
122+
enum E : char; // expected-warning{{class member cannot be redeclared}}
123+
};
124+
125+
template struct S8<float>;
126+
127+
template <typename T>
128+
struct S9 {
129+
enum class E1 : T;
130+
enum class E1 : T { X, Y, Z }; // expected-note{{previous declaration is here}}
131+
enum class E1 : T; // expected-warning{{class member cannot be redeclared}}
132+
enum class E2 : char;
133+
enum class E2 : char { X, Y, Z }; // expected-note{{previous declaration is here}}
134+
enum class E2 : char; // expected-warning{{class member cannot be redeclared}}
135+
};
136+
137+
template struct S9<int>;
138+
139+
#if defined(__cplusplus) && __cplusplus >= 201402L
140+
template <typename T>
141+
struct S10 {
142+
enum [[deprecated("for reasons")]] E : T; // expected-note{{explicitly marked deprecated here}}
143+
};
144+
145+
int X10 = S10<int>::E{} + 0; // expected-warning{{deprecated: for reasons}}
146+
#endif
147+
148+
template <typename T>
149+
struct S11 {};
150+
151+
template <>
152+
struct S11<unsigned> {
153+
enum E : unsigned;
154+
};
155+
156+
unsigned X11 = S11<unsigned>::E{} + 0u;
157+
158+
#if defined(__cplusplus) && __cplusplus >= 201402L
159+
template <typename T>
160+
struct S12 {
161+
enum [[deprecated("for reasons")]] E1 : T; // expected-note{{explicitly marked deprecated here}}
162+
enum [[deprecated("for reasons")]] E2 : T;
163+
};
164+
165+
template <>
166+
struct S12<float> {
167+
enum E1 : unsigned;
168+
enum E2 : unsigned;
169+
};
170+
171+
unsigned X12 = S12<float>::E1{} + 0u;
172+
unsigned Y12 = S12<float>::E2{} + 0u;
173+
int Z12 = S12<int>::E1{} + 0; // expected-warning{{deprecated: for reasons}}
174+
#endif
175+
176+
template <typename T>
177+
struct S13 {
178+
enum __attribute__((packed)) E { X, Y };
179+
};
180+
181+
static_assert(sizeof(S13<int>::E) == 1, "");
182+
183+
template<typename T>
184+
struct S14 {
185+
enum E : float; // expected-error {{invalid underlying type}}
186+
};
187+
188+
template<typename T>
189+
struct S15 {
190+
enum E : T; // expected-error {{invalid underlying type}}
191+
};
192+
193+
template struct S15<float>; // expected-note {{in instantiation of template class 'S15<float>' requested here}}
194+
195+
196+
197+
198+
template <typename T>
199+
int f1() {
200+
enum E : T;
201+
return E{} + 0;
202+
}
203+
204+
int F1 = f1<int>();
205+
206+
template <typename T>
207+
int f2() {
208+
struct LocalClass {
209+
enum E : T;
210+
};
211+
return typename LocalClass::E{} + 0;
212+
}
213+
214+
int F2 = f2<int>();

0 commit comments

Comments
 (0)