-
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
[CodeCompletion] Use GenericSignature methods to get 'associatedtype' requirements #29086
Conversation
@swift-ci Please smoke test |
…tRequirementSignature() instead of AssociatedTypeDecl::getInherited() when checking if the return type should be suggested as "opaque result type" in override completion. AssociatedTypeDecl::getInherited() is not serialized. So if the protocol is declared in a module, it was never suggested as 'some' result. rdar://problem/57245073
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
db8a312
to
1dd6fe5
Compare
@swift-ci Please smoke test |
lib/IDE/CodeCompletion.cpp
Outdated
|
||
SmallVector<Type, 2> opaqueTypes; | ||
bool hasExplicitAnyObject = false; | ||
for (auto req : protoD->getRequirementSignature()) { |
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.
Instead of looking at the requirement signature, can you do this instead?
proto->getGenericSignature()->getConformsTo(ResultT)
proto->getGenericSignature()->getSuperclassBound(ResultT)
proto->getGenericSignature()->getLayoutConstraint(ResultT)
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.
Thanks! I wasn't aware of these methods. Updated.
ca42ef8
to
f0434f1
Compare
@swift-ci Please smoke test |
if (auto *FD = dyn_cast<FuncDecl>(VD)) | ||
if (auto *FD = dyn_cast<FuncDecl>(VD)) { | ||
if (FD->getGenericParams()) { | ||
// Generic function cannot have opaque result type. |
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:
func foo<T>(_ t: T) -> some Any {
return t
}
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.
protocol MyProtocol {}
class C: MyProtocol {}
protocol P {
associatedtype Assoc : MyProtocol
func foo<T>(x: T) -> Assoc
}
struct S: P {
func foo<T>(x: T) -> some MyProtocol { return C() }
}
This S.foo(x:)
cannot be used to infer S.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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
This also works fine for me:
struct S<T> {
subscript<U>(_ u: U) -> some Any {
return u
}
}
lib/IDE/CodeCompletion.cpp
Outdated
// If resolved print it. | ||
return nullptr; | ||
|
||
return assocTyD->getInherited()[0]; | ||
GenericSignature sig = ResultT->castTo<DependentMemberType>() |
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.
Isn't this just VD->getDeclContext()->getGenericSignatureOfContext()
?
f0434f1
to
b9f1e58
Compare
@swift-ci Please smoke test |
@swift-ci Please smoke test Linux |
Slava approved in person. Merging |
instead of
AssociatedTypeDecl::getInherited()
when checking if the return type should be suggested as "opaque result type" in override completion.AssociatedTypeDecl::getInherited()
is not serialized. So if the protocol is declared in a module, it was never suggested assome
result.rdar://problem/57245073
Also, stop suggesting opaque result type for generic requirements:
SE-244 explicitly states that the following example cannot infer the
associatedtype
. So we can't usesome
here.