Skip to content

Commit 1bfe559

Browse files
authored
Merge pull request swiftlang#33055 from nkcsgexi/65889766
AST: consider extension and extended type are from the same module if valid alternative module names are equal
2 parents 2a6bad2 + 74edd07 commit 1bfe559

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
@@ -1295,14 +1295,28 @@ bool ExtensionDecl::isConstrainedExtension() const {
12951295
bool ExtensionDecl::isEquivalentToExtendedContext() const {
12961296
auto decl = getExtendedNominal();
12971297
bool extendDeclFromSameModule = false;
1298-
if (!decl->getAlternateModuleName().empty()) {
1299-
// if the extended type was defined in the same module with the extension,
1300-
// we should consider them as the same module to preserve ABI stability.
1301-
extendDeclFromSameModule = decl->getAlternateModuleName() ==
1302-
getParentModule()->getNameStr();
1298+
auto extensionAlterName = getAlternateModuleName();
1299+
auto typeAlterName = decl->getAlternateModuleName();
1300+
1301+
if (!extensionAlterName.empty()) {
1302+
if (!typeAlterName.empty()) {
1303+
// Case I: type and extension are both moved from somewhere else
1304+
extendDeclFromSameModule = typeAlterName == extensionAlterName;
1305+
} else {
1306+
// Case II: extension alone was moved from somewhere else
1307+
extendDeclFromSameModule = extensionAlterName ==
1308+
decl->getParentModule()->getNameStr();
1309+
}
13031310
} else {
1304-
extendDeclFromSameModule = decl->getParentModule() == getParentModule();
1311+
if (!typeAlterName.empty()) {
1312+
// Case III: extended type alone was moved from somewhere else
1313+
extendDeclFromSameModule = typeAlterName == getParentModule()->getNameStr();
1314+
} else {
1315+
// Case IV: neither of type and extension was moved from somewhere else
1316+
extendDeclFromSameModule = getParentModule() == decl->getParentModule();
1317+
}
13051318
}
1319+
13061320
return extendDeclFromSameModule
13071321
&& !isConstrainedExtension()
13081322
&& !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)