Skip to content

Commit fa476b0

Browse files
committed
[completion] Make NotRecommendedReason an enum class
1 parent be7b5a7 commit fa476b0

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

Diff for: include/swift/IDE/CodeCompletion.h

+16-10
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ class CodeCompletionResult {
599599
Identical,
600600
};
601601

602-
enum NotRecommendedReason {
602+
enum class NotRecommendedReason {
603603
None = 0,
604604
RedundantImport,
605605
Deprecated,
@@ -644,7 +644,8 @@ class CodeCompletionResult {
644644
CodeCompletionOperatorKind::None,
645645
StringRef BriefDocComment = StringRef())
646646
: Kind(Kind), KnownOperatorKind(unsigned(KnownOperatorKind)),
647-
SemanticContext(unsigned(SemanticContext)), NotRecommended(None),
647+
SemanticContext(unsigned(SemanticContext)),
648+
NotRecommended(unsigned(NotRecommendedReason::None)),
648649
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
649650
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
650651
assert(Kind != Declaration && "use the other constructor");
@@ -668,7 +669,8 @@ class CodeCompletionResult {
668669
ExpectedTypeRelation TypeDistance,
669670
StringRef BriefDocComment = StringRef())
670671
: Kind(Keyword), KnownOperatorKind(0),
671-
SemanticContext(unsigned(SemanticContext)), NotRecommended(None),
672+
SemanticContext(unsigned(SemanticContext)),
673+
NotRecommended(unsigned(NotRecommendedReason::None)),
672674
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
673675
BriefDocComment(BriefDocComment), TypeDistance(TypeDistance) {
674676
assert(CompletionString);
@@ -685,7 +687,8 @@ class CodeCompletionResult {
685687
CodeCompletionString *CompletionString,
686688
ExpectedTypeRelation TypeDistance)
687689
: Kind(Literal), KnownOperatorKind(0),
688-
SemanticContext(unsigned(SemanticContext)), NotRecommended(None),
690+
SemanticContext(unsigned(SemanticContext)),
691+
NotRecommended(unsigned(NotRecommendedReason::None)),
689692
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
690693
TypeDistance(TypeDistance) {
691694
AssociatedKind = static_cast<unsigned>(LiteralKind);
@@ -709,10 +712,11 @@ class CodeCompletionResult {
709712
enum ExpectedTypeRelation TypeDistance)
710713
: Kind(ResultKind::Declaration), KnownOperatorKind(0),
711714
SemanticContext(unsigned(SemanticContext)),
712-
NotRecommended(NotRecReason), NumBytesToErase(NumBytesToErase),
713-
CompletionString(CompletionString), ModuleName(ModuleName),
714-
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
715-
DocWords(DocWords), TypeDistance(TypeDistance) {
715+
NotRecommended(unsigned(NotRecReason)),
716+
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
717+
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
718+
AssociatedUSRs(AssociatedUSRs), DocWords(DocWords),
719+
TypeDistance(TypeDistance) {
716720
assert(AssociatedDecl && "should have a decl");
717721
AssociatedKind = unsigned(getCodeCompletionDeclKind(AssociatedDecl));
718722
IsSystem = getDeclIsSystem(AssociatedDecl);
@@ -739,7 +743,7 @@ class CodeCompletionResult {
739743
: Kind(ResultKind::Declaration),
740744
KnownOperatorKind(unsigned(KnownOperatorKind)),
741745
SemanticContext(unsigned(SemanticContext)),
742-
NotRecommended(NotRecReason), IsSystem(IsSystem),
746+
NotRecommended(unsigned(NotRecReason)), IsSystem(IsSystem),
743747
NumBytesToErase(NumBytesToErase), CompletionString(CompletionString),
744748
ModuleName(ModuleName), BriefDocComment(BriefDocComment),
745749
AssociatedUSRs(AssociatedUSRs), DocWords(DocWords),
@@ -801,7 +805,9 @@ class CodeCompletionResult {
801805
return static_cast<SemanticContextKind>(SemanticContext);
802806
}
803807

804-
bool isNotRecommended() const { return NotRecommended != None; }
808+
bool isNotRecommended() const {
809+
return getNotRecommendedReason() != NotRecommendedReason::None;
810+
}
805811

806812
unsigned getNumBytesToErase() const {
807813
return NumBytesToErase;

Diff for: lib/IDE/CodeCompletion.cpp

+20-25
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 {
@@ -2089,8 +2090,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
20892090
Builder.addBaseName(MD->getNameStr());
20902091
Builder.addTypeAnnotation("Module");
20912092
if (Pair.second)
2092-
Builder.setNotRecommended(
2093-
CodeCompletionResult::NotRecommendedReason::RedundantImport);
2093+
Builder.setNotRecommended(NotRecommendedReason::RedundantImport);
20942094
}
20952095
}
20962096

@@ -2114,9 +2114,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
21142114
}
21152115
}
21162116

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

21212119
// Don't add underscored cross-import overlay modules.
21222120
if (MD->getDeclaringModuleIfCrossImportOverlay())
@@ -2150,11 +2148,11 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
21502148
continue;
21512149

21522150
auto MD = ModuleDecl::create(ModuleName, Ctx);
2153-
Optional<CodeCompletionResult::NotRecommendedReason> Reason = None;
2151+
Optional<NotRecommendedReason> Reason = None;
21542152

21552153
// Imported modules are not recommended.
21562154
if (ImportedModules.count(MD->getNameStr()) != 0)
2157-
Reason = CodeCompletionResult::NotRecommendedReason::RedundantImport;
2155+
Reason = NotRecommendedReason::RedundantImport;
21582156

21592157
addModuleName(MD, Reason);
21602158
}
@@ -2517,9 +2515,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25172515
return Type();
25182516
}
25192517

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

25252522
switch (isolation.getKind()) {
@@ -2543,19 +2540,19 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25432540
if (implicitlyAsync && T) {
25442541
if (isa<VarDecl>(VD)) {
25452542
if (!isSendableType(CurrDeclContext, T)) {
2546-
NotRecommended = CodeCompletionResult::CrossActorReference;
2543+
NotRecommended = NotRecommendedReason::CrossActorReference;
25472544
}
25482545
} else {
25492546
assert(isa<FuncDecl>(VD) || isa<SubscriptDecl>(VD));
25502547
// Check if the result and the param types are all 'Sendable'.
25512548
auto *AFT = T->castTo<AnyFunctionType>();
25522549
if (!isSendableType(CurrDeclContext, AFT->getResult())) {
2553-
NotRecommended = CodeCompletionResult::CrossActorReference;
2550+
NotRecommended = NotRecommendedReason::CrossActorReference;
25542551
} else {
25552552
for (auto &param : AFT->getParams()) {
25562553
Type paramType = param.getPlainType();
25572554
if (!isSendableType(CurrDeclContext, paramType)) {
2558-
NotRecommended = CodeCompletionResult::CrossActorReference;
2555+
NotRecommended = NotRecommendedReason::CrossActorReference;
25592556
break;
25602557
}
25612558
}
@@ -2576,18 +2573,18 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
25762573
if (VD->hasInterfaceType())
25772574
VarType = getTypeOfMember(VD, dynamicLookupInfo);
25782575

2579-
Optional<CodeCompletionResult::NotRecommendedReason> NotRecommended;
2576+
Optional<NotRecommendedReason> NotRecommended;
25802577
// "not recommended" in its own getter.
25812578
if (Kind == LookupKind::ValueInDeclContext) {
25822579
if (auto accessor = dyn_cast<AccessorDecl>(CurrDeclContext)) {
25832580
if (accessor->getStorage() == VD && accessor->isGetter())
2584-
NotRecommended = CodeCompletionResult::VariableUsedInOwnDefinition;
2581+
NotRecommended = NotRecommendedReason::VariableUsedInOwnDefinition;
25852582
}
25862583
}
25872584
bool implicitlyAsync = false;
25882585
analyzeActorIsolation(VD, VarType, implicitlyAsync, NotRecommended);
25892586
if (!NotRecommended && implicitlyAsync && !CanCurrDeclContextHandleAsync) {
2590-
NotRecommended = CodeCompletionResult::InvalidAsyncContext;
2587+
NotRecommended = NotRecommendedReason::InvalidAsyncContext;
25912588
}
25922589

25932590
CommandWordsPairs Pairs;
@@ -2928,8 +2925,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
29282925
addTypeAnnotation(Builder, AFT->getResult(), genericSig);
29292926

29302927
if (AFT->isAsync() && !CanCurrDeclContextHandleAsync) {
2931-
Builder.setNotRecommended(
2932-
CodeCompletionResult::NotRecommendedReason::InvalidAsyncContext);
2928+
Builder.setNotRecommended(NotRecommendedReason::InvalidAsyncContext);
29332929
}
29342930
};
29352931

@@ -3038,14 +3034,14 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
30383034
if (AFT && !IsImplicitlyCurriedInstanceMethod)
30393035
trivialTrailingClosure = hasTrivialTrailingClosure(FD, AFT);
30403036

3041-
Optional<CodeCompletionResult::NotRecommendedReason> NotRecommended;
3037+
Optional<NotRecommendedReason> NotRecommended;
30423038
bool implictlyAsync = false;
30433039
analyzeActorIsolation(FD, AFT, implictlyAsync, NotRecommended);
30443040

30453041
if (!NotRecommended && !IsImplicitlyCurriedInstanceMethod &&
30463042
((AFT && AFT->isAsync()) || implictlyAsync) &&
30473043
!CanCurrDeclContextHandleAsync) {
3048-
NotRecommended = CodeCompletionResult::InvalidAsyncContext;
3044+
NotRecommended = NotRecommendedReason::InvalidAsyncContext;
30493045
}
30503046

30513047
// Add the method, possibly including any default arguments.
@@ -3243,8 +3239,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
32433239
}
32443240

32453241
if (ConstructorType->isAsync() && !CanCurrDeclContextHandleAsync) {
3246-
Builder.setNotRecommended(
3247-
CodeCompletionResult::NotRecommendedReason::InvalidAsyncContext);
3242+
Builder.setNotRecommended(NotRecommendedReason::InvalidAsyncContext);
32483243
}
32493244
};
32503245

@@ -3290,12 +3285,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
32903285
if (!subscriptType)
32913286
return;
32923287

3293-
Optional<CodeCompletionResult::NotRecommendedReason> NotRecommended;
3288+
Optional<NotRecommendedReason> NotRecommended;
32943289
bool implictlyAsync = false;
32953290
analyzeActorIsolation(SD, subscriptType, implictlyAsync, NotRecommended);
32963291

32973292
if (!NotRecommended && implictlyAsync && !CanCurrDeclContextHandleAsync) {
3298-
NotRecommended = CodeCompletionResult::InvalidAsyncContext;
3293+
NotRecommended = NotRecommendedReason::InvalidAsyncContext;
32993294
}
33003295

33013296
CommandWordsPairs Pairs;

0 commit comments

Comments
 (0)