Skip to content

[Sema] Downgrade to a warning public imports of an underlying private module #40770

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
Jan 10, 2022
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
9 changes: 7 additions & 2 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1769,10 +1769,15 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
"@_implementationOnly ");
}

static bool treatAsError = getenv("ENABLE_PUBLIC_IMPORT_OF_PRIVATE_AS_ERROR");
#ifndef NDEBUG
treatAsError = true;
static bool enableTreatAsError = true;
#else
static bool enableTreatAsError = getenv("ENABLE_PUBLIC_IMPORT_OF_PRIVATE_AS_ERROR");
#endif

bool isImportOfUnderlying = importer->getName() == target->getName();
bool treatAsError = enableTreatAsError &&
!isImportOfUnderlying;
if (!treatAsError)
inFlight.limitBehavior(DiagnosticBehavior::Warning);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void bar() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
framework module MainLib {
umbrella header "MainLib.h"

export *
module * { export * }
}
11 changes: 6 additions & 5 deletions test/Sema/implementation-only-import-suggestion-as-error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
// RUN: env ENABLE_PUBLIC_IMPORT_OF_PRIVATE_AS_ERROR=1 \
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
// RUN: -library-level api -verify
// RUN: -library-level api -verify -module-name MainLib

import PublicSwift
import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'main'}}
import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}

import PublicClang
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'main'}}
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'main'}}
import main // expected-warning{{'implementation-only-import-suggestion-as-error.swift' is part of module 'main'; ignoring import}}
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'}}

@_exported import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}
18 changes: 9 additions & 9 deletions test/Sema/implementation-only-import-suggestion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@
/// Expect warnings when building a public client.
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
// RUN: -library-level api -verify -D PUBLIC_IMPORTS
// RUN: -library-level api -verify -D PUBLIC_IMPORTS -module-name MainLib

/// Expect no warnings when building an SPI client.
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
// RUN: -library-level spi -D PUBLIC_IMPORTS
// RUN: -library-level spi -D PUBLIC_IMPORTS -module-name MainLib

/// The driver should also accept the flag and pass it along.
// RUN: %target-swiftc_driver -typecheck -sdk %t/sdk -module-cache-path %t %s \
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
// RUN: -library-level spi -D PUBLIC_IMPORTS
// RUN: -library-level spi -D PUBLIC_IMPORTS -module-name MainLib

/// Expect no warnings when building a client with some other library level.
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
// RUN: -D PUBLIC_IMPORTS
// RUN: -D PUBLIC_IMPORTS -module-name MainLib
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
// RUN: -library-level other -D PUBLIC_IMPORTS
// RUN: -library-level other -D PUBLIC_IMPORTS -module-name MainLib
#if PUBLIC_IMPORTS
import PublicSwift
import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'main'}}
import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported publicly from the public module 'MainLib'}}

import PublicClang
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'main'}}
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'main'}}
import main // expected-warning{{'implementation-only-import-suggestion.swift' is part of module 'main'; ignoring import}}
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'}}
@_exported import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}

/// Expect no warnings with implementation-only imports.
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
Expand Down