AST: Also respect @_originallyDefinedIn in TypeDecl::compare()

If a protocol was moved from one module to another, we need to
continue using the old module name when comparing it against
other protocols.
This commit is contained in:
Slava Pestov
2025-03-21 13:33:04 -04:00
parent 38c478f5ee
commit a7d2b24116
2 changed files with 21 additions and 0 deletions

View File

@@ -5439,6 +5439,13 @@ int TypeDecl::compare(const TypeDecl *type1, const TypeDecl *type2) {
// of the ABI, and so we must take care to get the correct module
// name for the comparison.
auto getModuleNameForOrder = [&](const TypeDecl *decl) -> StringRef {
// Respect @_originallyDefinedIn on the type itself, so that
// moving a protocol across modules does not change its
// position in the order.
auto alternateName = decl->getAlternateModuleName();
if (!alternateName.empty())
return alternateName;
// This used to just call getName(), which caused accidental ABI breaks
// when a module is imported under different aliases.
//

View File

@@ -0,0 +1,14 @@
// RUN: %target-swift-emit-silgen %s -module-name main | %FileCheck %s
// REQUIRES: OS=macosx
@_originallyDefinedIn(module: "AnimalKit", macOS 10.10)
@available(macOS 10.9, *)
public protocol Ungulate {}
@_originallyDefinedIn(module: "HorseKit", macOS 10.10)
@available(macOS 10.9, *)
public protocol Horse {}
// CHECK-LABEL: sil [ossa] @$s4main39requirementOrderWithOriginallyDefinedInyyx9AnimalKit8UngulateRz05HorseI00K0RzlF : $@convention(thin) <T where T : Ungulate, T : Horse> (@in_guaranteed T) -> () {
public func requirementOrderWithOriginallyDefinedIn<T: Ungulate & Horse>(_: T) {}