Skip to content

Commit 7ae7371

Browse files
authored
Merge pull request #80009 from xymus/deser-internal-shadow
Serialization: Ignore lookup shadowing when resolving cross-references
2 parents 30a364d + bf224e2 commit 7ae7371

File tree

3 files changed

+194
-3
lines changed

3 files changed

+194
-3
lines changed

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,7 +2112,7 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
21122112
getIdentifier(privateDiscriminator));
21132113
} else {
21142114
baseModule->lookupQualified(baseModule, DeclNameRef(name),
2115-
SourceLoc(), NL_QualifiedDefault,
2115+
SourceLoc(), NL_RemoveOverridden,
21162116
values);
21172117
}
21182118
filterValues(filterTy, nullptr, nullptr, isType, inProtocolExt,
@@ -2322,8 +2322,8 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
23222322
getIdentifier(privateDiscriminator));
23232323
} else {
23242324
otherModule->lookupQualified(otherModule, DeclNameRef(name),
2325-
SourceLoc(), NL_QualifiedDefault,
2326-
values);
2325+
SourceLoc(), NL_RemoveOverridden,
2326+
values);
23272327
}
23282328

23292329
std::optional<ValueDecl*> matchBeforeFiltering = std::nullopt;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/// Ensure inlined code in swiftmodules differentiates between shadowed Swift
2+
/// and clang decls.
3+
4+
// RUN: %empty-directory(%t)
5+
// RUN: split-file %s %t
6+
// REQUIRES: objc_interop
7+
8+
/// Reference build: Build libs and client for a normal build.
9+
// RUN: %target-swift-frontend -emit-module %t/RootLib.swift -I %t \
10+
// RUN: -emit-module-path %t/RootLib.swiftmodule
11+
// RUN: %target-swift-frontend -emit-module %t/MiddleLib.swift -I %t \
12+
// RUN: -emit-module-path %t/MiddleLib.swiftmodule
13+
// RUN: %target-swift-frontend -emit-sil -O %t/Client.swift -I %t > %t/Client.sil
14+
// RUN: %FileCheck %s --input-file=%t/Client.sil
15+
16+
//--- module.modulemap
17+
module RootLib {
18+
header "RootLib.h"
19+
}
20+
21+
//--- RootLib.h
22+
#include <stdio.h>
23+
void ShadowedFunc() {
24+
printf("Clang\n");
25+
}
26+
27+
//--- RootLib.swift
28+
@_exported import RootLib
29+
30+
@usableFromInline
31+
internal func ShadowedFunc() {
32+
print("Swift")
33+
}
34+
35+
@inlinable
36+
@inline(__always)
37+
public func swiftUser() {
38+
ShadowedFunc() // Swift one
39+
}
40+
41+
//--- MiddleLib.swift
42+
43+
import RootLib
44+
45+
@inlinable
46+
@inline(__always)
47+
public func clangUser() {
48+
ShadowedFunc() // Clang one
49+
}
50+
51+
//--- Client.swift
52+
53+
import RootLib
54+
import MiddleLib
55+
56+
swiftUser()
57+
// CHECK: [[SWIFT_FUNC:%.*]] = function_ref @$s7RootLib12ShadowedFuncyyF : $@convention(thin) () -> ()
58+
// CHECK: apply [[SWIFT_FUNC]]() : $@convention(thin) () -> ()
59+
// CHECK-NOT: apply [[SWIFT_FUNC]]() : $@convention(thin) () -> ()
60+
61+
clangUser()
62+
// CHECK: [[CLANG_FUNC:%.*]] = function_ref @ShadowedFunc : $@convention(c) () -> ()
63+
// CHECK: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()
64+
// CHECK-NOT: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/// Ensure inlined code in swiftmodules picks the right shadowed decl
2+
/// relative to where they were inlined from, no matter if other imported
3+
/// modules define more shadowers at the inlining site.
4+
5+
// RUN: %empty-directory(%t)
6+
// RUN: split-file %s %t
7+
// REQUIRES: objc_interop
8+
9+
/// Reference build: Build libs and client for a normal build.
10+
// RUN: %target-swift-frontend -emit-module %t/RootLib.swift -I %t \
11+
// RUN: -emit-module-path %t/RootLib.swiftmodule
12+
// RUN: %target-swift-frontend -emit-module %t/MiddleLib.swift -I %t \
13+
// RUN: -emit-module-path %t/MiddleLib.swiftmodule
14+
// RUN: %target-swift-frontend -emit-module %t/HighLib.swift -I %t \
15+
// RUN: -emit-module-path %t/HighLib.swiftmodule
16+
// RUN: %target-swift-frontend -emit-sil -O %t/Client.swift -I %t > %t/Client.sil
17+
// RUN: %FileCheck %s --input-file=%t/Client.sil
18+
19+
//--- module.modulemap
20+
module RootLib {
21+
header "RootLib.h"
22+
}
23+
module ClangMiddleLib {
24+
header "ClangMiddleLib.h"
25+
}
26+
27+
//--- RootLib.h
28+
#include <stdio.h>
29+
void ShadowedFunc() { // Never picked as it's shadowed by Swift's.
30+
printf("Clang\n");
31+
}
32+
33+
void ShadowedFuncClang();
34+
35+
//--- ClangMiddleLib.h
36+
#include "RootLib.h"
37+
void ShadowedFuncClang();
38+
39+
//--- RootLib.swift
40+
@_exported import RootLib
41+
@_exported import ClangMiddleLib
42+
43+
public func ShadowedFunc() {
44+
print("Swift Root")
45+
}
46+
47+
@inlinable
48+
@inline(__always)
49+
public func userFromRoot() {
50+
ShadowedFunc() // RootLib Swift one
51+
}
52+
53+
@inlinable
54+
@inline(__always)
55+
public func clangUserFromRoot() {
56+
ShadowedFuncClang()
57+
}
58+
59+
//--- MiddleLib.swift
60+
@_exported import RootLib
61+
62+
public func ShadowedFunc() {
63+
print("Swift Middle")
64+
}
65+
66+
@inlinable
67+
@inline(__always)
68+
public func userFromMiddle() {
69+
ShadowedFunc() // MiddleLib one
70+
}
71+
72+
@inlinable
73+
@inline(__always)
74+
public func clangUserFromMiddle() {
75+
ShadowedFuncClang()
76+
}
77+
78+
//--- HighLib.swift
79+
@_exported import MiddleLib
80+
81+
@inlinable
82+
@inline(__always)
83+
public func userFromHigh() {
84+
ShadowedFunc() // MiddleLib one
85+
}
86+
87+
@inlinable
88+
@inline(__always)
89+
public func explicitUserFromHigh() {
90+
RootLib.ShadowedFunc() // MRootLib.iddleLib one
91+
}
92+
93+
@inlinable
94+
@inline(__always)
95+
public func clangUserFromHigh() {
96+
ShadowedFuncClang()
97+
}
98+
99+
//--- Client.swift
100+
import HighLib
101+
import ClangMiddleLib
102+
103+
// RootLib call its own Swift ShadowedFunc.
104+
userFromRoot()
105+
// CHECK: [[ROOT_FUNC:%.*]] = function_ref @$s7RootLib12ShadowedFuncyyF : $@convention(thin) () -> ()
106+
// CHECK: apply [[ROOT_FUNC]]() : $@convention(thin) () -> ()
107+
explicitUserFromHigh()
108+
// CHECK: apply [[ROOT_FUNC]]() : $@convention(thin) () -> ()
109+
// CHECK-NOT: apply [[ROOT_FUNC]]() : $@convention(thin) () -> ()
110+
111+
// MiddleLib and HighLib call the last ShadowedFunc from MiddleLib.
112+
userFromMiddle()
113+
// CHECK: [[MIDDLE_FUNC:%.*]] = function_ref @$s9MiddleLib12ShadowedFuncyyF : $@convention(thin) () -> ()
114+
// CHECK: apply [[MIDDLE_FUNC]]() : $@convention(thin) () -> ()
115+
userFromHigh()
116+
// CHECK: apply [[MIDDLE_FUNC]]() : $@convention(thin) () -> ()
117+
// CHECK-NOT: apply [[MIDDLE_FUNC]]() : $@convention(thin) () -> ()
118+
119+
// All callers of the clang func use the merged one anyway.
120+
clangUserFromRoot()
121+
clangUserFromMiddle()
122+
clangUserFromHigh()
123+
// CHECK: [[CLANG_FUNC:%.*]] = function_ref @ShadowedFuncClang : $@convention(c) () -> ()
124+
// CHECK: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()
125+
// CHECK: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()
126+
// CHECK: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()
127+
// CHECK-NOT: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()

0 commit comments

Comments
 (0)