Skip to content

[Async Refactoring] Add parens around custom error cast #38583

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
Jul 23, 2021
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
40 changes: 27 additions & 13 deletions lib/IDE/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7596,17 +7596,32 @@ class AsyncConverter : private SourceEntityWalker {
OS << tok::r_paren;
}

/// If the error type of \p HandlerDesc is more specialized than \c Error,
/// adds an 'as! CustomError' cast to the more specialized error type to the
/// output stream.
void
addCastToCustomErrorTypeIfNecessary(const AsyncHandlerDesc &HandlerDesc) {
const ASTContext &Ctx = HandlerDesc.getHandler()->getASTContext();
/// Adds a forwarded error argument to a completion handler call. If the error
/// type of \p HandlerDesc is more specialized than \c Error, an
/// 'as! CustomError' cast to the more specialized error type will be added to
/// the output stream.
void addForwardedErrorArgument(StringRef ErrorName,
const AsyncHandlerDesc &HandlerDesc) {
// If the error type is already Error, we can pass it as-is.
auto ErrorType = *HandlerDesc.getErrorType();
if (ErrorType->getCanonicalType() != Ctx.getExceptionType()) {
OS << " " << tok::kw_as << tok::exclaim_postfix << " ";
ErrorType->lookThroughSingleOptionalType()->print(OS);
if (ErrorType->getCanonicalType() == getASTContext().getExceptionType()) {
OS << ErrorName;
return;
}

// Otherwise we need to add a force cast to the destination custom error
// type. If this is for an Error? parameter, we'll need to add parens around
// the cast to silence a compiler warning about force casting never
// producing nil.
auto RequiresParens = HandlerDesc.getErrorParam().hasValue();
if (RequiresParens)
OS << tok::l_paren;

OS << ErrorName << " " << tok::kw_as << tok::exclaim_postfix << " ";
ErrorType->lookThroughSingleOptionalType()->print(OS);

if (RequiresParens)
OS << tok::r_paren;
}

/// If \p T has a natural default value like \c nil for \c Optional or \c ()
Expand Down Expand Up @@ -7637,8 +7652,7 @@ class AsyncConverter : private SourceEntityWalker {
if (HandlerDesc.HasError && Index == HandlerDesc.params().size() - 1) {
// The error parameter is the last argument of the completion handler.
if (ResultName.empty()) {
OS << "error";
addCastToCustomErrorTypeIfNecessary(HandlerDesc);
addForwardedErrorArgument("error", HandlerDesc);
} else {
addDefaultValueOrPlaceholder(HandlerDesc.params()[Index].getPlainType());
}
Expand Down Expand Up @@ -7707,8 +7721,8 @@ class AsyncConverter : private SourceEntityWalker {
OS << tok::period_prefix << "success" << tok::l_paren << ResultName
<< tok::r_paren;
} else {
OS << tok::period_prefix << "failure" << tok::l_paren << "error";
addCastToCustomErrorTypeIfNecessary(HandlerDesc);
OS << tok::period_prefix << "failure" << tok::l_paren;
addForwardedErrorArgument("error", HandlerDesc);
OS << tok::r_paren;
}
break;
Expand Down
4 changes: 2 additions & 2 deletions test/refactoring/ConvertAsync/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func customError(completion: (String?, CustomError?) -> Void) { }
// ASYNC-CUSTOMERROR-NEXT: let result = try await customError()
// ASYNC-CUSTOMERROR-NEXT: completion(result, nil)
// ASYNC-CUSTOMERROR-NEXT: } catch {
// ASYNC-CUSTOMERROR-NEXT: completion(nil, error as! CustomError)
// ASYNC-CUSTOMERROR-NEXT: completion(nil, (error as! CustomError))
// ASYNC-CUSTOMERROR-NEXT: }
// ASYNC-CUSTOMERROR-NEXT: }
// ASYNC-CUSTOMERROR-NEXT: }
Expand Down Expand Up @@ -308,7 +308,7 @@ func genericError<E>(completion: (String?, E?) -> Void) where E: Error { }
// GENERIC-ERROR-NEXT: let result: String = try await genericError()
// GENERIC-ERROR-NEXT: completion(result, nil)
// GENERIC-ERROR-NEXT: } catch {
// GENERIC-ERROR-NEXT: completion(nil, error as! E)
// GENERIC-ERROR-NEXT: completion(nil, (error as! E))
// GENERIC-ERROR-NEXT: }
// GENERIC-ERROR-NEXT: }
// GENERIC-ERROR-NEXT: }
Expand Down
2 changes: 1 addition & 1 deletion test/refactoring/ConvertAsync/variable_as_callback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func testGenericErrorVariableCompletionHandler<MyGenericError>(completionHandler
// GENERIC-ERROR-NEXT: let result: String = try await genericError()
// GENERIC-ERROR-NEXT: completionHandler(result, nil)
// GENERIC-ERROR-NEXT: } catch {
// GENERIC-ERROR-NEXT: completionHandler(nil, error as! MyGenericError)
// GENERIC-ERROR-NEXT: completionHandler(nil, (error as! MyGenericError))
// GENERIC-ERROR-NEXT: }

// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+2):1 | %FileCheck -check-prefix=DEFAULT-ARGS-FUNC %s
Expand Down