Skip to content

Commit 210bafc

Browse files
committed
Diagnose deployment constraints on type metadata involving typed throws
Fixes the rest of rdar://121530587. (cherry picked from commit 21e5066)
1 parent ca8ab16 commit 210bafc

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6688,6 +6688,11 @@ ERROR(availability_isolated_any_only_version_newer, none,
66886688
"%0 %1 or newer",
66896689
(StringRef, llvm::VersionTuple))
66906690

6691+
ERROR(availability_typed_throws_only_version_newer, none,
6692+
"runtime support for typed throws function types is only available in "
6693+
"%0 %1 or newer",
6694+
(StringRef, llvm::VersionTuple))
6695+
66916696
ERROR(availability_variadic_type_only_version_newer, none,
66926697
"parameter packs in generic types are only available in %0 %1 or newer",
66936698
(StringRef, llvm::VersionTuple))

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,6 +3031,15 @@ static bool diagnoseIsolatedAnyAvailability(
30313031
ReferenceDC);
30323032
}
30333033

3034+
static bool diagnoseTypedThrowsAvailability(
3035+
SourceRange ReferenceRange, const DeclContext *ReferenceDC) {
3036+
return TypeChecker::checkAvailability(
3037+
ReferenceRange,
3038+
ReferenceDC->getASTContext().getTypedThrowsAvailability(),
3039+
diag::availability_typed_throws_only_version_newer,
3040+
ReferenceDC);
3041+
}
3042+
30343043
static bool checkTypeMetadataAvailabilityInternal(CanType type,
30353044
SourceRange refLoc,
30363045
const DeclContext *refDC) {
@@ -3041,6 +3050,8 @@ static bool checkTypeMetadataAvailabilityInternal(CanType type,
30413050
auto isolation = fnType->getIsolation();
30423051
if (isolation.isErased())
30433052
return diagnoseIsolatedAnyAvailability(refLoc, refDC);
3053+
if (fnType.getThrownError())
3054+
return diagnoseTypedThrowsAvailability(refLoc, refDC);
30443055
}
30453056
return false;
30463057
});

test/IRGen/typed_throws.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ public enum MyBigError: Error {
1313

1414

1515
// CHECK-MANGLE: @"$s12typed_throws1XVAA1PAAWP" = hidden global [2 x ptr] [ptr @"$s12typed_throws1XVAA1PAAMc", ptr getelementptr inbounds (i8, ptr @"symbolic ySi_____YKc 12typed_throws10MyBigErrorO", {{i32|i64}} 1)]
16+
@available(SwiftStdlib 6.0, *)
1617
struct X: P {
1718
typealias A = (Int) throws(MyBigError) -> Void
1819
}
1920

2021
func requiresP<T: P>(_: T.Type) { }
22+
23+
@available(SwiftStdlib 6.0, *)
2124
func createsP() {
2225
requiresP(X.self)
2326
}
@@ -27,6 +30,7 @@ func createsP() {
2730

2831

2932
// CHECK-LABEL: define {{.*}}hidden swiftcc ptr @"$s12typed_throws13buildMetatypeypXpyF"()
33+
@available(SwiftStdlib 6.0, *)
3034
func buildMetatype() -> Any.Type {
3135
typealias Fn = (Int) throws(MyBigError) -> Void
3236

test/Parse/typed_throws.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func testClosures() {
2020
let _ = { (x: Int, y: Int) throws(MyError) -> Int in x + y }
2121
}
2222

23+
@available(SwiftStdlib 6.0, *)
2324
func testTypes() {
2425
let _ = [(Int, Int) throws(MyError) -> Int]()
2526
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 5 -module-name test
2+
3+
// REQUIRES: OS=macosx
4+
5+
// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macos14.4
6+
7+
enum MyError: Error {
8+
case fail
9+
}
10+
11+
func bad() -> Any.Type {
12+
// expected-note@-1{{add @available attribute to enclosing global function}}
13+
typealias Fn = () throws(MyError) -> ()
14+
// expected-error@+2{{runtime support for typed throws function types is only available in macOS 15.0.0 or newer}}
15+
// expected-note@+1{{add 'if #available' version check}}
16+
return Fn.self
17+
}
18+
19+
func good() -> Any.Type {
20+
typealias Fn = () throws(any Error) -> ()
21+
return Fn.self
22+
}

0 commit comments

Comments
 (0)