Skip to content

[ClangImporter] Fix enum conversion type when importing global values (unwrap if needed) #81492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4630,9 +4630,14 @@ namespace {
auto convertKind = ConstantConvertKind::None;
// Request conversions on enums, and swift_wrapper((enum/struct))
// types
if (decl->getType()->isEnumeralType())
convertKind = ConstantConvertKind::Construction;
else if (findSwiftNewtype(decl, Impl.getClangSema(),
if (decl->getType()->isEnumeralType()) {
if (type->getEnumOrBoundGenericEnum()) {
// When importing as an enum, also apply implicit force unwrap
convertKind = ConstantConvertKind::ConstructionWithUnwrap;
} else {
convertKind = ConstantConvertKind::Construction;
}
} else if (findSwiftNewtype(decl, Impl.getClangSema(),
Impl.CurrentVersion))
convertKind = ConstantConvertKind::Construction;

Expand Down
38 changes: 38 additions & 0 deletions test/ClangImporter/const_values_apinotes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// RUN: %empty-directory(%t/src)
// RUN: split-file %s %t/src

// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) %t/src/main.swift \
// RUN: -module-name main -I %t/src -emit-sil -sil-verify-all | %FileCheck %s

//--- test.h

typedef long NSInteger;
typedef enum XCTestErrorCode: NSInteger {
XCTestErrorCodeTimeoutWhileWaiting,
XCTestErrorCodeFailureWhileWaiting,
} XCTestErrorCode;

static XCTestErrorCode const XCTestErrorUnsupported = (XCTestErrorCode)109;

//--- Framework.apinotes
---
Name: Framework
Tags:
- Name: XCTestErrorCode
NSErrorDomain: XCTestErrorDomain

//--- module.modulemap
module Framework {
header "test.h"
}

//--- main.swift
import Framework
func foo() {
print(XCTestError.timeoutWhileWaiting)
print(XCTestErrorUnsupported)
}


// CHECK: // XCTestErrorUnsupported.getter
// CHECK: sil shared [transparent] @$sSo22XCTestErrorUnsupportedSo0aB4CodeVvg : $@convention(thin) () -> XCTestError {