@@ -251,16 +251,17 @@ enum class CodeCompletionDiagnosticSeverity : uint8_t {
251251// / E.g. \c InvalidAsyncContext depends on whether the usage context is async or
252252// / not.
253253enum class NotRecommendedReason : uint8_t {
254- None = 0 , // both contextual and context-free
255- RedundantImport, // contextual
256- RedundantImportIndirect, // contextual
257- Deprecated, // context-free
258- SoftDeprecated, // context-free
259- InvalidAsyncContext, // contextual
260- CrossActorReference, // contextual
261- VariableUsedInOwnDefinition, // contextual
262-
263- MAX_VALUE = VariableUsedInOwnDefinition
254+ None = 0 , // both contextual and context-free
255+ RedundantImport, // contextual
256+ RedundantImportIndirect, // contextual
257+ Deprecated, // context-free
258+ SoftDeprecated, // context-free
259+ InvalidAsyncContext, // contextual
260+ CrossActorReference, // contextual
261+ VariableUsedInOwnDefinition, // contextual
262+ NonAsyncAlternativeUsedInAsyncContext, // contextual
263+
264+ MAX_VALUE = NonAsyncAlternativeUsedInAsyncContext
264265};
265266
266267// / TODO: We consider deprecation warnings as context free although they don't
@@ -294,10 +295,14 @@ enum class ContextualNotRecommendedReason : uint8_t {
294295 None = 0 ,
295296 RedundantImport,
296297 RedundantImportIndirect,
298+ // / A method that is async is being used in a non-async context.
297299 InvalidAsyncContext,
298300 CrossActorReference,
299301 VariableUsedInOwnDefinition,
300- MAX_VALUE = VariableUsedInOwnDefinition
302+ // / A method that is sync and has an async alternative is used in an async
303+ // / context.
304+ NonAsyncAlternativeUsedInAsyncContext,
305+ MAX_VALUE = NonAsyncAlternativeUsedInAsyncContext
301306};
302307
303308enum class CodeCompletionResultKind : uint8_t {
@@ -330,6 +335,9 @@ class ContextFreeCodeCompletionResult {
330335
331336 bool IsSystem : 1 ;
332337 bool IsAsync : 1 ;
338+ // / Whether the result has been annotated as having an async alternative that
339+ // / should be prefered in async contexts.
340+ bool HasAsyncAlternative : 1 ;
333341 CodeCompletionString *CompletionString;
334342 NullTerminatedStringRef ModuleName;
335343 NullTerminatedStringRef BriefDocComment;
@@ -361,7 +369,7 @@ class ContextFreeCodeCompletionResult {
361369 ContextFreeCodeCompletionResult (
362370 CodeCompletionResultKind Kind, uint8_t AssociatedKind,
363371 CodeCompletionOperatorKind KnownOperatorKind, bool IsSystem, bool IsAsync,
364- CodeCompletionString *CompletionString,
372+ bool HasAsyncAlternative, CodeCompletionString *CompletionString,
365373 NullTerminatedStringRef ModuleName,
366374 NullTerminatedStringRef BriefDocComment,
367375 ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
@@ -372,10 +380,11 @@ class ContextFreeCodeCompletionResult {
372380 NullTerminatedStringRef FilterName,
373381 NullTerminatedStringRef NameForDiagnostics)
374382 : Kind(Kind), KnownOperatorKind(KnownOperatorKind), IsSystem(IsSystem),
375- IsAsync (IsAsync), CompletionString(CompletionString),
376- ModuleName(ModuleName), BriefDocComment(BriefDocComment),
377- AssociatedUSRs(AssociatedUSRs), ResultType(ResultType),
378- NotRecommended(NotRecommended), DiagnosticSeverity(DiagnosticSeverity),
383+ IsAsync (IsAsync), HasAsyncAlternative(HasAsyncAlternative),
384+ CompletionString(CompletionString), ModuleName(ModuleName),
385+ BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
386+ ResultType(ResultType), NotRecommended(NotRecommended),
387+ DiagnosticSeverity(DiagnosticSeverity),
379388 DiagnosticMessage(DiagnosticMessage), FilterName(FilterName),
380389 NameForDiagnostics(NameForDiagnostics) {
381390 this ->AssociatedKind .Opaque = AssociatedKind;
@@ -387,6 +396,8 @@ class ContextFreeCodeCompletionResult {
387396 " Completion item should have diagnostic message iff the diagnostics "
388397 " severity is not none" );
389398 assert (CompletionString && " Result should have a completion string" );
399+ assert (!(HasAsyncAlternative && IsAsync) &&
400+ " A function shouldn't be both async and have an async alternative" );
390401 if (isOperator () && KnownOperatorKind == CodeCompletionOperatorKind::None) {
391402 this ->KnownOperatorKind = getCodeCompletionOperatorKind (CompletionString);
392403 }
@@ -442,7 +453,7 @@ class ContextFreeCodeCompletionResult {
442453 createDeclResult (CodeCompletionResultSink &Sink,
443454 CodeCompletionString *CompletionString,
444455 const Decl *AssociatedDecl, bool IsAsync,
445- NullTerminatedStringRef ModuleName,
456+ bool HasAsyncAlternative, NullTerminatedStringRef ModuleName,
446457 NullTerminatedStringRef BriefDocComment,
447458 ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
448459 CodeCompletionResultType ResultType,
@@ -480,6 +491,8 @@ class ContextFreeCodeCompletionResult {
480491
481492 bool isAsync () const { return IsAsync; };
482493
494+ bool hasAsyncAlternative () const { return HasAsyncAlternative; };
495+
483496 CodeCompletionString *getCompletionString () const { return CompletionString; }
484497
485498 NullTerminatedStringRef getModuleName () const { return ModuleName; }
@@ -671,6 +684,8 @@ class CodeCompletionResult {
671684 return NotRecommendedReason::RedundantImportIndirect;
672685 case ContextualNotRecommendedReason::InvalidAsyncContext:
673686 return NotRecommendedReason::InvalidAsyncContext;
687+ case ContextualNotRecommendedReason::NonAsyncAlternativeUsedInAsyncContext:
688+ return NotRecommendedReason::NonAsyncAlternativeUsedInAsyncContext;
674689 case ContextualNotRecommendedReason::CrossActorReference:
675690 return NotRecommendedReason::CrossActorReference;
676691 case ContextualNotRecommendedReason::VariableUsedInOwnDefinition:
0 commit comments