Skip to content

Commit 152e428

Browse files
committed
[CodeCompletion] Stop suggesting opaque result type for generic function
in override completion. As per SE-0244: > Associated type inference can only infer an opaque result type for a > non-generic requirement, because the opaque type is parameterized by > the function's own generic arguments (cherry picked from commit 1dd6fe5)
1 parent 3ba745c commit 152e428

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4207,14 +4207,23 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
42074207
return nullptr;
42084208

42094209
Type ResultT;
4210-
if (auto *FD = dyn_cast<FuncDecl>(VD))
4210+
if (auto *FD = dyn_cast<FuncDecl>(VD)) {
4211+
if (FD->getGenericParams()) {
4212+
// Generic function cannot have opaque result type.
4213+
return nullptr;
4214+
}
42114215
ResultT = FD->getResultInterfaceType();
4212-
else if (auto *SD = dyn_cast<SubscriptDecl>(VD))
4216+
} else if (auto *SD = dyn_cast<SubscriptDecl>(VD)) {
4217+
if (SD->getGenericParams()) {
4218+
// Generic subscript cannot have opaque result type.
4219+
return nullptr;
4220+
}
42134221
ResultT = SD->getElementInterfaceType();
4214-
else if (auto *VarD = dyn_cast<VarDecl>(VD))
4222+
} else if (auto *VarD = dyn_cast<VarDecl>(VD)) {
42154223
ResultT = VarD->getInterfaceType();
4216-
else
4224+
} else {
42174225
return nullptr;
4226+
}
42184227

42194228
if (!ResultT->is<DependentMemberType>() ||
42204229
!ResultT->castTo<DependentMemberType>()->getAssocType())

test/IDE/complete_opaque_result.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protocol HasAssocWithSuperClassConstraint {
9292
}
9393
protocol HasAssocWithCompositionConstraint {
9494
associatedtype AssocWithCompositionConstraint: MyClass & MyProtocol
95-
subscript<T>(idx: T) -> AssocWithCompositionConstraint where T: Comparable { get }
95+
subscript(idx: Int) -> AssocWithCompositionConstraint { get }
9696
}
9797
protocol HasAssocWithDefault {
9898
associatedtype AssocWithDefault = MyEnum
@@ -114,6 +114,10 @@ protocol HasAssocWithSameTypeConstraint where Self.AssocWithSameTypeConstraint =
114114
associatedtype AssocWithSameTypeConstraint
115115
func returnAssocWithSameTypeConstraint() -> AssocWithSameTypeConstraint
116116
}
117+
protocol HasAssocWithConformanceConstraintGeneric {
118+
associatedtype AssocWithConformanceConstraintGeneric: MyProtocol
119+
func returnAssocWithConformanceConstraintGeneric<T>(arg: T) -> AssocWithConformanceConstraintGeneric
120+
}
117121

118122
class TestClass :
119123
HasAssocPlain,
@@ -124,18 +128,20 @@ class TestClass :
124128
HasAssocWithConstraintAndDefault,
125129
HasAssocWithAnyObjectConstraint,
126130
HasAssocWithConstraintOnProto,
127-
HasAssocWithSameTypeConstraint {
131+
HasAssocWithSameTypeConstraint,
132+
HasAssocWithConformanceConstraintGeneric {
128133
#^OVERRIDE_TestClass^#
129134
// OVERRIDE: Begin completions
130135
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocPlain() -> AssocPlain {|};
131136
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithConformanceConstraint(fn: (Int) -> Int) -> some MyProtocol {|};
132137
// OVERRIDE-DAG: Decl[InstanceVar]/Super: var valAssocWithSuperClassConstraint: some MyClass;
133-
// OVERRIDE-DAG: Decl[Subscript]/Super: subscript<T>(idx: T) -> some MyClass & MyProtocol where T : Comparable {|};
138+
// OVERRIDE-DAG: Decl[Subscript]/Super: subscript(idx: Int) -> some MyClass & MyProtocol {|};
134139
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithDefault() -> MyEnum {|};
135140
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithConstraintAndDefault() -> ConcreteMyProtocol {|};
136141
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithAnyObjectConstraint() -> some MyProtocol & AnyObject {|}
137142
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithConstraintOnProto() -> some MyProtocol {|}
138143
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithSameTypeConstraint() -> AssocWithSameTypeConstraint {|}
144+
// OVERRIDE-DAG: Decl[InstanceMethod]/Super: func returnAssocWithConformanceConstraintGeneric<T>(arg: T) -> AssocWithConformanceConstraintGeneric {|}
139145
// OVERRIDE: End completions
140146
}
141147

@@ -148,7 +154,8 @@ struct TestStruct :
148154
HasAssocWithConstraintAndDefault,
149155
HasAssocWithAnyObjectConstraint,
150156
HasAssocWithConstraintOnProto,
151-
HasAssocWithSameTypeConstraint {
157+
HasAssocWithSameTypeConstraint,
158+
HasAssocWithConformanceConstraintGeneric {
152159
#^OVERRIDE_TestStruct^#
153160
}
154161

0 commit comments

Comments
 (0)