Crash: XCTest functions and namespaced enum classes

I wanted to post about this here since I can't file a bug right now, but also double-check that I'm not doing anything wrong.

If I have a C++ header with a enum class with the same name both in and out of a namespace, referencing the namespaced enum in an XCTest function will crash the test.

For example in a header:

enum class SameNameEnum { watermelon, apple, orange };

namespace sameName {

enum class SameNameEnum { watermelon, apple, orange };

}  // namespace sameName

And then in a Swift xctest file:

  func testClassEnumInOutNamespace_crashes() throws {
    // Test the class enum not in a namespace.
    let food = SameNameEnum.watermelon;
    XCTAssertNotEqual(food, SameNameEnum.orange)

    // Test the class enum in a namespace.
    // CRASHES in XCTAssertNotEqual()
    let moreFood = sameName.SameNameEnum.watermelon;
    XCTAssertNotEqual(moreFood, sameName.SameNameEnum.orange)
  }

Crashes in XCTAssertNotEqual in swift_getAssociatedTypeWitnessSlowImpl().

Should I just hold onto this and file a bug in a week or two when everything is back to normal?

-pink

Thanks Mike.

I have an idea why this might be happening. We had a similar issue for class templates. It has to do with caching metadata. Basically sometimes we use the wrong name (or in this case, don't account for the decl context's name). So the compiler uses the same (cached) metadata.

I will try to fix it later today. Got to run to a meeting now, but I can explain more later :)

Thank you again for finding this issue (and for the nice reproducer)! Here's the fix: [cxx-interop] Fix metadata caching for nested types with the same name. by zoecarver · Pull Request #42494 · apple/swift · GitHub

Should land later today.

1 Like

Sweet!