Implement typed throws support in _openExistential.

This commit is contained in:
Doug Gregor
2024-01-14 14:32:49 -08:00
parent 8912d4aa71
commit a30b623ee6
4 changed files with 36 additions and 5 deletions

View File

@@ -3145,11 +3145,14 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
auto result = CS.createTypeVariable(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
TVO_CanBindToNoEscape);
auto thrownError = CS.createTypeVariable(
CS.getConstraintLocator(locator, ConstraintLocator::ThrownErrorType),
0);
FunctionType::Param bodyArgs[] = {FunctionType::Param(openedTy)};
auto bodyClosure = FunctionType::get(bodyArgs, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(true)
.withThrows(true, /*FIXME:*/Type())
.withThrows(true, thrownError)
.withAsync(true)
.build());
FunctionType::Param args[] = {
@@ -3159,7 +3162,7 @@ static DeclReferenceType getTypeOfReferenceWithSpecialTypeCheckingSemantics(
auto refType = FunctionType::get(args, result,
FunctionType::ExtInfoBuilder()
.withNoEscape(false)
.withThrows(true, /*FIXME:*/Type())
.withThrows(true, thrownError)
.withAsync(true)
.build());
return {refType, refType, refType, refType, Type()};

View File

@@ -1053,12 +1053,28 @@ func __abi_withoutActuallyEscaping<ClosureType, ResultType>(
Builtin.unreachable()
}
@_alwaysEmitIntoClient
@_transparent
@_semantics("typechecker._openExistential(_:do:)")
public func _openExistential<ExistentialType, ContainedType, ResultType>(
public func _openExistential<ExistentialType, ContainedType, ResultType, Failure>(
_ existential: ExistentialType,
do body: (_ escapingClosure: ContainedType) throws(Failure) -> ResultType
) throws(Failure) -> ResultType {
// This implementation is never used, since calls to
// `Swift._openExistential(_:do:)` are resolved as a special case by
// the type checker.
Builtin.staticReport(_trueAfterDiagnostics(), true._value,
("internal consistency error: '_openExistential(_:do:)' operation failed to resolve"
as StaticString).utf8Start._rawValue)
Builtin.unreachable()
}
@usableFromInline
@_silgen_name("$ss16_openExistential_2doq0_x_q0_q_KXEtKr1_lF")
func __abi_openExistential<ExistentialType, ContainedType, ResultType>(
_ existential: ExistentialType,
do body: (_ escapingClosure: ContainedType) throws -> ResultType
) rethrows -> ResultType {
) throws -> ResultType {
// This implementation is never used, since calls to
// `Swift._openExistential(_:do:)` are resolved as a special case by
// the type checker.

View File

@@ -11,3 +11,13 @@ func open(existential: P, mutExistential: inout P) {
_openExistential(mutExistential, do: foo)
_openExistential(type(of: mutExistential), do: bar)
}
enum HomeworkError: Error {
case dogAteIt
}
func fooThrowing<T: P>(_: T) throws(HomeworkError) {}
func openMaybeThrow(existential: P) throws(HomeworkError) {
try _openExistential(existential, do: fooThrowing)
}

View File

@@ -102,7 +102,9 @@ Func Result.get() has mangled name changing from 'Swift.Result.get() throws -> A
Func withoutActuallyEscaping(_:do:) has been renamed to Func __abi_withoutActuallyEscaping(_:do:)
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'
Func withoutActuallyEscaping(_:do:) is now without @rethrows
Func _openExistential(_:do:) has been renamed to Func __abi_openExistential(_:do:)
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'
Func _openExistential(_:do:) is now without @rethrows
// These haven't actually been removed; they are simply marked unavailable.
// This seems to be a false positive in the ABI checker. This is not an ABI