Skip to content

Commit

Permalink
[Availability] Diagnose unavailable conformances in UnderlyingToOpaqu…
Browse files Browse the repository at this point in the history
…eExpr.

Change the availability checker to check substitution maps of underlying
values for opaque result types to diagnose unavailable conformances. This
change also makes sure `Sendable` availability diagnostics are errors in
Swift 6 mode.
  • Loading branch information
hborla committed May 17, 2024
1 parent 1b0dca0 commit 1ec5808
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2910,7 +2910,7 @@ bool swift::diagnoseExplicitUnavailability(SourceLoc loc,
diags.diagnose(loc, diag::conformance_availability_unavailable,
type, proto,
platform.empty(), platform, EncodedMessage.Message)
.limitBehavior(behavior)
.limitBehaviorUntilSwiftVersion(behavior, 6)
.warnUntilSwiftVersionIf(warnIfConformanceUnavailablePreSwift6, 6);

switch (attr->getVersionAvailability(ctx)) {
Expand Down Expand Up @@ -3468,6 +3468,11 @@ class ExprAvailabilityWalker : public ASTWalker {
}
}

if (auto UTO = dyn_cast<UnderlyingToOpaqueExpr>(E)) {
diagnoseSubstitutionMapAvailability(
UTO->getLoc(), UTO->substitutions, Where);
}

if (auto ME = dyn_cast<MacroExpansionExpr>(E)) {
diagnoseDeclRefAvailability(
ME->getMacroRef(), ME->getMacroNameLoc().getSourceRange());
Expand Down
12 changes: 12 additions & 0 deletions test/Concurrency/sendable_checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,15 @@ extension ImplicitSendableViaMain {
struct TestImplicitSendable: Sendable {
var x: ImplicitSendableViaMain
}

struct UnavailableSendable {}

@available(*, unavailable)
extension UnavailableSendable: Sendable {}
// expected-note@-1 {{conformance of 'UnavailableSendable' to 'Sendable' has been explicitly marked unavailable here}}

@available(SwiftStdlib 5.1, *)
func checkOpaqueType() -> some Sendable {
UnavailableSendable()
// expected-warning@-1 {{conformance of 'UnavailableSendable' to 'Sendable' is unavailable; this is an error in the Swift 6 language mode}}
}
16 changes: 16 additions & 0 deletions test/Concurrency/sendable_checking_swift6.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %target-swift-frontend -emit-sil -swift-version 6 %s -o /dev/null -verify

// REQUIRES: concurrency


struct UnavailableSendable {}

@available(*, unavailable)
extension UnavailableSendable: Sendable {}
// expected-note@-1 {{conformance of 'UnavailableSendable' to 'Sendable' has been explicitly marked unavailable here}}

@available(SwiftStdlib 5.1, *)
func checkOpaqueType() -> some Sendable {
UnavailableSendable()
// expected-error@-1 {{conformance of 'UnavailableSendable' to 'Sendable' is unavailable}}
}
11 changes: 11 additions & 0 deletions test/Sema/availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,14 @@ struct DeferBody {
}
}

struct NotP {}

protocol P {}

@available(*, unavailable)
extension NotP: P {} // expected-note {{conformance of 'NotP' to 'P' has been explicitly marked unavailable here}}

@available(SwiftStdlib 5.1, *)
func requireP() -> some P {
NotP() // expected-error {{conformance of 'NotP' to 'P' is unavailable}}
}

0 comments on commit 1ec5808

Please sign in to comment.