Skip to content

Commit b5d1ecc

Browse files
authored
Merge pull request #70917 from DougGregor/typed-throws-open-existential
Implement typed throws support in `_openExistential`.
2 parents 8ae3c9e + a30b623 commit b5d1ecc

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3145,11 +3145,14 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
31453145
auto result = CS.createTypeVariable(
31463146
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
31473147
TVO_CanBindToNoEscape);
3148+
auto thrownError = CS.createTypeVariable(
3149+
CS.getConstraintLocator(locator, ConstraintLocator::ThrownErrorType),
3150+
0);
31483151
FunctionType::Param bodyArgs[] = {FunctionType::Param(openedTy)};
31493152
auto bodyClosure = FunctionType::get(bodyArgs, result,
31503153
FunctionType::ExtInfoBuilder()
31513154
.withNoEscape(true)
3152-
.withThrows(true, /*FIXME:*/Type())
3155+
.withThrows(true, thrownError)
31533156
.withAsync(true)
31543157
.build());
31553158
FunctionType::Param args[] = {
@@ -3159,7 +3162,7 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
31593162
auto refType = FunctionType::get(args, result,
31603163
FunctionType::ExtInfoBuilder()
31613164
.withNoEscape(false)
3162-
.withThrows(true, /*FIXME:*/Type())
3165+
.withThrows(true, thrownError)
31633166
.withAsync(true)
31643167
.build());
31653168
return {refType, refType, refType, refType, Type()};

stdlib/public/core/Builtin.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,12 +1053,28 @@ func __abi_withoutActuallyEscaping<ClosureType, ResultType>(
10531053
Builtin.unreachable()
10541054
}
10551055

1056+
@_alwaysEmitIntoClient
10561057
@_transparent
10571058
@_semantics("typechecker._openExistential(_:do:)")
1058-
public func _openExistential<ExistentialType, ContainedType, ResultType>(
1059+
public func _openExistential<ExistentialType, ContainedType, ResultType, Failure>(
1060+
_ existential: ExistentialType,
1061+
do body: (_ escapingClosure: ContainedType) throws(Failure) -> ResultType
1062+
) throws(Failure) -> ResultType {
1063+
// This implementation is never used, since calls to
1064+
// `Swift._openExistential(_:do:)` are resolved as a special case by
1065+
// the type checker.
1066+
Builtin.staticReport(_trueAfterDiagnostics(), true._value,
1067+
("internal consistency error: '_openExistential(_:do:)' operation failed to resolve"
1068+
as StaticString).utf8Start._rawValue)
1069+
Builtin.unreachable()
1070+
}
1071+
1072+
@usableFromInline
1073+
@_silgen_name("$ss16_openExistential_2doq0_x_q0_q_KXEtKr1_lF")
1074+
func __abi_openExistential<ExistentialType, ContainedType, ResultType>(
10591075
_ existential: ExistentialType,
10601076
do body: (_ escapingClosure: ContainedType) throws -> ResultType
1061-
) rethrows -> ResultType {
1077+
) throws -> ResultType {
10621078
// This implementation is never used, since calls to
10631079
// `Swift._openExistential(_:do:)` are resolved as a special case by
10641080
// the type checker.

test/Constraints/openExistential.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@ func open(existential: P, mutExistential: inout P) {
1111
_openExistential(mutExistential, do: foo)
1212
_openExistential(type(of: mutExistential), do: bar)
1313
}
14+
15+
enum HomeworkError: Error {
16+
case dogAteIt
17+
}
18+
19+
func fooThrowing<T: P>(_: T) throws(HomeworkError) {}
20+
21+
func openMaybeThrow(existential: P) throws(HomeworkError) {
22+
try _openExistential(existential, do: fooThrowing)
23+
}

test/api-digester/stability-stdlib-abi-without-asserts.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ Func Result.get() has mangled name changing from 'Swift.Result.get() throws -> A
102102
Func withoutActuallyEscaping(_:do:) has been renamed to Func __abi_withoutActuallyEscaping(_:do:)
103103
Func withoutActuallyEscaping(_:do:) has mangled name changing from 'Swift.withoutActuallyEscaping<A, B>(_: A, do: (A) throws -> B) throws -> B' to 'Swift.__abi_withoutActuallyEscaping<A, B>(_: A, do: (A) throws -> B) throws -> B'
104104
Func withoutActuallyEscaping(_:do:) is now without @rethrows
105-
105+
Func _openExistential(_:do:) has been renamed to Func __abi_openExistential(_:do:)
106+
Func _openExistential(_:do:) has mangled name changing from 'Swift._openExistential<A, B, C>(_: A, do: (B) throws -> C) throws -> C' to 'Swift.__abi_openExistential<A, B, C>(_: A, do: (B) throws -> C) throws -> C'
107+
Func _openExistential(_:do:) is now without @rethrows
106108

107109
// These haven't actually been removed; they are simply marked unavailable.
108110
// This seems to be a false positive in the ABI checker. This is not an ABI

0 commit comments

Comments
 (0)