Skip to content

Sema: Use internal import in fixits on public imports of private modules #76386

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 3 commits into from
Sep 11, 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
11 changes: 7 additions & 4 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2404,10 +2404,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
InFlightDiagnostic inFlight =
Ctx.Diags.diagnose(ID, diag::error_public_import_of_private_module,
target->getName(), importer->getName());
if (ID->getAttrs().isEmpty()) {
inFlight.fixItInsert(ID->getStartLoc(),
"@_implementationOnly ");
}
if (auto attr = ID->getAttrs().getAttribute<AccessControlAttr>()) {
if (Ctx.LangOpts.hasFeature(Feature::InternalImportsByDefault))
inFlight.fixItRemove(attr->getLocation());
else
inFlight.fixItReplace(attr->getLocation(), "internal");
} else
inFlight.fixItInsert(ID->getStartLoc(), "internal ");

#ifndef NDEBUG
static bool enableTreatAsError = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// REQUIRES: VENDOR=apple
// REQUIRES: asserts

/// Prepare the SDK.
// RUN: cp -r %S/Inputs/public-private-sdk %t/sdk
Expand Down Expand Up @@ -42,12 +41,12 @@
// RUN: -library-level ipi -module-name MainLib
//--- PublicImports.swift
import PublicSwift
import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}
import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}

import PublicClang
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}
import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}{{1-1=internal }}
@_exported import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}

/// Expect no errors with implementation-only imports.
Expand Down Expand Up @@ -146,16 +145,27 @@ private import LocalClang
// RUN: -library-level api -verify
//--- ExplicitlyPublicImports.swift
public import PublicSwift
// expected-warning @-1 {{public import of 'PublicSwift' was not used in public declarations or inlinable code}}
public import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}
// expected-warning @-1 {{public import of 'PrivateSwift' was not used in public declarations or inlinable code}}
// expected-warning @-1 {{public import of 'PublicSwift' was not used in public declarations or inlinable code}}{{1-7=internal}}
public import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}{{1-7=internal}}
// expected-warning @-1 {{public import of 'PrivateSwift' was not used in public declarations or inlinable code}}{{1-7=internal}}

public import PublicClang
// expected-warning @-1 {{public import of 'PublicClang' was not used in public declarations or inlinable code}}
public import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}
// expected-warning @-1 {{public import of 'PublicClang_Private' was not used in public declarations or inlinable code}}
public import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}
// expected-warning @-1 {{public import of 'FullyPrivateClang' was not used in public declarations or inlinable code}}
public import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}
// expected-warning @-1 {{public import of 'LocalClang' was not used in public declarations or inlinable code}}
@_exported public import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}
// expected-warning @-1 {{public import of 'PublicClang' was not used in public declarations or inlinable code}}{{1-7=internal}}
public import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}{{1-7=internal}}
// expected-warning @-1 {{public import of 'PublicClang_Private' was not used in public declarations or inlinable code}}{{1-7=internal}}
public import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}{{1-7=internal}}
// expected-warning @-1 {{public import of 'FullyPrivateClang' was not used in public declarations or inlinable code}}{{1-7=internal}}
public import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}{{1-7=internal}}
// expected-warning @-1 {{public import of 'LocalClang' was not used in public declarations or inlinable code}}{{1-7=internal}}
@_exported public import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}{{12-18=internal}}

// RUN: %target-swift-frontend -typecheck -sdk %t/sdk %t/ImplictlyInternalImports.swift \
// RUN: -module-name MainLib -module-cache-path %t \
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
// RUN: -enable-upcoming-feature InternalImportsByDefault \
// RUN: -library-level api -verify
//--- ImplictlyInternalImports.swift
public import PublicSwift
// expected-warning @-1 {{public import of 'PublicSwift' was not used in public declarations or inlinable code}}{{1-8=}}
public import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}{{1-8=}}
// expected-warning @-1 {{public import of 'PrivateSwift' was not used in public declarations or inlinable code}}{{1-8=}}