Skip to content

Commit ea65a09

Browse files
committed
[CodeComplete] Fix crasher when completing inout IUO variable
Use `PrintOptionalAsImplicitlyUnwrapped` option. rdar://problem/40956846
1 parent e4ddba2 commit ea65a09

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,17 +1961,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
19611961
// lookups is IUO, not Optional as it is for the @optional attribute.
19621962
if (dynamicOrOptional) {
19631963
T = T->getOptionalObjectType();
1964-
suffix = "!?";
1965-
} else {
1966-
suffix = "!";
1964+
suffix = "?";
19671965
}
19681966

1969-
Type ObjectType = T->getReferenceStorageReferent()->getOptionalObjectType();
1970-
1971-
if (ObjectType->isVoid())
1972-
Builder.addTypeAnnotation("Void" + suffix);
1973-
else
1974-
Builder.addTypeAnnotation(ObjectType.getStringAsComponent() + suffix);
1967+
T = T->getReferenceStorageReferent();
1968+
PrintOptions PO;
1969+
PO.PrintOptionalAsImplicitlyUnwrapped = true;
1970+
Builder.addTypeAnnotation(T.getString(PO) + suffix);
19751971
}
19761972

19771973
/// For printing in code completion results, replace archetypes with

test/IDE/complete_crashes.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,20 @@ func test_28188259(x: ((Int) -> Void) -> Void) {
295295
// RDAR_28188259: Begin completions
296296
// RDAR_28188259-DAG: Pattern/CurrModule: ({#_#})[#Void#]; name=(_)
297297
// RDAR_28188259: End completions
298+
299+
// rdar://problem/40956846
300+
// RUN: %target-swift-ide-test -code-completion -code-completion-token=RDAR_40956846 -source-filename=%s | %FileCheck %s -check-prefix=RDAR_40956846
301+
func test_40956846(
302+
arg_40956846_1: inout Int!,
303+
arg_40956846_2: Void!,
304+
arg_40956846_3: (() -> Int)!,
305+
arg_40956846_4: inout ((Int) -> Int)!
306+
) {
307+
let y = #^RDAR_40956846^#
308+
}
309+
// RDAR_40956846: Begin completions
310+
// RDAR_40956846-DAG: Decl[LocalVar]/Local: arg_40956846_1[#inout Int!#]; name=arg_40956846_1
311+
// RDAR_40956846-DAG: Decl[LocalVar]/Local: arg_40956846_2[#Void!#]; name=arg_40956846_2
312+
// RDAR_40956846-DAG: Decl[LocalVar]/Local: arg_40956846_3[#(() -> Int)!#]; name=arg_40956846_3
313+
// RDAR_40956846-DAG: Decl[LocalVar]/Local: arg_40956846_4[#inout ((Int) -> Int)!#]; name=arg_40956846_4
314+
// RDAR_40956846: End completions

0 commit comments

Comments
 (0)