Skip to content

[4.2][CodeComplete] Use PrintOptionalAsImplicitlyUnwrapped to print IUO #18270

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
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
15 changes: 8 additions & 7 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2559,16 +2559,17 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
// What's left is the result type.
if (ResultType->isVoid()) {
OS << "Void";
} else if (!IsImplicitlyCurriedInstanceMethod
&& FD->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>()) {
} else {
// As we did with parameters in addParamPatternFromFunction,
// for regular methods we'll print '!' after implicitly
// unwrapped optional results.
auto ObjectType = ResultType->getOptionalObjectType();
OS << ObjectType->getStringAsComponent();
OS << "!";
} else {
ResultType.print(OS);
bool IsIUO =
!IsImplicitlyCurriedInstanceMethod &&
FD->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();

PrintOptions PO;
PO.PrintOptionalAsImplicitlyUnwrapped = IsIUO;
ResultType.print(OS, PO);
}
}
Builder.addTypeAnnotation(TypeStr);
Expand Down
9 changes: 2 additions & 7 deletions lib/IDE/CodeCompletionResultBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,8 @@ class CodeCompletionResultBuilder {

PrintOptions PO;
PO.SkipAttributes = true;
std::string TypeName;
if (IsIUO) {
assert(Ty->getOptionalObjectType());
TypeName = Ty->getOptionalObjectType()->getStringAsComponent(PO) + "!";
} else {
TypeName = Ty->getString(PO);
}
PO.PrintOptionalAsImplicitlyUnwrapped = IsIUO;
std::string TypeName = Ty->getString(PO);
addChunkWithText(CodeCompletionString::Chunk::ChunkKind::CallParameterType,
TypeName);

Expand Down
14 changes: 12 additions & 2 deletions test/IDE/complete_crashes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,28 @@ func test_28188259(x: ((Int) -> Void) -> Void) {
func test_40956846(
arg_40956846_1: inout Int!,
arg_40956846_2: Void!,
arg_40956846_3: (() -> Int)!,
arg_40956846_3: (() -> Int?)!,
arg_40956846_4: inout ((Int) -> Int)!
) {
let y = #^RDAR_40956846^#
}
// RDAR_40956846: Begin completions
// RDAR_40956846-DAG: Decl[LocalVar]/Local: arg_40956846_1[#inout Int!#]; name=arg_40956846_1
// RDAR_40956846-DAG: Decl[LocalVar]/Local: arg_40956846_2[#Void!#]; name=arg_40956846_2
// RDAR_40956846-DAG: Decl[LocalVar]/Local: arg_40956846_3[#(() -> Int)!#]; name=arg_40956846_3
// RDAR_40956846-DAG: Decl[LocalVar]/Local: arg_40956846_3[#(() -> Int?)!#]; name=arg_40956846_3
// RDAR_40956846-DAG: Decl[LocalVar]/Local: arg_40956846_4[#inout ((Int) -> Int)!#]; name=arg_40956846_4
// RDAR_40956846: End completions

// rdar://problem/42443512
// RUN: %target-swift-ide-test -code-completion -code-completion-token=RDAR_42443512 -source-filename=%s | %FileCheck %s -check-prefix=RDAR_42443512
class test_42443512 {
func foo(x: Int!) { }
static func test() {
self.foo#^RDAR_42443512^#
}
}
// RDAR_42443512: Begin completions

// rdar://problem/42452085
// RUN: %target-swift-ide-test -code-completion -code-completion-token=RDAR_42452085_1 -source-filename=%s | %FileCheck %s -check-prefix=RDAR_42452085
// RUN: %target-swift-ide-test -code-completion -code-completion-token=RDAR_42452085_2 -source-filename=%s | %FileCheck %s -check-prefix=RDAR_42452085
Expand Down