Skip to content

Sema: fix reporting reexporting submodules imports as unused in API #77538

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 1 commit into from
Nov 12, 2024
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
7 changes: 5 additions & 2 deletions lib/Sema/TypeCheckAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2517,8 +2517,11 @@ void swift::recordRequiredImportAccessLevelForDecl(
if (auto attributedImport = sf->getImportAccessLevel(definingModule)) {
auto importedModule = attributedImport->module.importedModule;

// If the defining module is transitively imported, mark the responsible
// module as requiring the minimum access level too.
// Ignore submodules, same behavior from `getModuleContext` above.
importedModule = importedModule->getTopLevelModule();

// If the defining module is transitively imported, mark the locally
// imported module as requiring the minimum access level too.
if (importedModule != definingModule)
sf->registerRequiredAccessLevelForModule(importedModule, accessLevel);

Expand Down
33 changes: 33 additions & 0 deletions test/Sema/superfluously-public-imports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
// RUN: %target-swift-frontend -typecheck %t/ClientOfClangModules.swift -I %t \
// RUN: -package-name pkg -Rmodule-api-import \
// RUN: -enable-upcoming-feature InternalImportsByDefault -verify
// RUN: %target-swift-frontend -typecheck %t/ClientOfClangReexportedSubmodules.swift -I %t \
// RUN: -package-name pkg -Rmodule-api-import \
// RUN: -enable-upcoming-feature InternalImportsByDefault -verify
// RUN: %target-swift-frontend -typecheck %t/Client_Swift5.swift -I %t \
// RUN: -swift-version 5 -verify

Expand Down Expand Up @@ -283,6 +286,21 @@ module ClangTopModule {
}
}

module ClangReexportedSubmodulePublic {
header "ClangReexportedSubmodulePublic.h"
module ClangReexportedSubmodulePublicSub {
header "ClangReexportedSubmodulePublicSub.h"
export *
}
}

module ClangReexportedSubmoduleTop {
header "ClangReexportedSubmoduleTop.h"
module ClangReexportedSubmoduleSub {
header "ClangReexportedSubmoduleSub.h"
}
}

//--- ClangSimpleUnused.h
//--- ClangSimple.h
struct ClangSimpleType {};
Expand All @@ -298,6 +316,15 @@ struct ClangSubmoduleSubmoduleType {};
struct ClangTopModuleType {};
//--- ClangTopModuleSubmodule.h

//--- ClangReexportedSubmodulePublic.h
//--- ClangReexportedSubmodulePublicSub.h
#include <ClangReexportedSubmoduleSub.h>

//--- ClangReexportedSubmoduleTop.h
//--- ClangReexportedSubmoduleSub.h
typedef struct _TypedefTypeUnderlying {
} TypedefType;

//--- ClientOfClangModules.swift
public import ClangSimple
public import ClangSimpleUnused // expected-warning {{public import of 'ClangSimpleUnused' was not used in public declarations or inlinable code}}
Expand All @@ -310,3 +337,9 @@ public import ClangTopModule.ClangTopModuleSubmodule
public func clangUser(a: ClangSimpleType) {} // expected-remark {{struct 'ClangSimpleType' is imported via 'ClangSimple'}}
public func clangUser(a: ClangSubmoduleSubmoduleType) {} // expected-remark {{struct 'ClangSubmoduleSubmoduleType' is imported via 'ClangSubmodule'}}
public func clangUser(a: ClangTopModuleType) {} // expected-remark {{struct 'ClangTopModuleType' is imported via 'ClangTopModule'}}

//--- ClientOfClangReexportedSubmodules.swift
public import ClangReexportedSubmodulePublic.ClangReexportedSubmodulePublicSub

public func useTypedefed(a: TypedefType) {} // expected-remark 2 {{typealias underlying type struct '_TypedefTypeUnderlying' is imported via 'ClangReexportedSubmodulePublicSub', which reexports definition from 'ClangReexportedSubmoduleTop'}}
// expected-remark @-1 {{type alias 'TypedefType' is imported via 'ClangReexportedSubmodulePublicSub', which reexports definition from 'ClangReexportedSubmoduleTop'}}