|
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"
|
@@ -5365,9 +5366,39 @@ int TypeDecl::compare(const TypeDecl *type1, const TypeDecl *type2) {
|
5365 | 5366 |
|
5366 | 5367 | // Prefer module names earlier in the alphabet.
|
5367 | 5368 | if (dc1->isModuleScopeContext() && dc2->isModuleScopeContext()) {
|
5368 |
| - auto module1 = dc1->getParentModule(); |
5369 |
| - auto module2 = dc2->getParentModule(); |
5370 |
| - if (int result = module1->getName().str().compare(module2->getName().str())) |
| 5369 | + // For protocol declarations specifically, the order here is part |
| 5370 | + // of the ABI, and so we must take care to get the correct module |
| 5371 | + // name for the comparison. |
| 5372 | + auto getModuleNameForOrder = [&](const TypeDecl *decl) -> StringRef { |
| 5373 | + // This used to just call getName(), which caused accidental ABI breaks |
| 5374 | + // when a module is imported under different aliases. |
| 5375 | + // |
| 5376 | + // The new behavior matches the behavior in the mangler: |
| 5377 | + // |
| 5378 | + // - getName() returns the name of the module as its imported by the |
| 5379 | + // client, which might be an alias different from the real name. |
| 5380 | + // - getRealName() returns the real name after desugaring aliases. |
| 5381 | + // - getABIName() returns the name set with the -module-abi-name flag, or |
| 5382 | + // **getName()** if the flag is not set. |
| 5383 | + // |
| 5384 | + // Thus, the correct module name to use for mangling and ordering is |
| 5385 | + // getRealName(), unless getABIName() is distinct from getName(), in |
| 5386 | + // which case we use getABIName(). |
| 5387 | + // |
| 5388 | + // FIXME: This should be fixed after auditing callers of getABIName(). |
| 5389 | + // |
| 5390 | + // However, to maintain ABI compatibility, we ignore the ABI name for the |
| 5391 | + // _Concurrency module, and for the compiler build of SwiftSyntax. |
| 5392 | + auto *module = decl->getDeclContext()->getParentModule(); |
| 5393 | + if (module->getRealName().str() == SWIFT_CONCURRENCY_NAME || |
| 5394 | + module->getABIName().str() == "CompilerSwiftSyntax" || |
| 5395 | + module->getName() == module->getABIName()) { |
| 5396 | + return module->getRealName().str(); |
| 5397 | + } |
| 5398 | + return module->getABIName().str(); |
| 5399 | + }; |
| 5400 | + |
| 5401 | + if (int result = getModuleNameForOrder(type1).compare(getModuleNameForOrder(type2))) |
5371 | 5402 | return result;
|
5372 | 5403 | }
|
5373 | 5404 |
|
|
0 commit comments