Skip to content

Commit 3f815ac

Browse files
authored
Merge pull request swiftlang#40770 from xymus/public-overlays
[Sema] Downgrade to a warning public imports of an underlying private module
2 parents 7972139 + bf12625 commit 3f815ac

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,10 +1769,15 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
17691769
"@_implementationOnly ");
17701770
}
17711771

1772-
static bool treatAsError = getenv("ENABLE_PUBLIC_IMPORT_OF_PRIVATE_AS_ERROR");
17731772
#ifndef NDEBUG
1774-
treatAsError = true;
1773+
static bool enableTreatAsError = true;
1774+
#else
1775+
static bool enableTreatAsError = getenv("ENABLE_PUBLIC_IMPORT_OF_PRIVATE_AS_ERROR");
17751776
#endif
1777+
1778+
bool isImportOfUnderlying = importer->getName() == target->getName();
1779+
bool treatAsError = enableTreatAsError &&
1780+
!isImportOfUnderlying;
17761781
if (!treatAsError)
17771782
inFlight.limitBehavior(DiagnosticBehavior::Warning);
17781783
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void bar() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
framework module MainLib {
2+
umbrella header "MainLib.h"
3+
4+
export *
5+
module * { export * }
6+
}

test/Sema/implementation-only-import-suggestion-as-error.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
// RUN: env ENABLE_PUBLIC_IMPORT_OF_PRIVATE_AS_ERROR=1 \
1818
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
1919
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
20-
// RUN: -library-level api -verify
20+
// RUN: -library-level api -verify -module-name MainLib
2121

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

2525
import PublicClang
26-
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'main'}}
27-
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'main'}}
28-
import main // expected-warning{{'implementation-only-import-suggestion-as-error.swift' is part of module 'main'; ignoring import}}
26+
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}
27+
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}
28+
29+
@_exported import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}

test/Sema/implementation-only-import-suggestion.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@
1414
/// Expect warnings when building a public client.
1515
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
1616
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
17-
// RUN: -library-level api -verify -D PUBLIC_IMPORTS
17+
// RUN: -library-level api -verify -D PUBLIC_IMPORTS -module-name MainLib
1818

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

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

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

4040
import PublicClang
41-
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'main'}}
42-
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'main'}}
43-
import main // expected-warning{{'implementation-only-import-suggestion.swift' is part of module 'main'; ignoring import}}
41+
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}
42+
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}
43+
@_exported import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}
4444

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

0 commit comments

Comments
 (0)