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

Conversation

kubamracek
Copy link
Contributor

This fixes a bug in the (recently added) ClangImporter logic that imports global/static integer+enum+float values on top of their declarations. See the attached testcase that triggers a compiler crash:

SIL verification failed: return value type does not match return type of function
  $XCTestError.Code
  $Optional<XCTestError.Code>
Verifying instruction:
     %6 = apply %5(%4, %0) : $@convention(method) (Int, @thin XCTestError.Code.Type) -> Optional<XCTestError.Code> // user: %7
->   return %6 : $Optional<XCTestError.Code>      // id: %7
In function:
// XCTestErrorUnsupported.getter
// Isolation: nonisolated
sil shared [transparent] [serialized] [ossa] @$sSo22XCTestErrorUnsupportedSo0aB4CodeVvg : $@convention(thin) () -> XCTestError.Code {
bb0:
  %0 = metatype $@thin XCTestError.Code.Type      // user: %6
  %1 = integer_literal $Builtin.IntLiteral, 109   // user: %4
  %2 = metatype $@thin Int.Type                   // user: %4
  // function_ref Int.init(_builtinIntegerLiteral:)
  %3 = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int // user: %4
  %4 = apply %3(%1, %2) : $@convention(method) (Builtin.IntLiteral, @thin Int.Type) -> Int // user: %6
  // function_ref XCTestErrorCode.init(rawValue:)
  %5 = function_ref @$sSo15XCTestErrorCodeV8rawValueABSgSi_tcfC : $@convention(method) (Int, @thin XCTestError.Code.Type) -> Optional<XCTestError.Code> // user: %6
  %6 = apply %5(%4, %0) : $@convention(method) (Int, @thin XCTestError.Code.Type) -> Optional<XCTestError.Code> // user: %7
  return %6                                       // id: %7
} // end sil function '$sSo22XCTestErrorUnsupportedSo0aB4CodeVvg'

If a C enum is imported as a Swift enum with a failable initializer, we need to unwrap the value after we call the initializer in the getter.

rdar://151174264

Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick fix, Kuba!

@kubamracek
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@j-hui j-hui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@kubamracek kubamracek merged commit 749e9ee into swiftlang:main May 14, 2025
5 checks passed
Copy link
Contributor

@beccadax beccadax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retrospective review: LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants