|
15 | 15 | //
|
16 | 16 | //===----------------------------------------------------------------------===//
|
17 | 17 |
|
| 18 | +#include "swift/Strings.h" |
18 | 19 | #include "swift/AST/Decl.h"
|
19 | 20 | #include "swift/AST/ASTContext.h"
|
20 | 21 | #include "swift/AST/ASTMangler.h"
|
@@ -5434,9 +5435,29 @@ int TypeDecl::compare(const TypeDecl *type1, const TypeDecl *type2) {
|
5434 | 5435 |
|
5435 | 5436 | // Prefer module names earlier in the alphabet.
|
5436 | 5437 | if (dc1->isModuleScopeContext() && dc2->isModuleScopeContext()) {
|
5437 |
| - auto module1 = dc1->getParentModule(); |
5438 |
| - auto module2 = dc2->getParentModule(); |
5439 |
| - if (int result = module1->getName().str().compare(module2->getName().str())) |
| 5438 | + // For protocol declarations specifically, the order here is part |
| 5439 | + // of the ABI, and so we must take care to get the correct module |
| 5440 | + // name for the comparison. |
| 5441 | + auto getModuleNameForOrder = [&](const TypeDecl *decl) -> StringRef { |
| 5442 | + // Respect @_originallyDefinedIn on the type itself, so that |
| 5443 | + // moving a protocol across modules does not change its |
| 5444 | + // position in the order. |
| 5445 | + auto alternateName = decl->getAlternateModuleName(); |
| 5446 | + if (!alternateName.empty()) |
| 5447 | + return alternateName; |
| 5448 | + |
| 5449 | + // This used to just call getName(), which caused accidental ABI breaks |
| 5450 | + // when a module is imported under different aliases. |
| 5451 | + // |
| 5452 | + // Ideally, we would use getABIName() here. However, this would |
| 5453 | + // cause ABI breaks with the _Concurrency and CompilerSwiftSyntax |
| 5454 | + // builds, which already passed in a -module-abi-name but had |
| 5455 | + // existing symbols mangled with the wrong order. |
| 5456 | + auto *module = decl->getDeclContext()->getParentModule(); |
| 5457 | + return module->getRealName().str(); |
| 5458 | + }; |
| 5459 | + |
| 5460 | + if (int result = getModuleNameForOrder(type1).compare(getModuleNameForOrder(type2))) |
5440 | 5461 | return result;
|
5441 | 5462 | }
|
5442 | 5463 |
|
|
0 commit comments