Skip to content

[CodeCompletion] Fix a crash in solver-based keypath completion #39226

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
Sep 20, 2021
Merged
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: 9 additions & 6 deletions lib/Sema/TypeCheckCodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1303,25 +1303,28 @@ void KeyPathTypeCheckCompletionCallback::sawSolution(
// to look up type variables by their locators.
auto RootLocator =
S.getConstraintLocator(KeyPath, {ConstraintLocator::KeyPathRoot});
auto BaseVariableType =
auto BaseVariableTypeBinding =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very strange, the only reason why root is not in the bindings is key path isn't in scope of the solution since solution always records all of the type variables present in the constraint system...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My gut feeling was that it’s because the key path was skipped due to invalid code inside result builders. Do you think that might be the cause?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, maybe if part of the statement is skipped...

llvm::find_if(S.typeBindings, [&RootLocator](const auto &Entry) {
return Entry.first->getImpl().getLocator() == RootLocator;
})->getSecond();
BaseType = S.simplifyType(BaseVariableType);
});
if (BaseVariableTypeBinding != S.typeBindings.end()) {
BaseType = S.simplifyType(BaseVariableTypeBinding->getSecond());
}
}
} else {
// We are completing after a component. Get the previous component's result
// type.
BaseType = S.simplifyType(S.getType(KeyPath, ComponentIndex - 1));
}
if (BaseType.isNull()) {
return;
}

// If ExpectedTy is a duplicate of any other result, ignore this solution.
if (llvm::any_of(Results, [&](const Result &R) {
return R.BaseType->isEqual(BaseType);
})) {
return;
}
if (BaseType) {
Results.push_back({BaseType, /*OnRoot=*/(ComponentIndex == 0)});
}
Results.push_back({BaseType, /*OnRoot=*/(ComponentIndex == 0)});
}