Skip to content

Commit 76f9337

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents af68664 + acc6c10 commit 76f9337

28 files changed

+388
-461
lines changed

include/swift/IDE/AfterPoundExprCompletion.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ class AfterPoundExprCompletion : public TypeCheckCompletionCallback {
4747
: CompletionExpr(CompletionExpr), DC(DC), ParentStmtKind(ParentStmtKind) {
4848
}
4949

50-
void deliverResults(ide::CodeCompletionContext &CompletionCtx,
51-
CodeCompletionConsumer &Consumer);
50+
void collectResults(ide::CodeCompletionContext &CompletionCtx);
5251
};
5352

5453
} // end namespace ide

include/swift/IDE/ArgumentCompletion.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,48 @@ class ArgumentTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
2525
struct Result {
2626
/// The type associated with the code completion expression itself.
2727
Type ExpectedType;
28+
2829
/// The expected return type of the function call.
2930
Type ExpectedCallType;
31+
3032
/// True if this is a subscript rather than a function call.
3133
bool IsSubscript;
34+
3235
/// The FuncDecl or SubscriptDecl associated with the call.
3336
ValueDecl *FuncD;
37+
3438
/// The type of the function being called.
3539
AnyFunctionType *FuncTy;
40+
3641
/// The index of the argument containing the completion location
3742
unsigned ArgIdx;
43+
3844
/// The index of the parameter corresponding to the completion argument.
3945
llvm::Optional<unsigned> ParamIdx;
46+
4047
/// The indices of all params that were bound to non-synthesized
4148
/// arguments. Used so we don't suggest them even when the args are out of
4249
/// order.
4350
std::set<unsigned> ClaimedParamIndices;
51+
4452
/// True if the completion is a noninitial term in a variadic argument.
4553
bool IsNoninitialVariadic;
54+
4655
/// The base type of the call/subscript (null for free functions).
4756
Type BaseType;
57+
4858
/// True if an argument label precedes the completion location.
4959
bool HasLabel;
60+
61+
/// The argument index of the first trailing closure.
62+
///
63+
/// \c None if the call doesn't have a trailing closure.
64+
llvm::Optional<unsigned> FirstTrailingClosureIndex;
65+
5066
/// Whether the surrounding context is async and thus calling async
5167
/// functions is supported.
5268
bool IsInAsyncContext;
69+
5370
/// A bitfield to mark whether the parameter at a given index is optional.
5471
/// Parameters can be optional if they have a default argument or belong to
5572
/// a parameter pack.
@@ -93,9 +110,12 @@ class ArgumentTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
93110
/// function signature instead of suggesting individual labels. Used when
94111
/// completing after the opening '(' of a function call \param Loc The
95112
/// location of the code completion token
96-
void deliverResults(bool IncludeSignature, SourceLoc Loc, DeclContext *DC,
97-
CodeCompletionContext &CompletionCtx,
98-
CodeCompletionConsumer &Consumer);
113+
/// \param IsLabeledTrailingClosure Whether we are completing the label of a
114+
/// labeled trailing closure, ie. if the code completion location is outside
115+
/// the call after the first trailing closure of the call.
116+
void collectResults(bool IncludeSignature, bool IsLabeledTrailingClosure,
117+
SourceLoc Loc, DeclContext *DC,
118+
CodeCompletionContext &CompletionCtx);
99119
};
100120

101121
} // end namespace ide

include/swift/IDE/CodeCompletion.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ void postProcessCompletionResults(
6565
MutableArrayRef<CodeCompletionResult *> results, CompletionKind Kind,
6666
const DeclContext *DC, CodeCompletionResultSink *Sink);
6767

68-
void deliverCompletionResults(CodeCompletionContext &CompletionContext,
68+
void collectCompletionResults(CodeCompletionContext &CompletionContext,
6969
CompletionLookup &Lookup, DeclContext *DC,
70-
CodeCompletionConsumer &Consumer);
70+
const ExpectedTypeContext &TypeContext,
71+
bool CanCurrDeclContextHandleAsync);
7172

7273
/// Create a factory for code completion callbacks.
7374
IDEInspectionCallbacksFactory *

include/swift/IDE/CodeCompletionConsumer.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,9 @@ namespace ide {
2222
struct RequestedCachedModule;
2323

2424
/// An abstract base class for consumers of code completion results.
25-
/// \see \c SimpleCachingCodeCompletionConsumer.
2625
class CodeCompletionConsumer {
2726
public:
2827
virtual ~CodeCompletionConsumer() {}
29-
virtual void
30-
handleResultsAndModules(CodeCompletionContext &context,
31-
ArrayRef<RequestedCachedModule> requestedModules,
32-
const ExpectedTypeContext *TypeContext,
33-
const DeclContext *DC,
34-
bool CanCurrDeclContextHandleAsync) = 0;
35-
};
36-
37-
/// A simplified code completion consumer interface that clients can use to get
38-
/// CodeCompletionResults with automatic caching of top-level completions from
39-
/// imported modules.
40-
struct SimpleCachingCodeCompletionConsumer : public CodeCompletionConsumer {
41-
42-
// Implement the CodeCompletionConsumer interface.
43-
void handleResultsAndModules(CodeCompletionContext &context,
44-
ArrayRef<RequestedCachedModule> requestedModules,
45-
const ExpectedTypeContext *TypeContext,
46-
const DeclContext *DCForModules,
47-
bool CanCurrDeclContextHandleAsync) override;
48-
4928
/// Clients should override this method to receive \p Results.
5029
virtual void handleResults(CodeCompletionContext &context) = 0;
5130
};

include/swift/IDE/CodeCompletionContext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace swift {
2020
namespace ide {
2121

2222
class CodeCompletionCache;
23+
struct RequestedCachedModule;
2324

2425
class CodeCompletionContext {
2526
friend class CodeCompletionResultBuilder;
@@ -103,6 +104,13 @@ class CodeCompletionContext {
103104
sortCompletionResults(ArrayRef<CodeCompletionResult *> Results);
104105

105106
CodeCompletionResultSink &getResultSink() { return CurrentResults; }
107+
108+
/// Add code completion results from the given requested modules to this
109+
/// context.
110+
void addResultsFromModules(ArrayRef<RequestedCachedModule> RequestedModules,
111+
const ExpectedTypeContext &TypeContext,
112+
const DeclContext *DC,
113+
bool CanCurrDeclContextHandleAsync);
106114
};
107115

108116
} // end namespace ide

include/swift/IDE/CodeCompletionResult.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ enum class CompletionKind : uint8_t {
211211
EffectsSpecifier,
212212
PoundAvailablePlatform,
213213
CallArg,
214-
LabeledTrailingClosure,
215214
ReturnStmtExpr,
216215
ThenStmtExpr,
217216
YieldStmtExpr,

include/swift/IDE/CodeCompletionResultType.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ class ExpectedTypeContext {
7474
}
7575
}
7676

77+
/// Form a union of this expected type context with \p Other.
78+
///
79+
/// Any possible type from either type context will be considered a possible
80+
/// type in the merged type context.
81+
void merge(const ExpectedTypeContext &Other) {
82+
PossibleTypes.append(Other.PossibleTypes);
83+
84+
// We can't merge ideal types. If they are different, setting to a null type
85+
// is the best thing we can do.
86+
if (!IdealType || !Other.IdealType || !IdealType->isEqual(Other.IdealType)) {
87+
IdealType = Type();
88+
}
89+
IsImplicitSingleExpressionReturn |= Other.IsImplicitSingleExpressionReturn;
90+
PreferNonVoid &= Other.PreferNonVoid;
91+
ExpectedCustomAttributeKinds |= Other.ExpectedCustomAttributeKinds;
92+
}
93+
7794
Type getIdealType() const { return IdealType; }
7895

7996
void setIdealType(Type IdealType) { this->IdealType = IdealType; }

include/swift/IDE/ExprCompletion.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ class ExprTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
9090
AddUnresolvedMemberCompletions(AddUnresolvedMemberCompletions) {}
9191

9292
/// \param CCLoc The location of the code completion token.
93-
void deliverResults(SourceLoc CCLoc,
94-
ide::CodeCompletionContext &CompletionCtx,
95-
CodeCompletionConsumer &Consumer);
93+
void collectResults(SourceLoc CCLoc,
94+
ide::CodeCompletionContext &CompletionCtx);
9695
};
9796

9897
} // end namespace ide

include/swift/IDE/KeyPathCompletion.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ class KeyPathTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
3737
public:
3838
KeyPathTypeCheckCompletionCallback(KeyPathExpr *KeyPath) : KeyPath(KeyPath) {}
3939

40-
void deliverResults(DeclContext *DC, SourceLoc DotLoc,
41-
ide::CodeCompletionContext &CompletionCtx,
42-
CodeCompletionConsumer &Consumer);
40+
void collectResults(DeclContext *DC, SourceLoc DotLoc,
41+
ide::CodeCompletionContext &CompletionCtx);
4342
};
4443

4544
} // end namespace ide

include/swift/IDE/PostfixCompletion.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,9 @@ class PostfixCompletionCallback : public TypeCheckCompletionCallback {
103103
/// \p DotLoc is invalid
104104
/// \param HasLeadingSpace Whether there is a space separating the exiting
105105
/// expression and the code completion token.
106-
void deliverResults(SourceLoc DotLoc, bool IsInSelector,
106+
void collectResults(SourceLoc DotLoc, bool IsInSelector,
107107
bool IncludeOperators, bool HasLeadingSpace,
108-
CodeCompletionContext &CompletionCtx,
109-
CodeCompletionConsumer &Consumer);
108+
CodeCompletionContext &CompletionCtx);
110109
};
111110

112111
} // end namespace ide

include/swift/IDE/UnresolvedMemberCompletion.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ class UnresolvedMemberTypeCheckCompletionCallback
5959
CodeCompletionExpr *CompletionExpr, DeclContext *DC)
6060
: CompletionExpr(CompletionExpr), DC(DC) {}
6161

62-
void deliverResults(DeclContext *DC, SourceLoc DotLoc,
63-
ide::CodeCompletionContext &CompletionCtx,
64-
CodeCompletionConsumer &Consumer);
62+
void collectResults(DeclContext *DC, SourceLoc DotLoc,
63+
ide::CodeCompletionContext &CompletionCtx);
6564
};
6665

6766
} // end namespace ide

include/swift/Parse/IDEInspectionCallbacks.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,7 @@ class CodeCompletionCallbacks {
230230
return false;
231231
}
232232

233-
virtual void completeLabeledTrailingClosure(CodeCompletionExpr *E,
234-
bool isAtStartOfLine) {};
235-
236-
virtual void completeReturnStmt(CodeCompletionExpr *E) {};
233+
virtual void completeReturnStmt(CodeCompletionExpr *E){};
237234

238235
virtual void completeThenStmt(CodeCompletionExpr *E) {};
239236

lib/IDE/AfterPoundExprCompletion.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,29 @@ void AfterPoundExprCompletion::sawSolutionImpl(const constraints::Solution &S) {
3737
}
3838
}
3939

40-
void AfterPoundExprCompletion::deliverResults(
41-
ide::CodeCompletionContext &CompletionCtx,
42-
CodeCompletionConsumer &Consumer) {
40+
void AfterPoundExprCompletion::collectResults(
41+
ide::CodeCompletionContext &CompletionCtx) {
4342
ASTContext &Ctx = DC->getASTContext();
4443
CompletionLookup Lookup(CompletionCtx.getResultSink(), Ctx, DC,
4544
&CompletionCtx);
4645

4746
Lookup.shouldCheckForDuplicates(Results.size() > 1);
4847

48+
// The type context that is being used for global results.
49+
ExpectedTypeContext UnifiedTypeContext;
50+
UnifiedTypeContext.setPreferNonVoid(true);
51+
4952
for (auto &Result : Results) {
5053
Lookup.setExpectedTypes({Result.ExpectedTy},
5154
Result.IsImplicitSingleExpressionReturn,
5255
/*expectsNonVoid=*/true);
5356
Lookup.addPoundAvailable(ParentStmtKind);
5457
Lookup.addObjCPoundKeywordCompletions(/*needPound=*/false);
5558
Lookup.getMacroCompletions(CodeCompletionMacroRole::Expression);
59+
60+
UnifiedTypeContext.merge(*Lookup.getExpectedTypeContext());
5661
}
5762

58-
deliverCompletionResults(CompletionCtx, Lookup, DC, Consumer);
63+
collectCompletionResults(CompletionCtx, Lookup, DC, UnifiedTypeContext,
64+
/*CanCurrDeclContextHandleAsync=*/false);
5965
}

lib/IDE/ArgumentCompletion.cpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ bool ArgumentTypeCheckCompletionCallback::addPossibleParams(
5050
const AnyFunctionType::Param *TypeParam = &ParamsToPass[Idx];
5151
bool Required = !Res.DeclParamIsOptional[Idx];
5252

53+
if (Res.FirstTrailingClosureIndex && Idx > *Res.FirstTrailingClosureIndex &&
54+
!TypeParam->getPlainType()
55+
->lookThroughAllOptionalTypes()
56+
->is<AnyFunctionType>()) {
57+
// We are completing an argument after the first trailing closure, i.e.
58+
// a multitple trailing closure label but the parameter is not a function
59+
// type. Since we only allow labeled trailing closures after the first
60+
// trailing closure, we cannot pass an argument for this parameter.
61+
// If the parameter is required, stop here since we cannot pass an argument
62+
// for the parameter. If it's optional, keep looking for more trailing
63+
// closures that can be passed.
64+
if (Required) {
65+
break;
66+
} else {
67+
continue;
68+
}
69+
}
70+
5371
if (TypeParam->hasLabel() && !(IsCompletion && Res.IsNoninitialVariadic)) {
5472
// Suggest parameter label if parameter has label, we are completing in it
5573
// and it is not a variadic parameter that already has arguments
@@ -198,9 +216,11 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
198216
}
199217

200218
bool HasLabel = false;
219+
llvm::Optional<unsigned> FirstTrailingClosureIndex = llvm::None;
201220
if (auto PE = CS.getParentExpr(CompletionExpr)) {
202221
if (auto Args = PE->getArgs()) {
203222
HasLabel = !Args->getLabel(ArgIdx).empty();
223+
FirstTrailingClosureIndex = Args->getFirstTrailingClosureIndex();
204224
}
205225
}
206226

@@ -258,11 +278,11 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
258278
}
259279
}
260280

261-
Results.push_back({ExpectedTy, ExpectedCallType,
262-
isa<SubscriptExpr>(ParentCall), Info.getValue(), FuncTy,
263-
ArgIdx, ParamIdx, std::move(ClaimedParams),
264-
IsNoninitialVariadic, Info.BaseTy, HasLabel, IsAsync,
265-
DeclParamIsOptional, SolutionSpecificVarTypes});
281+
Results.push_back(
282+
{ExpectedTy, ExpectedCallType, isa<SubscriptExpr>(ParentCall),
283+
Info.getValue(), FuncTy, ArgIdx, ParamIdx, std::move(ClaimedParams),
284+
IsNoninitialVariadic, Info.BaseTy, HasLabel, FirstTrailingClosureIndex,
285+
IsAsync, DeclParamIsOptional, SolutionSpecificVarTypes});
266286
}
267287

268288
void ArgumentTypeCheckCompletionCallback::computeShadowedDecls(
@@ -295,10 +315,9 @@ void ArgumentTypeCheckCompletionCallback::computeShadowedDecls(
295315
}
296316
}
297317

298-
void ArgumentTypeCheckCompletionCallback::deliverResults(
299-
bool IncludeSignature, SourceLoc Loc, DeclContext *DC,
300-
ide::CodeCompletionContext &CompletionCtx,
301-
CodeCompletionConsumer &Consumer) {
318+
void ArgumentTypeCheckCompletionCallback::collectResults(
319+
bool IncludeSignature, bool IsLabeledTrailingClosure, SourceLoc Loc,
320+
DeclContext *DC, ide::CodeCompletionContext &CompletionCtx) {
302321
ASTContext &Ctx = DC->getASTContext();
303322
CompletionLookup Lookup(CompletionCtx.getResultSink(), Ctx, DC,
304323
&CompletionCtx);
@@ -375,7 +394,7 @@ void ArgumentTypeCheckCompletionCallback::deliverResults(
375394
shouldPerformGlobalCompletion |=
376395
addPossibleParams(Ret, Params, ExpectedTypes);
377396
}
378-
Lookup.addCallArgumentCompletionResults(Params);
397+
Lookup.addCallArgumentCompletionResults(Params, IsLabeledTrailingClosure);
379398
}
380399

381400
if (shouldPerformGlobalCompletion) {
@@ -402,5 +421,7 @@ void ArgumentTypeCheckCompletionCallback::deliverResults(
402421
addExprKeywords(CompletionCtx.getResultSink(), DC);
403422
}
404423

405-
deliverCompletionResults(CompletionCtx, Lookup, DC, Consumer);
424+
collectCompletionResults(CompletionCtx, Lookup, DC,
425+
*Lookup.getExpectedTypeContext(),
426+
Lookup.canCurrDeclContextHandleAsync());
406427
}

lib/IDE/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ add_swift_host_library(swiftIDE STATIC
55
ArgumentCompletion.cpp
66
CodeCompletion.cpp
77
CodeCompletionCache.cpp
8-
CodeCompletionConsumer.cpp
98
CodeCompletionContext.cpp
109
CodeCompletionDiagnostics.cpp
1110
CodeCompletionResult.cpp

0 commit comments

Comments
 (0)