Skip to content

Commit 21e5066

Browse files
committed
Diagnose deployment constraints on type metadata involving typed throws
Fixes the rest of rdar://121530587.
1 parent 9604019 commit 21e5066

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
@@ -6735,6 +6735,11 @@ ERROR(availability_isolated_any_only_version_newer, none,
67356735
"%0 %1 or newer",
67366736
(StringRef, llvm::VersionTuple))
67376737

6738+
ERROR(availability_typed_throws_only_version_newer, none,
6739+
"runtime support for typed throws function types is only available in "
6740+
"%0 %1 or newer",
6741+
(StringRef, llvm::VersionTuple))
6742+
67386743
ERROR(availability_variadic_type_only_version_newer, none,
67396744
"parameter packs in generic types are only available in %0 %1 or newer",
67406745
(StringRef, llvm::VersionTuple))

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,15 @@ static bool diagnoseIsolatedAnyAvailability(
30233023
ReferenceDC);
30243024
}
30253025

3026+
static bool diagnoseTypedThrowsAvailability(
3027+
SourceRange ReferenceRange, const DeclContext *ReferenceDC) {
3028+
return TypeChecker::checkAvailability(
3029+
ReferenceRange,
3030+
ReferenceDC->getASTContext().getTypedThrowsAvailability(),
3031+
diag::availability_typed_throws_only_version_newer,
3032+
ReferenceDC);
3033+
}
3034+
30263035
static bool checkTypeMetadataAvailabilityInternal(CanType type,
30273036
SourceRange refLoc,
30283037
const DeclContext *refDC) {
@@ -3033,6 +3042,8 @@ static bool checkTypeMetadataAvailabilityInternal(CanType type,
30333042
auto isolation = fnType->getIsolation();
30343043
if (isolation.isErased())
30353044
return diagnoseIsolatedAnyAvailability(refLoc, refDC);
3045+
if (fnType.getThrownError())
3046+
return diagnoseTypedThrowsAvailability(refLoc, refDC);
30363047
}
30373048
return false;
30383049
});

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)