Skip to content

Serialization: Ignore lookup shadowing when resolving cross-references #80009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,7 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
getIdentifier(privateDiscriminator));
} else {
baseModule->lookupQualified(baseModule, DeclNameRef(name),
SourceLoc(), NL_QualifiedDefault,
SourceLoc(), NL_RemoveOverridden,
values);
}
filterValues(filterTy, nullptr, nullptr, isType, inProtocolExt,
Expand Down Expand Up @@ -2285,8 +2285,8 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
getIdentifier(privateDiscriminator));
} else {
otherModule->lookupQualified(otherModule, DeclNameRef(name),
SourceLoc(), NL_QualifiedDefault,
values);
SourceLoc(), NL_RemoveOverridden,
values);
}

std::optional<ValueDecl*> matchBeforeFiltering = std::nullopt;
Expand Down
64 changes: 64 additions & 0 deletions test/Serialization/inlined-shadowed-clang-vs-swift.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/// Ensure inlined code in swiftmodules differentiates between shadowed Swift
/// and clang decls.

// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// REQUIRES: objc_interop

/// Reference build: Build libs and client for a normal build.
// RUN: %target-swift-frontend -emit-module %t/RootLib.swift -I %t \
// RUN: -emit-module-path %t/RootLib.swiftmodule
// RUN: %target-swift-frontend -emit-module %t/MiddleLib.swift -I %t \
// RUN: -emit-module-path %t/MiddleLib.swiftmodule
// RUN: %target-swift-frontend -emit-sil -O %t/Client.swift -I %t > %t/Client.sil
// RUN: %FileCheck %s --input-file=%t/Client.sil

//--- module.modulemap
module RootLib {
header "RootLib.h"
}

//--- RootLib.h
#include <stdio.h>
void ShadowedFunc() {
printf("Clang\n");
}

//--- RootLib.swift
@_exported import RootLib

@usableFromInline
internal func ShadowedFunc() {
print("Swift")
}

@inlinable
@inline(__always)
public func swiftUser() {
ShadowedFunc() // Swift one
}

//--- MiddleLib.swift

import RootLib

@inlinable
@inline(__always)
public func clangUser() {
ShadowedFunc() // Clang one
}

//--- Client.swift

import RootLib
import MiddleLib

swiftUser()
// CHECK: [[SWIFT_FUNC:%.*]] = function_ref @$s7RootLib12ShadowedFuncyyF : $@convention(thin) () -> ()
// CHECK: apply [[SWIFT_FUNC]]() : $@convention(thin) () -> ()
// CHECK-NOT: apply [[SWIFT_FUNC]]() : $@convention(thin) () -> ()

clangUser()
// CHECK: [[CLANG_FUNC:%.*]] = function_ref @ShadowedFunc : $@convention(c) () -> ()
// CHECK: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()
// CHECK-NOT: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()
127 changes: 127 additions & 0 deletions test/Serialization/inlined-shadowed-layered-modules.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/// Ensure inlined code in swiftmodules picks the right shadowed decl
/// relative to where they were inlined from, no matter if other imported
/// modules define more shadowers at the inlining site.

// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// REQUIRES: objc_interop

/// Reference build: Build libs and client for a normal build.
// RUN: %target-swift-frontend -emit-module %t/RootLib.swift -I %t \
// RUN: -emit-module-path %t/RootLib.swiftmodule
// RUN: %target-swift-frontend -emit-module %t/MiddleLib.swift -I %t \
// RUN: -emit-module-path %t/MiddleLib.swiftmodule
// RUN: %target-swift-frontend -emit-module %t/HighLib.swift -I %t \
// RUN: -emit-module-path %t/HighLib.swiftmodule
// RUN: %target-swift-frontend -emit-sil -O %t/Client.swift -I %t > %t/Client.sil
// RUN: %FileCheck %s --input-file=%t/Client.sil

//--- module.modulemap
module RootLib {
header "RootLib.h"
}
module ClangMiddleLib {
header "ClangMiddleLib.h"
}

//--- RootLib.h
#include <stdio.h>
void ShadowedFunc() { // Never picked as it's shadowed by Swift's.
printf("Clang\n");
}

void ShadowedFuncClang();

//--- ClangMiddleLib.h
#include "RootLib.h"
void ShadowedFuncClang();

//--- RootLib.swift
@_exported import RootLib
@_exported import ClangMiddleLib

public func ShadowedFunc() {
print("Swift Root")
}

@inlinable
@inline(__always)
public func userFromRoot() {
ShadowedFunc() // RootLib Swift one
}

@inlinable
@inline(__always)
public func clangUserFromRoot() {
ShadowedFuncClang()
}

//--- MiddleLib.swift
@_exported import RootLib

public func ShadowedFunc() {
print("Swift Middle")
}

@inlinable
@inline(__always)
public func userFromMiddle() {
ShadowedFunc() // MiddleLib one
}

@inlinable
@inline(__always)
public func clangUserFromMiddle() {
ShadowedFuncClang()
}

//--- HighLib.swift
@_exported import MiddleLib

@inlinable
@inline(__always)
public func userFromHigh() {
ShadowedFunc() // MiddleLib one
}

@inlinable
@inline(__always)
public func explicitUserFromHigh() {
RootLib.ShadowedFunc() // MRootLib.iddleLib one
}

@inlinable
@inline(__always)
public func clangUserFromHigh() {
ShadowedFuncClang()
}

//--- Client.swift
import HighLib
import ClangMiddleLib

// RootLib call its own Swift ShadowedFunc.
userFromRoot()
// CHECK: [[ROOT_FUNC:%.*]] = function_ref @$s7RootLib12ShadowedFuncyyF : $@convention(thin) () -> ()
// CHECK: apply [[ROOT_FUNC]]() : $@convention(thin) () -> ()
explicitUserFromHigh()
// CHECK: apply [[ROOT_FUNC]]() : $@convention(thin) () -> ()
// CHECK-NOT: apply [[ROOT_FUNC]]() : $@convention(thin) () -> ()

// MiddleLib and HighLib call the last ShadowedFunc from MiddleLib.
userFromMiddle()
// CHECK: [[MIDDLE_FUNC:%.*]] = function_ref @$s9MiddleLib12ShadowedFuncyyF : $@convention(thin) () -> ()
// CHECK: apply [[MIDDLE_FUNC]]() : $@convention(thin) () -> ()
userFromHigh()
// CHECK: apply [[MIDDLE_FUNC]]() : $@convention(thin) () -> ()
// CHECK-NOT: apply [[MIDDLE_FUNC]]() : $@convention(thin) () -> ()

// All callers of the clang func use the merged one anyway.
clangUserFromRoot()
clangUserFromMiddle()
clangUserFromHigh()
// CHECK: [[CLANG_FUNC:%.*]] = function_ref @ShadowedFuncClang : $@convention(c) () -> ()
// CHECK: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()
// CHECK: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()
// CHECK: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()
// CHECK-NOT: apply [[CLANG_FUNC]]() : $@convention(c) () -> ()