Skip to content

Commit 89039a8

Browse files
authored
Merge pull request #36961 from benlangmuir/not-recommended-improvement
[completion] Clarify and simplify not-recommended state
2 parents 8b9d7ae + fa476b0 commit 89039a8

File tree

5 files changed

+54
-63
lines changed

5 files changed

+54
-63
lines changed

include/swift/IDE/CodeCompletion.h

+22-24
Original file line numberDiff line numberDiff line change
@@ -599,21 +599,21 @@ class CodeCompletionResult {
599599
Identical,
600600
};
601601

602-
enum NotRecommendedReason {
603-
Redundant,
602+
enum class NotRecommendedReason {
603+
None = 0,
604+
RedundantImport,
604605
Deprecated,
605-
InvalidContext,
606+
InvalidAsyncContext,
606607
CrossActorReference,
607-
NoReason,
608+
VariableUsedInOwnDefinition,
608609
};
609610

610611
private:
611612
unsigned Kind : 3;
612613
unsigned AssociatedKind : 8;
613614
unsigned KnownOperatorKind : 6;
614615
unsigned SemanticContext : 3;
615-
unsigned NotRecommended : 1;
616-
unsigned NotRecReason : 3;
616+
unsigned NotRecommended : 4;
617617
unsigned IsSystem : 1;
618618

619619
/// The number of bytes to the left of the code completion point that
@@ -644,11 +644,10 @@ class CodeCompletionResult {
644644
CodeCompletionOperatorKind::None,
645645
StringRef BriefDocComment = StringRef())
646646
: Kind(Kind), KnownOperatorKind(unsigned(KnownOperatorKind)),
647-
SemanticContext(unsigned(SemanticContext)), NotRecommended(false),
648-
NotRecReason(NotRecommendedReason::NoReason),
647+
SemanticContext(unsigned(SemanticContext)),
648+
NotRecommended(unsigned(NotRecommendedReason::None)),
649649
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
650-
BriefDocComment(BriefDocComment),
651-
TypeDistance(TypeDistance) {
650+
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
652651
assert(Kind != Declaration && "use the other constructor");
653652
assert(CompletionString);
654653
if (isOperator() && KnownOperatorKind == CodeCompletionOperatorKind::None)
@@ -670,8 +669,8 @@ class CodeCompletionResult {
670669
ExpectedTypeRelation TypeDistance,
671670
StringRef BriefDocComment = StringRef())
672671
: Kind(Keyword), KnownOperatorKind(0),
673-
SemanticContext(unsigned(SemanticContext)), NotRecommended(false),
674-
NotRecReason(NotRecommendedReason::NoReason),
672+
SemanticContext(unsigned(SemanticContext)),
673+
NotRecommended(unsigned(NotRecommendedReason::None)),
675674
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
676675
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
677676
assert(CompletionString);
@@ -688,8 +687,8 @@ class CodeCompletionResult {
688687
CodeCompletionString *CompletionString,
689688
ExpectedTypeRelation TypeDistance)
690689
: Kind(Literal), KnownOperatorKind(0),
691-
SemanticContext(unsigned(SemanticContext)), NotRecommended(false),
692-
NotRecReason(NotRecommendedReason::NoReason),
690+
SemanticContext(unsigned(SemanticContext)),
691+
NotRecommended(unsigned(NotRecommendedReason::None)),
693692
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
694693
TypeDistance(TypeDistance) {
695694
AssociatedKind = static_cast<unsigned>(LiteralKind);
@@ -706,15 +705,14 @@ class CodeCompletionResult {
706705
unsigned NumBytesToErase,
707706
CodeCompletionString *CompletionString,
708707
const Decl *AssociatedDecl, StringRef ModuleName,
709-
bool NotRecommended,
710708
CodeCompletionResult::NotRecommendedReason NotRecReason,
711709
StringRef BriefDocComment,
712710
ArrayRef<StringRef> AssociatedUSRs,
713711
ArrayRef<std::pair<StringRef, StringRef>> DocWords,
714712
enum ExpectedTypeRelation TypeDistance)
715713
: Kind(ResultKind::Declaration), KnownOperatorKind(0),
716714
SemanticContext(unsigned(SemanticContext)),
717-
NotRecommended(NotRecommended), NotRecReason(NotRecReason),
715+
NotRecommended(unsigned(NotRecReason)),
718716
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
719717
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
720718
AssociatedUSRs(AssociatedUSRs), DocWords(DocWords),
@@ -735,7 +733,7 @@ class CodeCompletionResult {
735733
unsigned NumBytesToErase,
736734
CodeCompletionString *CompletionString,
737735
CodeCompletionDeclKind DeclKind, bool IsSystem,
738-
StringRef ModuleName, bool NotRecommended,
736+
StringRef ModuleName,
739737
CodeCompletionResult::NotRecommendedReason NotRecReason,
740738
StringRef BriefDocComment,
741739
ArrayRef<StringRef> AssociatedUSRs,
@@ -745,11 +743,11 @@ class CodeCompletionResult {
745743
: Kind(ResultKind::Declaration),
746744
KnownOperatorKind(unsigned(KnownOperatorKind)),
747745
SemanticContext(unsigned(SemanticContext)),
748-
NotRecommended(NotRecommended), NotRecReason(NotRecReason),
749-
IsSystem(IsSystem), NumBytesToErase(NumBytesToErase),
750-
CompletionString(CompletionString), ModuleName(ModuleName),
751-
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
752-
DocWords(DocWords), TypeDistance(TypeDistance) {
746+
NotRecommended(unsigned(NotRecReason)), IsSystem(IsSystem),
747+
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
748+
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
749+
AssociatedUSRs(AssociatedUSRs), DocWords(DocWords),
750+
TypeDistance(TypeDistance) {
753751
AssociatedKind = static_cast<unsigned>(DeclKind);
754752
assert(CompletionString);
755753
assert(!isOperator() ||
@@ -800,15 +798,15 @@ class CodeCompletionResult {
800798
}
801799

802800
NotRecommendedReason getNotRecommendedReason() const {
803-
return static_cast<NotRecommendedReason>(NotRecReason);
801+
return static_cast<NotRecommendedReason>(NotRecommended);
804802
}
805803

806804
SemanticContextKind getSemanticContext() const {
807805
return static_cast<SemanticContextKind>(SemanticContext);
808806
}
809807

810808
bool isNotRecommended() const {
811-
return NotRecommended;
809+
return getNotRecommendedReason() != NotRecommendedReason::None;
812810
}
813811

814812
unsigned getNumBytesToErase() const {

lib/IDE/CodeCompletion.cpp

+21-27
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ using namespace swift;
5959
using namespace ide;
6060

6161
using CommandWordsPairs = std::vector<std::pair<StringRef, StringRef>>;
62+
using NotRecommendedReason = CodeCompletionResult::NotRecommendedReason;
6263

6364
enum CodeCompletionCommandKind {
6465
none,
@@ -819,7 +820,7 @@ void CodeCompletionResultBuilder::setAssociatedDecl(const Decl *D) {
819820
}
820821

821822
if (D->getAttrs().getDeprecated(D->getASTContext()))
822-
setNotRecommended(CodeCompletionResult::Deprecated);
823+
setNotRecommended(NotRecommendedReason::Deprecated);
823824
}
824825

825826
namespace {
@@ -1294,8 +1295,7 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
12941295

12951296
return new (*Sink.Allocator) CodeCompletionResult(
12961297
SemanticContext, NumBytesToErase, CCS, AssociatedDecl, ModuleName,
1297-
/*NotRecommended=*/IsNotRecommended, NotRecReason,
1298-
copyString(*Sink.Allocator, BriefComment),
1298+
NotRecReason, copyString(*Sink.Allocator, BriefComment),
12991299
copyAssociatedUSRs(*Sink.Allocator, AssociatedDecl),
13001300
copyArray(*Sink.Allocator, CommentWords), ExpectedTypeRelation);
13011301
}
@@ -2090,8 +2090,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
20902090
Builder.addBaseName(MD->getNameStr());
20912091
Builder.addTypeAnnotation("Module");
20922092
if (Pair.second)
2093-
Builder.setNotRecommended(CodeCompletionResult::NotRecommendedReason::
2094-
Redundant);
2093+
Builder.setNotRecommended(NotRecommendedReason::RedundantImport);
20952094
}
20962095
}
20972096

@@ -2115,9 +2114,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
21152114
}
21162115
}
21172116

2118-
void addModuleName(
2119-
ModuleDecl *MD,
2120-
Optional<CodeCompletionResult::NotRecommendedReason> R = None) {
2117+
void addModuleName(ModuleDecl *MD, Optional<NotRecommendedReason> R = None) {
21212118

21222119
// Don't add underscored cross-import overlay modules.
21232120
if (MD->getDeclaringModuleIfCrossImportOverlay())
@@ -2151,11 +2148,11 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
21512148
continue;
21522149

21532150
auto MD = ModuleDecl::create(ModuleName, Ctx);
2154-
Optional<CodeCompletionResult::NotRecommendedReason> Reason = None;
2151+
Optional<NotRecommendedReason> Reason = None;
21552152

21562153
// Imported modules are not recommended.
21572154
if (ImportedModules.count(MD->getNameStr()) != 0)
2158-
Reason = CodeCompletionResult::NotRecommendedReason::Redundant;
2155+
Reason = NotRecommendedReason::RedundantImport;
21592156

21602157
addModuleName(MD, Reason);
21612158
}
@@ -2518,9 +2515,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25182515
return Type();
25192516
}
25202517

2521-
void analyzeActorIsolation(
2522-
const ValueDecl *VD, Type T, bool &implicitlyAsync,
2523-
Optional<CodeCompletionResult::NotRecommendedReason> &NotRecommended) {
2518+
void analyzeActorIsolation(const ValueDecl *VD, Type T, bool &implicitlyAsync,
2519+
Optional<NotRecommendedReason> &NotRecommended) {
25242520
auto isolation = getActorIsolation(const_cast<ValueDecl *>(VD));
25252521

25262522
switch (isolation.getKind()) {
@@ -2544,19 +2540,19 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25442540
if (implicitlyAsync && T) {
25452541
if (isa<VarDecl>(VD)) {
25462542
if (!isSendableType(CurrDeclContext, T)) {
2547-
NotRecommended = CodeCompletionResult::CrossActorReference;
2543+
NotRecommended = NotRecommendedReason::CrossActorReference;
25482544
}
25492545
} else {
25502546
assert(isa<FuncDecl>(VD) || isa<SubscriptDecl>(VD));
25512547
// Check if the result and the param types are all 'Sendable'.
25522548
auto *AFT = T->castTo<AnyFunctionType>();
25532549
if (!isSendableType(CurrDeclContext, AFT->getResult())) {
2554-
NotRecommended = CodeCompletionResult::CrossActorReference;
2550+
NotRecommended = NotRecommendedReason::CrossActorReference;
25552551
} else {
25562552
for (auto &param : AFT->getParams()) {
25572553
Type paramType = param.getPlainType();
25582554
if (!isSendableType(CurrDeclContext, paramType)) {
2559-
NotRecommended = CodeCompletionResult::CrossActorReference;
2555+
NotRecommended = NotRecommendedReason::CrossActorReference;
25602556
break;
25612557
}
25622558
}
@@ -2577,18 +2573,18 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25772573
if (VD->hasInterfaceType())
25782574
VarType = getTypeOfMember(VD, dynamicLookupInfo);
25792575

2580-
Optional<CodeCompletionResult::NotRecommendedReason> NotRecommended;
2576+
Optional<NotRecommendedReason> NotRecommended;
25812577
// "not recommended" in its own getter.
25822578
if (Kind == LookupKind::ValueInDeclContext) {
25832579
if (auto accessor = dyn_cast<AccessorDecl>(CurrDeclContext)) {
25842580
if (accessor->getStorage() == VD && accessor->isGetter())
2585-
NotRecommended = CodeCompletionResult::NoReason;
2581+
NotRecommended = NotRecommendedReason::VariableUsedInOwnDefinition;
25862582
}
25872583
}
25882584
bool implicitlyAsync = false;
25892585
analyzeActorIsolation(VD, VarType, implicitlyAsync, NotRecommended);
25902586
if (!NotRecommended && implicitlyAsync && !CanCurrDeclContextHandleAsync) {
2591-
NotRecommended = CodeCompletionResult::InvalidContext;
2587+
NotRecommended = NotRecommendedReason::InvalidAsyncContext;
25922588
}
25932589

25942590
CommandWordsPairs Pairs;
@@ -2929,8 +2925,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
29292925
addTypeAnnotation(Builder, AFT->getResult(), genericSig);
29302926

29312927
if (AFT->isAsync() && !CanCurrDeclContextHandleAsync) {
2932-
Builder.setNotRecommended(
2933-
CodeCompletionResult::NotRecommendedReason::InvalidContext);
2928+
Builder.setNotRecommended(NotRecommendedReason::InvalidAsyncContext);
29342929
}
29352930
};
29362931

@@ -3039,14 +3034,14 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
30393034
if (AFT && !IsImplicitlyCurriedInstanceMethod)
30403035
trivialTrailingClosure = hasTrivialTrailingClosure(FD, AFT);
30413036

3042-
Optional<CodeCompletionResult::NotRecommendedReason> NotRecommended;
3037+
Optional<NotRecommendedReason> NotRecommended;
30433038
bool implictlyAsync = false;
30443039
analyzeActorIsolation(FD, AFT, implictlyAsync, NotRecommended);
30453040

30463041
if (!NotRecommended && !IsImplicitlyCurriedInstanceMethod &&
30473042
((AFT && AFT->isAsync()) || implictlyAsync) &&
30483043
!CanCurrDeclContextHandleAsync) {
3049-
NotRecommended = CodeCompletionResult::InvalidContext;
3044+
NotRecommended = NotRecommendedReason::InvalidAsyncContext;
30503045
}
30513046

30523047
// Add the method, possibly including any default arguments.
@@ -3244,8 +3239,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
32443239
}
32453240

32463241
if (ConstructorType->isAsync() && !CanCurrDeclContextHandleAsync) {
3247-
Builder.setNotRecommended(
3248-
CodeCompletionResult::NotRecommendedReason::InvalidContext);
3242+
Builder.setNotRecommended(NotRecommendedReason::InvalidAsyncContext);
32493243
}
32503244
};
32513245

@@ -3291,12 +3285,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
32913285
if (!subscriptType)
32923286
return;
32933287

3294-
Optional<CodeCompletionResult::NotRecommendedReason> NotRecommended;
3288+
Optional<NotRecommendedReason> NotRecommended;
32953289
bool implictlyAsync = false;
32963290
analyzeActorIsolation(SD, subscriptType, implictlyAsync, NotRecommended);
32973291

32983292
if (!NotRecommended && implictlyAsync && !CanCurrDeclContextHandleAsync) {
3299-
NotRecommended = CodeCompletionResult::InvalidContext;
3293+
NotRecommended = NotRecommendedReason::InvalidAsyncContext;
33003294
}
33013295

33023296
CommandWordsPairs Pairs;

lib/IDE/CodeCompletionCache.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
192192
auto declKind = static_cast<CodeCompletionDeclKind>(*cursor++);
193193
auto opKind = static_cast<CodeCompletionOperatorKind>(*cursor++);
194194
auto context = static_cast<SemanticContextKind>(*cursor++);
195-
auto notRecommended = static_cast<bool>(*cursor++);
195+
auto notRecommended =
196+
static_cast<CodeCompletionResult::NotRecommendedReason>(*cursor++);
196197
auto isSystem = static_cast<bool>(*cursor++);
197198
auto numBytesToErase = static_cast<unsigned>(*cursor++);
198199
auto oldCursor = cursor;
@@ -228,9 +229,10 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
228229
if (kind == CodeCompletionResult::Declaration) {
229230
result = new (*V.Sink.Allocator) CodeCompletionResult(
230231
context, numBytesToErase, string, declKind, isSystem, moduleName,
231-
notRecommended, CodeCompletionResult::NotRecommendedReason::NoReason,
232-
briefDocComment, copyArray(*V.Sink.Allocator, ArrayRef<StringRef>(assocUSRs)),
233-
copyArray(*V.Sink.Allocator, ArrayRef<std::pair<StringRef, StringRef>>(declKeywords)),
232+
notRecommended, briefDocComment,
233+
copyArray(*V.Sink.Allocator, ArrayRef<StringRef>(assocUSRs)),
234+
copyArray(*V.Sink.Allocator,
235+
ArrayRef<std::pair<StringRef, StringRef>>(declKeywords)),
234236
CodeCompletionResult::Unknown, opKind);
235237
} else {
236238
result = new (*V.Sink.Allocator)
@@ -349,7 +351,7 @@ static void writeCachedModule(llvm::raw_ostream &out,
349351
else
350352
LE.write(static_cast<uint8_t>(CodeCompletionOperatorKind::None));
351353
LE.write(static_cast<uint8_t>(R->getSemanticContext()));
352-
LE.write(static_cast<uint8_t>(R->isNotRecommended()));
354+
LE.write(static_cast<uint8_t>(R->getNotRecommendedReason()));
353355
LE.write(static_cast<uint8_t>(R->isSystem()));
354356
LE.write(static_cast<uint8_t>(R->getNumBytesToErase()));
355357
LE.write(

lib/IDE/CodeCompletionResultBuilder.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ class CodeCompletionResultBuilder {
8989
CodeCompletionResult::Unknown;
9090
bool Cancelled = false;
9191
ArrayRef<std::pair<StringRef, StringRef>> CommentWords;
92-
bool IsNotRecommended = false;
9392
CodeCompletionResult::NotRecommendedReason NotRecReason =
94-
CodeCompletionResult::NotRecommendedReason::NoReason;
93+
CodeCompletionResult::NotRecommendedReason::None;
9594
StringRef BriefDocComment = StringRef();
9695

9796
void addChunkWithText(CodeCompletionString::Chunk::ChunkKind Kind,
@@ -148,7 +147,6 @@ class CodeCompletionResultBuilder {
148147
void setLiteralKind(CodeCompletionLiteralKind kind) { LiteralKind = kind; }
149148
void setKeywordKind(CodeCompletionKeywordKind kind) { KeywordKind = kind; }
150149
void setNotRecommended(CodeCompletionResult::NotRecommendedReason Reason) {
151-
IsNotRecommended = true;
152150
NotRecReason = Reason;
153151
}
154152

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1169,10 +1169,9 @@ Completion *CompletionBuilder::finish() {
11691169
base = SwiftResult(
11701170
semanticContext, current.getNumBytesToErase(), completionString,
11711171
current.getAssociatedDeclKind(), current.isSystem(),
1172-
current.getModuleName(), current.isNotRecommended(),
1173-
current.getNotRecommendedReason(), current.getBriefDocComment(),
1174-
current.getAssociatedUSRs(), current.getDeclKeywords(),
1175-
typeRelation, opKind);
1172+
current.getModuleName(), current.getNotRecommendedReason(),
1173+
current.getBriefDocComment(), current.getAssociatedUSRs(),
1174+
current.getDeclKeywords(), typeRelation, opKind);
11761175
} else {
11771176
base = SwiftResult(current.getKind(), semanticContext,
11781177
current.getNumBytesToErase(), completionString,

0 commit comments

Comments
 (0)