Skip to content

Implement typed throws support in _openExistential. #70917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand All @@ -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()};
Expand Down
20 changes: 18 additions & 2 deletions stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions test/Constraints/openExistential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 3 additions & 1 deletion test/api-digester/stability-stdlib-abi-without-asserts.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down