-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[CodeCompletion] Use GenericSignature methods to get 'associatedtype' requirements #29086
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
rintaro
merged 3 commits into
swiftlang:master
from
rintaro:ide-completion-opaqueresult-rdar57245073
Jan 9, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e388c4a
[CodeCompletion] Get associatedtype constraints from ProtocolDecl::ge…
rintaro 1dd6fe5
[CodeCompletion] Stop suggesting opaque result type for generic function
rintaro b9f1e58
[CodeCompletion] Use GenericSignature methods to get associatedtype reqs
rintaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4159,8 +4159,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer { | |
|
||
/// Return type if the result type if \p VD should be represented as opaque | ||
/// result type. | ||
TypeLoc getOpaqueResultTypeLoc(const ValueDecl *VD, DeclVisibilityKind Reason, | ||
DynamicLookupInfo dynamicLookupInfo) { | ||
Type getOpaqueResultType(const ValueDecl *VD, DeclVisibilityKind Reason, | ||
DynamicLookupInfo dynamicLookupInfo) { | ||
if (Reason != | ||
DeclVisibilityKind::MemberOfProtocolImplementedByCurrentNominal) | ||
return nullptr; | ||
|
@@ -4170,50 +4170,76 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer { | |
return nullptr; | ||
|
||
Type ResultT; | ||
if (auto *FD = dyn_cast<FuncDecl>(VD)) | ||
if (auto *FD = dyn_cast<FuncDecl>(VD)) { | ||
if (FD->getGenericParams()) { | ||
// Generic function cannot have opaque result type. | ||
return nullptr; | ||
} | ||
ResultT = FD->getResultInterfaceType(); | ||
else if (auto *SD = dyn_cast<SubscriptDecl>(VD)) | ||
} else if (auto *SD = dyn_cast<SubscriptDecl>(VD)) { | ||
if (SD->getGenericParams()) { | ||
// Generic subscript cannot have opaque result type. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also works fine for me:
|
||
return nullptr; | ||
} | ||
ResultT = SD->getElementInterfaceType(); | ||
else if (auto *VarD = dyn_cast<VarDecl>(VD)) | ||
} else if (auto *VarD = dyn_cast<VarDecl>(VD)) { | ||
ResultT = VarD->getInterfaceType(); | ||
else | ||
return nullptr; | ||
|
||
if (!ResultT->is<DependentMemberType>()) | ||
// The result is not associatedtype. | ||
} else { | ||
return nullptr; | ||
} | ||
|
||
// If associatedtype doesn't have conformance/superclass constraint, we | ||
// can't use opaque type. | ||
auto assocTyD = ResultT->castTo<DependentMemberType>()->getAssocType(); | ||
if (!assocTyD->getInherited().size()) | ||
if (!ResultT->is<DependentMemberType>() || | ||
!ResultT->castTo<DependentMemberType>()->getAssocType()) | ||
// The result is not a valid associatedtype. | ||
return nullptr; | ||
|
||
// Try substitution to see if the associated type is resolved to concrete | ||
// type. | ||
auto substMap = currTy->getMemberSubstitutionMap( | ||
CurrDeclContext->getParentModule(), VD); | ||
ResultT = ResultT.subst(substMap); | ||
if (!ResultT || !ResultT->is<DependentMemberType>()) | ||
if (!ResultT.subst(substMap)->is<DependentMemberType>()) | ||
// If resolved print it. | ||
return nullptr; | ||
|
||
return assocTyD->getInherited()[0]; | ||
auto genericSig = VD->getDeclContext()->getGenericSignatureOfContext(); | ||
|
||
if (genericSig->isConcreteType(ResultT)) | ||
// If it has same type requrement, we will emit the concrete type. | ||
return nullptr; | ||
|
||
// Collect requirements on the associatedtype. | ||
SmallVector<Type, 2> opaqueTypes; | ||
bool hasExplicitAnyObject = false; | ||
if (auto superTy = genericSig->getSuperclassBound(ResultT)) | ||
opaqueTypes.push_back(superTy); | ||
for (auto proto : genericSig->getConformsTo(ResultT)) | ||
opaqueTypes.push_back(proto->getDeclaredInterfaceType()); | ||
if (auto layout = genericSig->getLayoutConstraint(ResultT)) | ||
hasExplicitAnyObject = layout->isClass(); | ||
|
||
if (!hasExplicitAnyObject) { | ||
if (opaqueTypes.empty()) | ||
return nullptr; | ||
if (opaqueTypes.size() == 1) | ||
return opaqueTypes.front(); | ||
} | ||
return ProtocolCompositionType::get( | ||
VD->getASTContext(), opaqueTypes, hasExplicitAnyObject); | ||
} | ||
|
||
void addValueOverride(const ValueDecl *VD, DeclVisibilityKind Reason, | ||
DynamicLookupInfo dynamicLookupInfo, | ||
CodeCompletionResultBuilder &Builder, | ||
bool hasDeclIntroducer) { | ||
class DeclPrinter : public StreamPrinter { | ||
TypeLoc OpaqueBaseTy; | ||
Type OpaqueBaseTy; | ||
|
||
public: | ||
using StreamPrinter::StreamPrinter; | ||
|
||
Optional<unsigned> NameOffset; | ||
|
||
DeclPrinter(raw_ostream &OS, TypeLoc OpaqueBaseTy) | ||
DeclPrinter(raw_ostream &OS, Type OpaqueBaseTy) | ||
: StreamPrinter(OS), OpaqueBaseTy(OpaqueBaseTy) {} | ||
|
||
void printDeclLoc(const Decl *D) override { | ||
|
@@ -4225,7 +4251,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer { | |
void printDeclResultTypePre(ValueDecl *VD, TypeLoc &TL) override { | ||
if (!OpaqueBaseTy.isNull()) { | ||
OS << "some "; | ||
TL = OpaqueBaseTy; | ||
TL = TypeLoc::withoutLoc(OpaqueBaseTy); | ||
} | ||
} | ||
}; | ||
|
@@ -4235,7 +4261,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer { | |
{ | ||
llvm::raw_svector_ostream OS(DeclStr); | ||
DeclPrinter Printer( | ||
OS, getOpaqueResultTypeLoc(VD, Reason, dynamicLookupInfo)); | ||
OS, getOpaqueResultType(VD, Reason, dynamicLookupInfo)); | ||
PrintOptions Options; | ||
if (auto transformType = CurrDeclContext->getDeclaredTypeInContext()) | ||
Options.setBaseType(transformType); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %{python} %utils/split_file.py -o %t %s | ||
|
||
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift | ||
// RUN: %target-swift-ide-test -code-completion -source-filename %t/Test.swift -I %t -code-completion-token=OPAQUE_RESULT | %FileCheck --check-prefix=OPAQUE_RESULT %s | ||
|
||
// BEGIN MyModule.swift | ||
|
||
public protocol HasAssocWithConstraint { | ||
associatedtype AssocWithContraint: HasAssocWithConstraint | ||
var value: AssocWithContraint { get } | ||
} | ||
|
||
// BEGIN Test.swift | ||
import MyModule | ||
|
||
struct MyValue: HasAssocWithConstraint { | ||
var #^OPAQUE_RESULT^# | ||
// OPAQUE_RESULT: Begin completions | ||
// OPAQUE_RESULT-DAG: Decl[InstanceVar]/Super: value: some HasAssocWithConstraint; | ||
// OPAQUE_RESULT: End completions | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really the case? This seems to type check and compile just fine:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is only for conformance completion.
This
S.foo(x:)
cannot be used to inferS.Assoc
. Even if the user explicitly adds, for instance,typealias Assoc = C
,func foo<T>(x: T) -> some MyProtocol
doesn't satisfy the requirement.So we can't use
some MyProtocol
as a conformance completion.