Skip to content

[Completion] Add convertible type relation for attached macros #65515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions include/swift/IDE/CodeCompletionResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@ class ContextFreeCodeCompletionResult {
return NotRecommended;
}

ContextualNotRecommendedReason calculateContextualNotRecommendedReason(
ContextualNotRecommendedReason explicitReason,
bool canCurrDeclContextHandleAsync) const;

CodeCompletionResultTypeRelation calculateContextualTypeRelation(
const DeclContext *dc, const ExpectedTypeContext *typeContext,
const USRBasedTypeContext *usrTypeContext) const;

CodeCompletionDiagnosticSeverity getDiagnosticSeverity() const {
return DiagnosticSeverity;
}
Expand Down Expand Up @@ -611,6 +619,7 @@ class CodeCompletionResult {
CodeCompletionResultTypeRelation TypeDistance : 3;
static_assert(int(CodeCompletionResultTypeRelation::MAX_VALUE) < 1 << 3, "");

public:
/// Memberwise initializer
/// The \c ContextFree result must outlive this result. Typically, this is
/// done by allocating the two in the same sink or adopting the context free
Expand All @@ -624,25 +633,6 @@ class CodeCompletionResult {
Flair(Flair.toRaw()), NotRecommended(NotRecommended),
NumBytesToErase(NumBytesToErase), TypeDistance(TypeDistance) {}

public:
/// Enrich a \c ContextFreeCodeCompletionResult with the following contextual
/// information.
/// This computes the type relation between the completion item and its
/// expected type context.
/// See \c CodeCompletionResultType::calculateTypeRelation for documentation
/// on \p USRTypeContext.
/// The \c ContextFree result must outlive this result. Typically, this is
/// done by allocating the two in the same sink or adopting the context free
/// sink in the sink that allocates this result.
CodeCompletionResult(const ContextFreeCodeCompletionResult &ContextFree,
SemanticContextKind SemanticContext,
CodeCompletionFlair Flair, uint8_t NumBytesToErase,
const ExpectedTypeContext *TypeContext,
const DeclContext *DC,
const USRBasedTypeContext *USRTypeContext,
bool CanCurrDeclContextHandleAsync,
ContextualNotRecommendedReason NotRecommended);

const ContextFreeCodeCompletionResult &getContextFreeResult() const {
return ContextFree;
}
Expand Down
12 changes: 10 additions & 2 deletions lib/IDE/CodeCompletionConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,19 @@ static MutableArrayRef<CodeCompletionResult *> copyCodeCompletionResults(
if (!shouldIncludeResult(contextFreeResult)) {
continue;
}

CodeCompletionResultTypeRelation typeRelation =
contextFreeResult->calculateContextualTypeRelation(DC, TypeContext,
&USRTypeContext);
ContextualNotRecommendedReason notRecommendedReason =
contextFreeResult->calculateContextualNotRecommendedReason(
ContextualNotRecommendedReason::None,
CanCurrDeclContextHandleAsync);

auto contextualResult = new (*targetSink.Allocator) CodeCompletionResult(
*contextFreeResult, SemanticContextKind::OtherModule,
CodeCompletionFlair(),
/*numBytesToErase=*/0, TypeContext, DC, &USRTypeContext,
CanCurrDeclContextHandleAsync, ContextualNotRecommendedReason::None);
/*numBytesToErase=*/0, typeRelation, notRecommendedReason);
targetSink.Results.push_back(contextualResult);
}

Expand Down
52 changes: 27 additions & 25 deletions lib/IDE/CodeCompletionResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,39 +392,41 @@ bool ContextFreeCodeCompletionResult::getDeclIsSystem(const Decl *D) {
return D->getModuleContext()->isNonUserModule();
}

// MARK: - CodeCompletionResult

static ContextualNotRecommendedReason
getNotRecommenedReason(const ContextFreeCodeCompletionResult &ContextFree,
bool CanCurrDeclContextHandleAsync,
ContextualNotRecommendedReason ExplicitReason) {
if (ExplicitReason != ContextualNotRecommendedReason::None) {
return ExplicitReason;
}
if (ContextFree.isAsync() && !CanCurrDeclContextHandleAsync) {
ContextualNotRecommendedReason
ContextFreeCodeCompletionResult::calculateContextualNotRecommendedReason(
ContextualNotRecommendedReason explicitReason,
bool canCurrDeclContextHandleAsync) const {
if (explicitReason != ContextualNotRecommendedReason::None) {
return explicitReason;
}
if (IsAsync && !canCurrDeclContextHandleAsync) {
return ContextualNotRecommendedReason::InvalidAsyncContext;
}
if (ContextFree.hasAsyncAlternative() && CanCurrDeclContextHandleAsync) {
if (HasAsyncAlternative && canCurrDeclContextHandleAsync) {
return ContextualNotRecommendedReason::
NonAsyncAlternativeUsedInAsyncContext;
}
return ContextualNotRecommendedReason::None;
}

CodeCompletionResult::CodeCompletionResult(
const ContextFreeCodeCompletionResult &ContextFree,
SemanticContextKind SemanticContext, CodeCompletionFlair Flair,
uint8_t NumBytesToErase, const ExpectedTypeContext *TypeContext,
const DeclContext *DC, const USRBasedTypeContext *USRTypeContext,
bool CanCurrDeclContextHandleAsync,
ContextualNotRecommendedReason NotRecommended)
: ContextFree(ContextFree), SemanticContext(SemanticContext),
Flair(Flair.toRaw()),
NotRecommended(getNotRecommenedReason(
ContextFree, CanCurrDeclContextHandleAsync, NotRecommended)),
NumBytesToErase(NumBytesToErase),
TypeDistance(ContextFree.getResultType().calculateTypeRelation(
TypeContext, DC, USRTypeContext)) {}
CodeCompletionResultTypeRelation
ContextFreeCodeCompletionResult::calculateContextualTypeRelation(
const DeclContext *dc, const ExpectedTypeContext *typeContext,
const USRBasedTypeContext *usrTypeContext) const {
CodeCompletionResultTypeRelation typeRelation =
getResultType().calculateTypeRelation(typeContext, dc, usrTypeContext);
if (typeRelation >= CodeCompletionResultTypeRelation::Convertible ||
!typeContext)
return typeRelation;

CodeCompletionMacroRoles expectedRoles =
getCompletionMacroRoles(typeContext->getExpectedCustomAttributeKinds());
if (MacroRoles & expectedRoles)
return CodeCompletionResultTypeRelation::Convertible;
return typeRelation;
}

// MARK: - CodeCompletionResult

CodeCompletionResult *
CodeCompletionResult::withFlair(CodeCompletionFlair NewFlair,
Expand Down
15 changes: 10 additions & 5 deletions lib/IDE/CodeCompletionResultBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,27 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
// we don't need to compute any contextual properties.
return new (Allocator) CodeCompletionResult(
*ContextFreeResult, SemanticContextKind::None, CodeCompletionFlair(),
/*NumBytesToErase=*/0, /*TypeContext=*/nullptr,
/*DC=*/nullptr, /*USRTypeContext=*/nullptr,
/*CanCurrDeclContextHandleAsync=*/false,
/*NumBytesToErase=*/0, CodeCompletionResultTypeRelation::Unrelated,
ContextualNotRecommendedReason::None);
} else {
assert(
ContextFreeResult != nullptr &&
"ContextFreeResult should have been constructed by the switch above");

// We know that the ContextFreeResult has an AST-based type because it was
// just computed and not read from the cache and
// Sink.shouldProduceContextFreeResults() is false. So we can pass nullptr
// for USRTypeContext.
CodeCompletionResultTypeRelation typeRelation =
ContextFreeResult->calculateContextualTypeRelation(
DC, TypeContext, /*usrTypeContext=*/nullptr);
ContextualNotRecommendedReason notRecommendedReason =
ContextFreeResult->calculateContextualNotRecommendedReason(
ContextualNotRecReason, CanCurrDeclContextHandleAsync);

return new (Allocator) CodeCompletionResult(
*ContextFreeResult, SemanticContext, Flair, NumBytesToErase,
TypeContext, DC, /*USRTypeContext=*/nullptr,
CanCurrDeclContextHandleAsync, ContextualNotRecReason);
typeRelation, notRecommendedReason);
}
}

Expand Down
36 changes: 18 additions & 18 deletions test/IDE/complete_macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ import MacroDefinitions
@#^STRUCT_ATTR?check=NOMINAL_ATTR^# struct S{}
// NOMINAL_ATTR-NOT: freestanding
// NOMINAL_ATTR-NOT: AttachedAccessorMacro
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedMemberMacro[#Void#]; name=AttachedMemberMacro
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedMemberMacroWithArgs({#arg1: Int#})[#Void#]; name=AttachedMemberMacroWithArgs
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedMemberAttributeMacro[#Void#]; name=AttachedMemberAttributeMacro
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedPeerMacro[#Void#]; name=AttachedPeerMacro
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedConformanceMacro[#Void#]; name=AttachedConformanceMacro
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}: EverythingMacro[#Void#]; name=EverythingMacro
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedMemberMacro[#Void#]; name=AttachedMemberMacro
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO that’s why we shouldn’t wildcard match anything after Decl[Macro] but always be explicit about relations etc. You don’t need to change it in this PR though.

Copy link
Contributor Author

@bnbarham bnbarham Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue here is that module is before type relation and this test does both in-module and cross-module completions. IMO we should move type relation before the location of the completion. I could of course regex match the current module/other module but... it's really not all that important to this test.

// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedMemberMacroWithArgs({#arg1: Int#})[#Void#]; name=AttachedMemberMacroWithArgs
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedMemberAttributeMacro[#Void#]; name=AttachedMemberAttributeMacro
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedPeerMacro[#Void#]; name=AttachedPeerMacro
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedConformanceMacro[#Void#]; name=AttachedConformanceMacro
// NOMINAL_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: EverythingMacro[#Void#]; name=EverythingMacro

@#^FUNC_ATTR?check=DECL_ATTR^# func method() {}
struct MethodAttrs {
Expand All @@ -83,8 +83,8 @@ struct MethodAttrs {
// DECL_ATTR-NOT: AttachedMemberMacro
// DECL_ATTR-NOT: AttachedMemberMacroWithArgs
// DECL_ATTR-NOT: AttachedConformanceMacro
// DECL_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedPeerMacro[#Void#]; name=AttachedPeerMacro
// DECL_ATTR-DAG: Decl[Macro]/{{.*}}: EverythingMacro[#Void#]; name=EverythingMacro
// DECL_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedPeerMacro[#Void#]; name=AttachedPeerMacro
// DECL_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: EverythingMacro[#Void#]; name=EverythingMacro

@#^GLOBAL_ATTR?check=VAR_ATTR^# var globalVar
struct PropAttr {
Expand All @@ -98,9 +98,9 @@ struct PropAttr {
// VAR_ATTR-NOT: AttachedMemberMacroWithArgs
// VAR_ATTR-NOT: AttachedMemberAttributeMacro
// VAR_ATTR-NOT: AttachedConformanceMacro
// VAR_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedAccessorMacro[#Void#]; name=AttachedAccessorMacro
// VAR_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedPeerMacro[#Void#]; name=AttachedPeerMacro
// VAR_ATTR-DAG: Decl[Macro]/{{.*}}: EverythingMacro[#Void#]; name=EverythingMacro
// VAR_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedAccessorMacro[#Void#]; name=AttachedAccessorMacro
// VAR_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedPeerMacro[#Void#]; name=AttachedPeerMacro
// VAR_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: EverythingMacro[#Void#]; name=EverythingMacro

func paramAttr(@#^PARAM_ATTR?check=PARAM_ATTR^#) {}
func paramAttr2(@#^PARAM2_ATTR?check=PARAM_ATTR^# arg: Int) {}
Expand Down Expand Up @@ -163,10 +163,10 @@ struct LastMember {
@#^INDEPENDENT?check=INDEPENDENT_ATTR^#
// INDEPENDENT_ATTR-NOT: freestandingExprMacro
// INDEPENDENT_ATTR-NOT: freestandingDeclMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedAccessorMacro[#Void#]; name=AttachedAccessorMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedMemberMacro[#Void#]; name=AttachedMemberMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedMemberMacroWithArgs({#arg1: Int#})[#Void#]; name=AttachedMemberMacroWithArgs
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedMemberAttributeMacro[#Void#]; name=AttachedMemberAttributeMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedPeerMacro[#Void#]; name=AttachedPeerMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}: AttachedConformanceMacro[#Void#]; name=AttachedConformanceMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}: EverythingMacro[#Void#]; name=EverythingMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedAccessorMacro[#Void#]; name=AttachedAccessorMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedMemberMacro[#Void#]; name=AttachedMemberMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedMemberMacroWithArgs({#arg1: Int#})[#Void#]; name=AttachedMemberMacroWithArgs
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedMemberAttributeMacro[#Void#]; name=AttachedMemberAttributeMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedPeerMacro[#Void#]; name=AttachedPeerMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: AttachedConformanceMacro[#Void#]; name=AttachedConformanceMacro
// INDEPENDENT_ATTR-DAG: Decl[Macro]/{{.*}}/TypeRelation[Convertible]: EverythingMacro[#Void#]; name=EverythingMacro
3 changes: 1 addition & 2 deletions tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ bool SourceKit::CodeCompletion::addCustomCompletions(
auto *swiftResult = new (sink.allocator) CodeCompletion::SwiftResult(
*contextFreeResult, SemanticContextKind::Local,
CodeCompletionFlairBit::ExpressionSpecific,
/*NumBytesToErase=*/0, /*TypeContext=*/nullptr, /*DC=*/nullptr,
/*USRTypeContext=*/nullptr, /*CanCurrDeclContextHandleAsync=*/false,
/*NumBytesToErase=*/0, CodeCompletionResultTypeRelation::Unrelated,
ContextualNotRecommendedReason::None);

CompletionBuilder builder(sink, *swiftResult);
Expand Down
3 changes: 1 addition & 2 deletions tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,7 @@ static void transformAndForwardResults(
*contextFreeResult, SemanticContextKind::CurrentNominal,
CodeCompletionFlairBit::ExpressionSpecific,
exactMatch ? exactMatch->getNumBytesToErase() : 0,
/*TypeContext=*/nullptr, /*DC=*/nullptr, /*USRTypeContext=*/nullptr,
/*CanCurrDeclContextHandleAsync=*/false,
CodeCompletionResultTypeRelation::Unrelated,
ContextualNotRecommendedReason::None);

SwiftCompletionInfo info;
Expand Down
3 changes: 1 addition & 2 deletions tools/swift-ide-test/swift-ide-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4310,8 +4310,7 @@ int main(int argc, char *argv[]) {
auto contextualResult = new CodeCompletionResult(
*contextFreeResult, SemanticContextKind::OtherModule,
CodeCompletionFlair(),
/*numBytesToErase=*/0, /*TypeContext=*/nullptr, /*DC=*/nullptr,
/*USRTypeContext=*/nullptr, /*CanCurrDeclContextHandleAsync=*/false,
/*numBytesToErase=*/0, CodeCompletionResultTypeRelation::Unrelated,
ContextualNotRecommendedReason::None);
contextualResults.push_back(contextualResult);
}
Expand Down