Skip to content

Commit 6caeb26

Browse files
authored
Fix ternary operator for enum result type (#4838)
Note: existing path that limits operator to basic numeric types still allowed enum due to it being AR_TOBJ_BASIC, rather than using a new ArTypeObjectKind. It's questionable whether this was intentional, but changing it would require another special case to allow enums, like exists for object types, and it might have other side effects as well.
1 parent ce495eb commit 6caeb26

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9855,8 +9855,15 @@ clang::QualType HLSLExternalSource::CheckVectorConditional(
98559855
return QualType();
98569856
}
98579857

9858-
// Here, element kind is combined with dimensions for result type.
9859-
ResultTy = NewSimpleAggregateType(AR_TOBJ_INVALID, resultElementKind, 0, rowCount, colCount)->getCanonicalTypeInternal();
9858+
// Here, element kind is combined with dimensions for primitive types.
9859+
if (IS_BASIC_PRIMITIVE(resultElementKind)) {
9860+
ResultTy = NewSimpleAggregateType(AR_TOBJ_INVALID, resultElementKind, 0, rowCount, colCount)->getCanonicalTypeInternal();
9861+
} else {
9862+
DXASSERT(rowCount == 1 && colCount == 1,
9863+
"otherwise, attempting to construct vector or matrix with "
9864+
"non-primitive component type");
9865+
ResultTy = ResultTy.getUnqualifiedType();
9866+
}
98609867

98619868
// Cast condition to RValue
98629869
if (Cond.get()->isLValue())
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %dxc -ast-dump -T vs_6_0 %s | FileCheck %s
2+
// RUN: %dxc -ast-dump -T vs_6_0 -HV 2021 %s | FileCheck %s
3+
// RUN: %dxc -ast-dump -T vs_6_0 -DCLASS %s | FileCheck %s
4+
// RUN: %dxc -ast-dump -T vs_6_0 -DCLASS -HV 2021 %s | FileCheck %s
5+
6+
// CHECK: VarDecl
7+
// CHECK-SAME: referenced g_c 'const MyEnum' static cinit
8+
// CHECK-NEXT: ConditionalOperator
9+
// CHECK-SAME: 'MyEnum'
10+
// CHECK-NEXT: CXXBoolLiteralExpr
11+
// CHECK-SAME: 'bool' true
12+
// CHECK-NEXT: DeclRefExpr
13+
// CHECK-SAME: 'MyEnum' EnumConstant
14+
// CHECK-SAME: 'One' 'MyEnum'
15+
// CHECK-NEXT: DeclRefExpr
16+
// CHECK-SAME: 'MyEnum' EnumConstant
17+
// CHECK-SAME: 'Two' 'MyEnum'
18+
19+
enum
20+
#ifdef CLASS
21+
class
22+
#endif
23+
MyEnum {
24+
One,
25+
Two,
26+
};
27+
28+
#ifdef CLASS
29+
static const MyEnum g_c = true ? MyEnum::One : MyEnum::Two;
30+
#else
31+
static const MyEnum g_c = true ? One : Two;
32+
#endif
33+
34+
int main() : OUT {
35+
return (int)g_c;
36+
}

0 commit comments

Comments
 (0)