Skip to content

Commit 7e1eac7

Browse files
committed
Sema: Avoid emitting superfluous resilience diagnostics with MemberImportVisibility.
When `MemberImportVisibility` is enabled, if the import that would bring a member declaration into scope is missing it is diagnosed as an error. The existing resilience diagnostics that would also diagnose the same problem in contexts that are visible in the module interface are therefore superflous with the feature enabled.
1 parent 447ce81 commit 7e1eac7

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

include/swift/AST/SourceFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,11 @@ class SourceFile final : public FileUnit {
507507
MissingImportForMemberDiagnostics[decl].push_back(loc);
508508
}
509509

510+
/// Returns true if there is a pending missing import diagnostic for \p decl.
511+
bool hasDelayedMissingImportForMemberDiagnostic(const ValueDecl *decl) const {
512+
return MissingImportForMemberDiagnostics.contains(decl);
513+
}
514+
510515
DelayedMissingImportForMemberDiags
511516
takeDelayedMissingImportForMemberDiagnostics() {
512517
DelayedMissingImportForMemberDiags diags;

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
236236

237237
auto reason = where.getExportabilityReason();
238238
auto DC = where.getDeclContext();
239+
auto SF = DC->getParentSourceFile();
239240
ASTContext &ctx = DC->getASTContext();
240241
auto originKind = getDisallowedOriginKind(D, where, downgradeToWarning);
241242

@@ -275,16 +276,18 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
275276
if (originKind == DisallowedOriginKind::None)
276277
return false;
277278

278-
auto diagName = D->getName();
279+
// Some diagnostics emitted with the `MemberImportVisibility` feature enabled
280+
// subsume these diagnostics.
281+
if (originKind == DisallowedOriginKind::MissingImport &&
282+
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility) && SF &&
283+
SF->hasDelayedMissingImportForMemberDiagnostic(D))
284+
return false;
285+
279286
if (auto accessor = dyn_cast<AccessorDecl>(D)) {
280287
// Only diagnose accessors if their disallowed origin kind differs from
281288
// that of their storage.
282289
if (getDisallowedOriginKind(accessor->getStorage(), where) == originKind)
283290
return false;
284-
285-
// For accessors, diagnose with the name of the storage instead of the
286-
// implicit '_'.
287-
diagName = accessor->getStorage()->getName();
288291
}
289292

290293
auto fragileKind = where.getFragileFunctionKind();

test/NameLookup/members_transitive_multifile_access_level.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ extension Int {
6060
internal func usesTypealiasInInternalUsesOnly(x: TypealiasInInternalUsesOnly) {} // expected-error {{type alias 'TypealiasInInternalUsesOnly' is not available due to missing import of defining module 'InternalUsesOnly'}}
6161
package func usesTypealiasInPackageUsesOnly(x: TypealiasInPackageUsesOnly) {} // expected-error {{type alias 'TypealiasInPackageUsesOnly' is not available due to missing import of defining module 'PackageUsesOnly'}}
6262
public func usesTypealiasInPublicUsesOnly(x: TypealiasInPublicUsesOnly) {} // expected-error {{type alias 'TypealiasInPublicUsesOnly' is not available due to missing import of defining module 'PublicUsesOnly'}}
63-
// expected-warning@-1 {{cannot use type alias 'TypealiasInPublicUsesOnly' here; 'PublicUsesOnly' was not imported by this file}}
6463
public func usesTypealiasInMixedUses(x: TypealiasInMixedUses) {} // expected-error {{type alias 'TypealiasInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
65-
// expected-warning@-1 {{cannot use type alias 'TypealiasInMixedUses' here; 'MixedUses' was not imported by this file}}
6664
internal func usesTypealiasInMixedUses_Internal(x: TypealiasInMixedUses) {} // expected-error {{type alias 'TypealiasInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
6765
}
6866

@@ -78,9 +76,7 @@ private func usesTypealiasInInternalUsesOnly_Private(x: Int.TypealiasInInternalU
7876
internal func usesTypealiasInInternalUsesOnly(x: Int.TypealiasInInternalUsesOnly) {} // expected-error {{type alias 'TypealiasInInternalUsesOnly' is not available due to missing import of defining module 'InternalUsesOnly'}}
7977
package func usesTypealiasInPackageUsesOnly(x: Int.TypealiasInPackageUsesOnly) {} // expected-error {{type alias 'TypealiasInPackageUsesOnly' is not available due to missing import of defining module 'PackageUsesOnly'}}
8078
public func usesTypealiasInPublicUsesOnly(x: Int.TypealiasInPublicUsesOnly) {} // expected-error {{type alias 'TypealiasInPublicUsesOnly' is not available due to missing import of defining module 'PublicUsesOnly'}}
81-
// expected-warning@-1 {{cannot use type alias 'TypealiasInPublicUsesOnly' here; 'PublicUsesOnly' was not imported by this file}}
8279
public func usesTypealiasInMixedUses(x: Int.TypealiasInMixedUses) {} // expected-error {{type alias 'TypealiasInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
83-
// expected-warning@-1 {{cannot use type alias 'TypealiasInMixedUses' here; 'MixedUses' was not imported by this file}}
8480
internal func usesTypealiasInMixedUses_Internal(x: Int.TypealiasInMixedUses) {} // expected-error {{type alias 'TypealiasInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
8581

8682
//--- extensions.swift
@@ -104,12 +100,10 @@ extension Int.NestedInPackageUsesOnly { // expected-error {{struct 'NestedInPack
104100
}
105101

106102
extension Int.NestedInPublicUsesOnly { // expected-error {{struct 'NestedInPublicUsesOnly' is not available due to missing import of defining module 'PublicUsesOnly'}}
107-
// expected-warning@-1 {{cannot use struct 'NestedInPublicUsesOnly' in an extension with public or '@usableFromInline' members; 'PublicUsesOnly' was not imported by this file}}
108103
public func publicMethod() {}
109104
}
110105

111106
extension Int.NestedInMixedUses { // expected-error {{struct 'NestedInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
112-
// expected-warning@-1 {{cannot use struct 'NestedInMixedUses' in an extension with public or '@usableFromInline' members; 'MixedUses' was not imported by this file}}
113107
public func publicMethod() {}
114108
}
115109

0 commit comments

Comments
 (0)