Skip to content

[CodeCompletion] Remove unresolved type in prepareForRetypechecking() #18721

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 2 commits into from
Aug 16, 2018
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
14 changes: 9 additions & 5 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,21 +412,24 @@ static void prepareForRetypechecking(Expr *E) {
assert(E);
struct Eraser : public ASTWalker {
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
if (expr && expr->getType() && expr->getType()->hasError())
if (expr && expr->getType() && (expr->getType()->hasError() ||
expr->getType()->hasUnresolvedType()))
expr->setType(Type());
if (auto *ACE = dyn_cast_or_null<AutoClosureExpr>(expr)) {
return { true, ACE->getSingleExpressionBody() };
}
return { true, expr };
}
bool walkToTypeLocPre(TypeLoc &TL) override {
if (TL.getType() && TL.getType()->hasError())
if (TL.getType() && (TL.getType()->hasError() ||
TL.getType()->hasUnresolvedType()))
TL.setType(Type());
return true;
}

std::pair<bool, Pattern*> walkToPatternPre(Pattern *P) override {
if (P && P->hasType() && P->getType()->hasError()) {
if (P && P->hasType() && (P->getType()->hasError() ||
P->getType()->hasUnresolvedType())) {
P->setType(Type());
}
return { true, P };
Expand Down Expand Up @@ -3304,6 +3307,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
// FIXME: This is workaround for getTypeOfExpressionWithoutApplying()
// modifies type of 'expr'.
expr->setType(Ty);
prepareForRetypechecking(expr);
};

// We allocate these expressions on the stack because we know they can't
Expand Down Expand Up @@ -3425,8 +3429,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
Expr *expr = SE;
if (!typeCheckCompletionSequence(const_cast<DeclContext *>(CurrDeclContext),
expr)) {

if (!LHS->getType()->getRValueType()->getOptionalObjectType()) {
if (!LHS->getType() ||
!LHS->getType()->getRValueType()->getOptionalObjectType()) {
// Don't complete optional operators on non-optional types.
// FIXME: can we get the type-checker to disallow these for us?
if (op->getName().str() == "??")
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ void ConstraintSystem::PotentialBindings::addPotentialBinding(
// check whether we can combine it with another
// supertype binding by computing the 'join' of the types.
if (binding.Kind == AllowedBindingKind::Supertypes &&
!binding.BindingType->hasUnresolvedType() &&
!binding.BindingType->hasTypeVariable() &&
!binding.BindingType->hasUnboundGenericType() &&
!binding.DefaultedProtocol && !binding.isDefaultableBinding() &&
Expand Down
9 changes: 2 additions & 7 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,7 @@ bool TypeChecker::typeCheckCompletionSequence(Expr *&expr, DeclContext *DC) {

// Attempt to solve the constraint system.
SmallVector<Solution, 4> viable;
if (CS.solve(expr, viable, FreeTypeVariableBinding::UnresolvedType))
if (CS.solve(expr, viable, FreeTypeVariableBinding::Disallow))
return true;

auto &solution = viable[0];
Expand All @@ -2255,12 +2255,7 @@ bool TypeChecker::typeCheckCompletionSequence(Expr *&expr, DeclContext *DC) {
auto &solutionCS = solution.getConstraintSystem();
expr->setType(solution.simplifyType(solutionCS.getType(expr)));
auto completionType = solution.simplifyType(solutionCS.getType(CCE));

// If completion expression is unresolved it doesn't provide
// any meaningful information so shouldn't be in the results.
if (completionType->is<UnresolvedType>())
return true;

assert(!completionType->hasUnresolvedType());
CCE->setType(completionType);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion test/SourceKit/CodeComplete/complete_inner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func test010(x: E1, y: FooBar) {
// RUN: %sourcekitd-test -req=complete.open -pos=26:11 -req-opts=filtertext=one %s -- %s | %FileCheck %s -check-prefix=INNER_POSTFIX_0b
// INNER_POSTFIX_0b-NOT: key.description: "one{{.+}}"
// INNER_POSTFIX_0b: key.description: "one",{{$}}
// INNER_POSTFIX_0b: key.description: "one...",{{$}}
// INNER_POSTFIX_0b: key.description: "one.",{{$}}
// INNER_POSTFIX_0b-NOT: key.description: "one{{.+}}"

// RUN: %sourcekitd-test -req=complete.open -pos=29:9 -req-opts=filtertext=pro %s -- %s | %FileCheck %s -check-prefix=INNER_POSTFIX_1
Expand Down
6 changes: 6 additions & 0 deletions validation-test/IDE/crashers_2_fixed/rdar41224316.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %target-swift-ide-test -code-completion -code-completion-token=A -source-filename=%s
// REQUIRES: asserts

func test(str: String?) {
_ = str == nil #^A^#
}