Skip to content

Commit f8a3afb

Browse files
committed
Fix lifetime of type context in result builder
There usually won't be a lot of expected types, so just use a SmallVector and copy it instead of trying to get tricky with the lifetime.
1 parent b817cbb commit f8a3afb

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -867,25 +867,6 @@ ArrayRef<StringRef> copyAssociatedUSRs(llvm::BumpPtrAllocator &Allocator,
867867
return ArrayRef<StringRef>();
868868
}
869869

870-
/// The expected contextual type(s) for code-completion.
871-
struct ide::ExpectedTypeContext {
872-
/// Possible types of the code completion expression.
873-
std::vector<Type> possibleTypes;
874-
875-
/// Whether the `ExpectedTypes` comes from a single-expression body, e.g.
876-
/// `foo({ here })`.
877-
///
878-
/// Since the input may be incomplete, we take into account that the types are
879-
/// only a hint.
880-
bool isSingleExpressionBody = false;
881-
882-
bool empty() const { return possibleTypes.empty(); }
883-
884-
ExpectedTypeContext() = default;
885-
ExpectedTypeContext(std::vector<Type> types, bool isSingleExpressionBody)
886-
: possibleTypes(types), isSingleExpressionBody(isSingleExpressionBody) {}
887-
};
888-
889870
static CodeCompletionResult::ExpectedTypeRelation calculateTypeRelation(
890871
Type Ty,
891872
Type ExpectedTy,
@@ -1289,11 +1270,10 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
12891270
if (ST.isNull() || ST->is<ErrorType>())
12901271
return;
12911272

1292-
ExpectedTypeContext empty;
12931273
CodeCompletionResultBuilder Builder(Sink,
12941274
CodeCompletionResult::ResultKind::Keyword,
12951275
SemanticContextKind::CurrentNominal,
1296-
empty);
1276+
{});
12971277
Builder.setKeywordKind(CodeCompletionKeywordKind::kw_super);
12981278
Builder.addTextChunk("super");
12991279
Builder.addTypeAnnotation(ST.getString());

lib/IDE/CodeCompletionResultBuilder.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,25 @@ class ModuleDecl;
3131

3232
namespace ide {
3333

34-
struct ExpectedTypeContext;
34+
/// The expected contextual type(s) for code-completion.
35+
struct ExpectedTypeContext {
36+
/// Possible types of the code completion expression.
37+
llvm::SmallVector<Type, 4> possibleTypes;
38+
39+
/// Whether the `ExpectedTypes` comes from a single-expression body, e.g.
40+
/// `foo({ here })`.
41+
///
42+
/// Since the input may be incomplete, we take into account that the types are
43+
/// only a hint.
44+
bool isSingleExpressionBody = false;
45+
46+
bool empty() const { return possibleTypes.empty(); }
47+
48+
ExpectedTypeContext() = default;
49+
ExpectedTypeContext(ArrayRef<Type> types, bool isSingleExpressionBody)
50+
: possibleTypes(types.begin(), types.end()),
51+
isSingleExpressionBody(isSingleExpressionBody) {}
52+
};
3553

3654
class CodeCompletionResultBuilder {
3755
CodeCompletionResultSink &Sink;
@@ -45,7 +63,7 @@ class CodeCompletionResultBuilder {
4563
SmallVector<CodeCompletionString::Chunk, 4> Chunks;
4664
llvm::PointerUnion<const ModuleDecl *, const clang::Module *>
4765
CurrentModule;
48-
const ExpectedTypeContext &declTypeContext;
66+
ExpectedTypeContext declTypeContext;
4967
CodeCompletionResult::ExpectedTypeRelation ExpectedTypeRelation =
5068
CodeCompletionResult::Unrelated;
5169
bool Cancelled = false;

0 commit comments

Comments
 (0)