Skip to content

[5.2][CodeCompletion] Use GenericSignature methods to get 'associatedtype' requirements #29104

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
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
4 changes: 2 additions & 2 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3008,10 +3008,10 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
Printer << " -> ";

TypeLoc elementTy = decl->getElementTypeLoc();
Printer.printDeclResultTypePre(decl, elementTy);
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
if (!elementTy.getTypeRepr())
elementTy = TypeLoc::withoutLoc(decl->getElementInterfaceType());
Printer.printDeclResultTypePre(decl, elementTy);
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);

// HACK: When printing result types for subscripts with opaque result types,
// always print them using the `some` keyword instead of printing
Expand Down
68 changes: 47 additions & 21 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4196,8 +4196,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;
Expand All @@ -4207,50 +4207,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.
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 {
Expand All @@ -4262,7 +4288,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
void printDeclResultTypePre(ValueDecl *VD, TypeLoc &TL) override {
if (!OpaqueBaseTy.isNull()) {
OS << "some ";
TL = OpaqueBaseTy;
TL = TypeLoc::withoutLoc(OpaqueBaseTy);
}
}
};
Expand All @@ -4272,7 +4298,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);
Expand Down
22 changes: 22 additions & 0 deletions test/IDE/complete_crossmodule.swift
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
}
36 changes: 32 additions & 4 deletions test/IDE/complete_opaque_result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protocol HasAssocWithSuperClassConstraint {
}
protocol HasAssocWithCompositionConstraint {
associatedtype AssocWithCompositionConstraint: MyClass & MyProtocol
subscript<T>(idx: T) -> AssocWithCompositionConstraint where T: Comparable { get }
subscript(idx: Int) -> AssocWithCompositionConstraint { get }
}
protocol HasAssocWithDefault {
associatedtype AssocWithDefault = MyEnum
Expand All @@ -102,22 +102,46 @@ protocol HasAssocWithConstraintAndDefault {
associatedtype AssocWithConstraintAndDefault: MyProtocol = ConcreteMyProtocol
func returnAssocWithConstraintAndDefault() -> AssocWithConstraintAndDefault
}
protocol HasAssocWithAnyObjectConstraint {
associatedtype AssocWithAnyObjectConstraint: AnyObject & MyProtocol
func returnAssocWithAnyObjectConstraint() -> AssocWithAnyObjectConstraint
}
protocol HasAssocWithConstraintOnProto where Self.AssocWithConstraintOnProto : MyProtocol {
associatedtype AssocWithConstraintOnProto
func returnAssocWithConstraintOnProto() -> AssocWithConstraintOnProto
}
protocol HasAssocWithSameTypeConstraint where Self.AssocWithSameTypeConstraint == ConcreteMyProtocol {
associatedtype AssocWithSameTypeConstraint : MyProtocol
func returnAssocWithSameTypeConstraint() -> AssocWithSameTypeConstraint
}
protocol HasAssocWithConformanceConstraintGeneric {
associatedtype AssocWithConformanceConstraintGeneric: MyProtocol
func returnAssocWithConformanceConstraintGeneric<T>(arg: T) -> AssocWithConformanceConstraintGeneric
}

class TestClass :
HasAssocPlain,
HasAssocWithConformanceConstraint,
HasAssocWithSuperClassConstraint,
HasAssocWithCompositionConstraint,
HasAssocWithDefault,
HasAssocWithConstraintAndDefault {
HasAssocWithConstraintAndDefault,
HasAssocWithAnyObjectConstraint,
HasAssocWithConstraintOnProto,
HasAssocWithSameTypeConstraint,
HasAssocWithConformanceConstraintGeneric {
#^OVERRIDE_TestClass^#
// OVERRIDE: Begin completions
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocPlain() -> AssocPlain {|};
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithConformanceConstraint(fn: (Int) -> Int) -> some MyProtocol {|};
// OVERRIDE-DAG: Decl[InstanceVar]/Super: var valAssocWithSuperClassConstraint: some MyClass;
// OVERRIDE-DAG: Decl[Subscript]/Super: subscript<T>(idx: T) -> some MyClass & MyProtocol where T : Comparable {|};
// OVERRIDE-DAG: Decl[Subscript]/Super: subscript(idx: Int) -> some MyClass & MyProtocol {|};
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithDefault() -> MyEnum {|};
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithConstraintAndDefault() -> ConcreteMyProtocol {|};
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithAnyObjectConstraint() -> some MyProtocol & AnyObject {|}
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithConstraintOnProto() -> some MyProtocol {|}
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithSameTypeConstraint() -> AssocWithSameTypeConstraint {|}
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithConformanceConstraintGeneric<T>(arg: T) -> AssocWithConformanceConstraintGeneric {|}
// OVERRIDE: End completions
}

Expand All @@ -127,7 +151,11 @@ struct TestStruct :
HasAssocWithSuperClassConstraint,
HasAssocWithCompositionConstraint,
HasAssocWithDefault,
HasAssocWithConstraintAndDefault {
HasAssocWithConstraintAndDefault,
HasAssocWithAnyObjectConstraint,
HasAssocWithConstraintOnProto,
HasAssocWithSameTypeConstraint,
HasAssocWithConformanceConstraintGeneric {
#^OVERRIDE_TestStruct^#
}

Expand Down