Skip to content

Commit 9b7c609

Browse files
authored
Merge pull request #33058 from nkcsgexi/65889766-5.3
[5.3] AST: consider extension and extended type are from the same module if valid alternative module names are equal
2 parents a176d64 + e092141 commit 9b7c609

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

lib/AST/Decl.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,14 +1326,28 @@ bool ExtensionDecl::isConstrainedExtension() const {
13261326
bool ExtensionDecl::isEquivalentToExtendedContext() const {
13271327
auto decl = getExtendedNominal();
13281328
bool extendDeclFromSameModule = false;
1329-
if (!decl->getAlternateModuleName().empty()) {
1330-
// if the extended type was defined in the same module with the extension,
1331-
// we should consider them as the same module to preserve ABI stability.
1332-
extendDeclFromSameModule = decl->getAlternateModuleName() ==
1333-
getParentModule()->getNameStr();
1329+
auto extensionAlterName = getAlternateModuleName();
1330+
auto typeAlterName = decl->getAlternateModuleName();
1331+
1332+
if (!extensionAlterName.empty()) {
1333+
if (!typeAlterName.empty()) {
1334+
// Case I: type and extension are both moved from somewhere else
1335+
extendDeclFromSameModule = typeAlterName == extensionAlterName;
1336+
} else {
1337+
// Case II: extension alone was moved from somewhere else
1338+
extendDeclFromSameModule = extensionAlterName ==
1339+
decl->getParentModule()->getNameStr();
1340+
}
13341341
} else {
1335-
extendDeclFromSameModule = decl->getParentModule() == getParentModule();
1342+
if (!typeAlterName.empty()) {
1343+
// Case III: extended type alone was moved from somewhere else
1344+
extendDeclFromSameModule = typeAlterName == getParentModule()->getNameStr();
1345+
} else {
1346+
// Case IV: neither of type and extension was moved from somewhere else
1347+
extendDeclFromSameModule = getParentModule() == decl->getParentModule();
1348+
}
13361349
}
1350+
13371351
return extendDeclFromSameModule
13381352
&& !isConstrainedExtension()
13391353
&& !getDeclaredInterfaceType()->isExistentialType();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// REQUIRES: OS=macosx
2+
// RUN: %empty-directory(%t)
3+
4+
// RUN: %target-swift-frontend -typecheck %s -emit-tbd -emit-tbd-path %t/before_move.tbd -D BEFORE_MOVE -module-name Foo -enable-library-evolution -emit-sil -o %t/before_move.sil
5+
// RUN: %FileCheck %s --check-prefix=CHECK-TBD < %t/before_move.tbd
6+
// RUN: %FileCheck %s --check-prefix=CHECK-SIL < %t/before_move.sil
7+
8+
// RUN: %target-swift-frontend %s -emit-module -emit-module-path %t/FooCore.swiftmodule -D AFTER_MOVE_FOO_CORE -module-name FooCore -enable-library-evolution -emit-tbd -emit-tbd-path %t/after_move.tbd -emit-sil -o %t/after_move.sil
9+
10+
// RUN: %FileCheck %s --check-prefix=CHECK-TBD < %t/after_move.tbd
11+
// RUN: %FileCheck %s --check-prefix=CHECK-SIL < %t/after_move.sil
12+
13+
// CHECK-TBD: '_$s3Foo4DateC03SubB0V4yearAESi_tcfC'
14+
// CHECK-TBD: '_$s3Foo4DateC03SubB0VMn'
15+
16+
// CHECK-SIL: sil [available 10.7] @$s3Foo4DateC03SubB0V4yearAESi_tcfC : $@convention(method) (Int, @thin Date.SubDate.Type) -> @out Date.SubDate
17+
18+
#if BEFORE_MOVE
19+
20+
@available(OSX 10.7, *)
21+
public class Date {
22+
public static func getCurrentYear() -> Int { return 2020 }
23+
}
24+
25+
@available(OSX 10.7, *)
26+
extension Date {
27+
public struct SubDate {
28+
public init(year: Int) {}
29+
}
30+
}
31+
32+
#endif
33+
34+
#if AFTER_MOVE_FOO_CORE
35+
36+
@available(OSX 10.7, *)
37+
@_originallyDefinedIn(module: "Foo", OSX 10.9)
38+
public class Date {}
39+
40+
@available(OSX 10.7, *)
41+
@_originallyDefinedIn(module: "Foo", OSX 10.9)
42+
extension Date {
43+
public struct SubDate {
44+
public init(year: Int) {}
45+
}
46+
}
47+
48+
#endif

0 commit comments

Comments
 (0)