Skip to content

Reflection: API to distinguish types imported from C/C++/ObjC #69146

@grynspan

Description

@grynspan

Motivation
swift-testing needs to distinguish types imported from C, in particular C enumerations, so that we can correctly display instances of those types when a test fails. For example:

typedef NS_ENUM(int, Vegetable) {
  VegetableLettuce, VegetableCarrot, VegetableParsnip
}
let vegetable = ...
#expect(vegetable == .Carrot)

If the value of vegetable does not equal .Carrot, swift-testing diagnoses the issue with a message like:

Expectation failed: (vegetable -> ACTUAL_VALUE) == (.Carrot -> EXPECTED_VALUE)

Where ACTUAL_VALUE and EXPECTED_VALUE equal String(describing: vegetable) and String(describing: Vegetable.Carrot) respectively.

Solution

We'd like to be able to refine this further for C types which currently describe themselves as just Vegetable. Case names aren't available, which is understandable, but being able to distinguish C types means we can special-case these types and present the raw values of the cases (e.g. (.Carrot -> Vegetable(rawValue: 1)).) This improves the diagnostic output from swift-testing.

Alternatives considered

  • Leaving our output as-is. We get sub-optimal output for imported C types as a result.
  • Using the unsupported public function _mangledTypeName() to get the mangled name and looking for evidence of the __C module. We're doing this today as a temporary workaround, but would prefer to use supported API.
  • Spelunking in ABI for this information. That's even worse.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureA feature request or implementationreflection

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions