Skip to content

Commit 936e911

Browse files
committed
[ClangImporter] Fix importing values for constant globals of enum types
1 parent ec24b74 commit 936e911

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4583,7 +4583,7 @@ namespace {
45834583
if (dc->isTypeContext())
45844584
isStatic = true;
45854585

4586-
// For now we don't import static constexpr
4586+
// For now we don't import static constexpr. TODO: Lift this restriction.
45874587
if (isStatic && decl->isConstexpr())
45884588
return nullptr;
45894589

@@ -4606,8 +4606,13 @@ namespace {
46064606
decl->getType(), ImportTypeKind::Value,
46074607
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
46084608
isInSystemModule(dc), Bridgeability::None, ImportTypeAttrs());
4609+
4610+
auto convertKind = ConstantConvertKind::None;
4611+
if (decl->getType()->isEnumeralType())
4612+
convertKind = ConstantConvertKind::Construction;
4613+
46094614
result = synthesizer.createConstant(
4610-
name, dc, type, *val, ConstantConvertKind::None, isStatic, decl,
4615+
name, dc, type, *val, convertKind, isStatic, decl,
46114616
importer::convertClangAccess(decl->getAccess()));
46124617
}
46134618
}

test/ClangImporter/const_values.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ typedef enum MyEnum: char {
2828
} MyEnum;
2929
static const MyEnum static_const_enum = MyEnumCase1;
3030

31-
3231
//--- main.swift
3332
func foo() {
3433
print(MACRO_INT)
@@ -110,4 +109,10 @@ func foo() {
110109
// CHECK-NEXT: return %1
111110
// CHECK-NEXT: }
112111

113-
// TODO: Add static_const_enum
112+
// CHECK: sil shared [transparent] @$sSo17static_const_enumSo6MyEnumVvg : $@convention(thin) () -> MyEnum {
113+
// CHECK-NEXT: bb0:
114+
// CHECK-NEXT: %0 = integer_literal $Builtin.Int8, 1
115+
// CHECK-NEXT: %1 = struct $Int8 (%0)
116+
// CHECK-NEXT: %2 = struct $MyEnum (%1)
117+
// CHECK-NEXT: return %2
118+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)