Skip to content

[CodeComplete] Restore original type in getOperatorCompletions #17421

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

Closed
wants to merge 1 commit into from
Closed
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: 14 additions & 1 deletion lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3268,8 +3268,17 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
}

void tryPostfixOperator(Expr *expr, PostfixOperatorDecl *op) {
if (!expr->getType())
auto Ty = expr->getType();
if (!Ty)
return;

SWIFT_DEFER {
// Restore type.
// FIXME: This is workaround for getTypeOfExpressionWithoutApplying()
// modifies type of 'expr'.
expr->setType(Ty);
};

// We allocate these expressions on the stack because we know they can't
// escape and there isn't a better way to allocate scratch Expr nodes.
UnresolvedDeclRefExpr UDRE(op->getName(), DeclRefKind::PostfixOperator,
Expand Down Expand Up @@ -3344,6 +3353,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
LHS->getType()->is<AnyFunctionType>()))
return;

// Preserve LHS type for restoring it.
Type LHSTy = LHS->getType();

// We allocate these expressions on the stack because we know they can't
// escape and there isn't a better way to allocate scratch Expr nodes.
UnresolvedDeclRefExpr UDRE(op->getName(), DeclRefKind::BinaryOperator,
Expand All @@ -3356,6 +3368,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
// Reset sequence.
SE->setElement(SE->getNumElements() - 1, nullptr);
SE->setElement(SE->getNumElements() - 2, nullptr);
LHS->setType(LHSTy);
prepareForRetypechecking(SE);

// Reset any references to operators in types, so they are properly
Expand Down
11 changes: 11 additions & 0 deletions test/IDE/complete_crashes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,14 @@ func foo_38272904(a: A_38272904) {
bar_38272904(a: .foo() #^RDAR_38272904^#)
}
// RDAR_38272904: Begin completions


// RUN: %target-swift-ide-test -code-completion -code-completion-token=RDAR_28188259 -source-filename=%s | %FileCheck %s -check-prefix=RDAR_28188259

func test_28188259(x: ((Int) -> Void) -> Void) {
x({_ in }#^RDAR_28188259^#)
}
// RDAR_28188259: Begin completions
// RDAR_28188259: Pattern/CurrModule: ({#_#})[#Void#]; name=(_)
// RDAR_28188259: Keyword[self]/CurrNominal: .self[#(_) -> ()#]; name=self
// RDAR_28188259: End completions