Skip to content

Commit 1ecdb17

Browse files
committed
[ClangImporter] Fix enum conversion type when importing global values (unwrap if needed)
1 parent bb67d1e commit 1ecdb17

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4630,9 +4630,14 @@ namespace {
46304630
auto convertKind = ConstantConvertKind::None;
46314631
// Request conversions on enums, and swift_wrapper((enum/struct))
46324632
// types
4633-
if (decl->getType()->isEnumeralType())
4634-
convertKind = ConstantConvertKind::Construction;
4635-
else if (findSwiftNewtype(decl, Impl.getClangSema(),
4633+
if (decl->getType()->isEnumeralType()) {
4634+
if (type->getEnumOrBoundGenericEnum()) {
4635+
// When importing as an enum, also apply implicit force unwrap
4636+
convertKind = ConstantConvertKind::ConstructionWithUnwrap;
4637+
} else {
4638+
convertKind = ConstantConvertKind::Construction;
4639+
}
4640+
} else if (findSwiftNewtype(decl, Impl.getClangSema(),
46364641
Impl.CurrentVersion))
46374642
convertKind = ConstantConvertKind::Construction;
46384643

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %empty-directory(%t/src)
2+
// RUN: split-file %s %t/src
3+
4+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) %t/src/main.swift \
5+
// RUN: -module-name main -I %t/src -emit-sil -sil-verify-all | %FileCheck %s
6+
7+
//--- test.h
8+
9+
typedef long NSInteger;
10+
typedef enum XCTestErrorCode: NSInteger {
11+
XCTestErrorCodeTimeoutWhileWaiting,
12+
XCTestErrorCodeFailureWhileWaiting,
13+
} XCTestErrorCode;
14+
15+
static XCTestErrorCode const XCTestErrorUnsupported = (XCTestErrorCode)109;
16+
17+
//--- Framework.apinotes
18+
---
19+
Name: Framework
20+
Tags:
21+
- Name: XCTestErrorCode
22+
NSErrorDomain: XCTestErrorDomain
23+
24+
//--- module.modulemap
25+
module Framework {
26+
header "test.h"
27+
}
28+
29+
//--- main.swift
30+
import Framework
31+
func foo() {
32+
print(XCTestError.timeoutWhileWaiting)
33+
print(XCTestErrorUnsupported)
34+
}
35+
36+
37+
// CHECK: // XCTestErrorUnsupported.getter
38+
// CHECK: sil shared [transparent] @$sSo22XCTestErrorUnsupportedSo0aB4CodeVvg : $@convention(thin) () -> XCTestError {

0 commit comments

Comments
 (0)