Expand DisallowedOriginKind with an explicit entry for internal bridging headers

This commit is contained in:
Doug Gregor
2025-09-22 10:38:06 -07:00
parent d04e6dd881
commit b7a83495fa
6 changed files with 35 additions and 3 deletions

View File

@@ -3834,6 +3834,7 @@ ERROR(decl_from_hidden_module,none,
"%2 was imported for SPI only|"
"%2 was not imported by this file|"
"C++ types from imported module %2 do not support library evolution|"
"it was imported via the internal bridging header|"
"%2 was not imported publicly}3",
(const Decl *, unsigned, Identifier, unsigned))
ERROR(typealias_desugars_to_type_from_hidden_module,none,
@@ -3850,6 +3851,7 @@ ERROR(typealias_desugars_to_type_from_hidden_module,none,
"%4 was imported for SPI only|"
"%4 was not imported by this file|"
"C++ types from imported module %4 do not support library evolution|"
"it was imported via the internal bridging header|"
"%4 was not imported publicly}5",
(const TypeAliasDecl *, StringRef, StringRef, unsigned, Identifier, unsigned))
ERROR(conformance_from_implementation_only_module,none,
@@ -3864,6 +3866,7 @@ ERROR(conformance_from_implementation_only_module,none,
"%3 was imported for SPI only|"
"%3 was not imported by this file|"
"C++ types from imported module %3 do not support library evolution|"
"it was imported via the internal bridging header|"
"%3 was not imported publicly}4",
(Type, Identifier, unsigned, Identifier, unsigned))
NOTE(assoc_conformance_from_implementation_only_module,none,
@@ -7317,6 +7320,7 @@ ERROR(inlinable_decl_ref_from_hidden_module,
"%2 was imported for SPI only|"
"%2 was not imported by this file|"
"C++ APIs from imported module %2 do not support library evolution|"
"it was imported via the internal bridging header|"
"%2 was not imported publicly}3",
(const ValueDecl *, unsigned, Identifier, unsigned))
@@ -7328,6 +7332,7 @@ ERROR(inlinable_typealias_desugars_to_type_from_hidden_module,
"%4 was imported for SPI only|"
"%4 was not imported by this file|"
"C++ types from imported module %4 do not support library evolution|"
"it was imported via the internal bridging header|"
"%4 was not imported publicly}5",
(const TypeAliasDecl *, StringRef, StringRef, unsigned, Identifier, unsigned))

View File

@@ -277,7 +277,8 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
D, DC, AccessLevel::Public,
[&](AttributedImport<ImportedModule> attributedImport) {
if (where.isExported() && reason != ExportabilityReason::General &&
originKind != DisallowedOriginKind::NonPublicImport) {
originKind != DisallowedOriginKind::NonPublicImport &&
originKind != DisallowedOriginKind::InternalBridgingHeaderImport) {
// These may be reported twice, for the Type and for the TypeRepr.
ModuleDecl *importedVia = attributedImport.module.importedModule,
*sourceModule = D->getModuleContext();
@@ -294,6 +295,7 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
return false;
case DisallowedOriginKind::NonPublicImport:
case DisallowedOriginKind::InternalBridgingHeaderImport:
// With a few exceptions, access levels from imports are diagnosed during
// access checking and should be skipped here.
if (!shouldDiagnoseDeclAccess(D, where))

View File

@@ -2167,8 +2167,11 @@ swift::getDisallowedOriginKind(const Decl *decl,
// See \c diagnoseValueDeclRefExportability.
auto importSource = decl->getImportAccessFrom(where.getDeclContext());
if (importSource.has_value() &&
importSource->accessLevel < AccessLevel::Public)
return DisallowedOriginKind::NonPublicImport;
importSource->accessLevel < AccessLevel::Public) {
return importSource->module.importedModule->isClangHeaderImportModule()
? DisallowedOriginKind::InternalBridgingHeaderImport
: DisallowedOriginKind::NonPublicImport;
}
return DisallowedOriginKind::None;
}

View File

@@ -47,6 +47,10 @@ enum class DisallowedOriginKind : uint8_t {
SPIOnly,
MissingImport,
FragileCxxAPI,
/// An import that is internal via the internally-imported bridging header.
InternalBridgingHeaderImport,
NonPublicImport,
None
};

View File

@@ -8,4 +8,5 @@ typedef struct {
double x, y;
} MyPoint;
typedef double MyDouble;
#endif

View File

@@ -20,3 +20,20 @@ public func getX(point: MyPoint) -> Double { point.x } // expected-error{{functi
// Comes from the macros module.
public func returnsFromMacrosModule() -> okay_t { 0 }
public protocol P {
associatedtype A
}
public struct MyType: P {
public typealias A = MyDouble
// expected-error@-1{{type alias cannot be declared public because its underlying type uses an internal type}}
// expected-note@-2{{type alias 'MyDouble' is imported by this file as 'internal' from bridging header}}
}
// expected-error@+1{{cannot use struct 'MyPoint' in an extension with public or '@usableFromInline' members; it was imported via the internal bridging header}}
extension MyPoint: P {
public typealias A = MyDouble
// expected-error@-1{{type alias cannot be declared public because its underlying type uses an internal type}}
// expected-note@-2{{type alias 'MyDouble' is imported by this file as 'internal' from bridging header}}
}