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
[SR-5215] Generated CodingKeys type is not available in function signatures #47791
Comments
cc @itaiferber This is because the CodingKeys enum isn't synthesized until the Codable conformance is checked. We should be handling this in TypeChecker::lookupMemberType, though. |
@swift-ci Create |
@belkadan Would adding `NL_ProtocolMembers` to the qualified lookup in `lookupMemberType` normally address this? There's the following note in `NL_Options`: /// The default set of options used for qualified name lookup.
///
/// FIXME: Eventually, add NL_ProtocolMembers to this, once all of the
/// callers can handle it.
NL_QualifiedDefault = NL_RemoveNonVisible | NL_RemoveOverridden, but unfortunately, turning `NL_ProtocolMembers` on by default prevents the stdlib from compiling... (There are a ton of ambiguous name lookups in `String` views.) |
No, that's for actually looking into protocols themselves, not members synthesized as part of checking conformances. It's not clear whether the FIXME is still what we want anyway. |
@belkadan Got it, makes sense. Is it reasonable, then, to expect to be able to do eager conformance checking (to force protocol conformance synthesis) before member lookup, a la: LookupTypeResult TypeChecker::lookupMemberType(DeclContext *dc,
Type type, Identifier name,
NameLookupOptions options) {
if (auto *decl = type->getAnyNominal()) {
checkConformancesInContext(decl, decl);
}
// ...
} I think we'd both prefer to make this fix not be |
That seems really quite expensive. There's a section below marked
which is supposed to handle this, but it'll only work when the caller passes |
@belkadan In this case we never even reach there, though. The qualified lookup fails since the type doesn't yet exist, so we bail almost immediately: if (!dc->lookupQualified(type, name, subOptions, this, decls))
return result; |
Since |
I guess it's somewhat reasonable, though. Since |
For instance, the following allows lookup to succeed: auto lookupSuccess = dc->lookupQualified(type, name, subOptions, this, decls);
if (!lookupSuccess) {
// If the requested type is a CodingKeys type on something which may require
// Encodable/Decodable synthesis, it may not have been synthesized yet. If
// this is the case, force synthesis and attempt lookup again.
NominalTypeDecl *targetDecl = nullptr;
if (name == dc->getASTContext().Id_CodingKeys &&
(targetDecl = type->getAnyNominal())) {
checkConformancesInContext(targetDecl, targetDecl);
lookupSuccess = dc->lookupQualified(type, name, subOptions, this, decls);
}
}
if (!lookupSuccess)
return result; |
Ah, I forgot that CodingKeys wasn't a requirement of anything. That explains why this is special. Rather than trying to check all conformances, what do you think about asking conformsToProtocol(Encodable) and conformsToProtocol(Decodable)? That should be less work. |
@belkadan Sounds good — that's what I've put together so far. PR should be up soon with some testing. |
Up against master at PR-10723 |
Forgot to resolve this issue — this went in with PR#10930 a while back. |
Environment
Xcode Version 9.0 beta (9M136h)
Apple Swift version 4.0 (swiftlang-900.0.43 clang-900.0.22.8)
Additional Detail from JIRA
md5: 57756000cdaf8b024f7be02417094e67
Issue Description:
Referencing the generated
CodingKeys
enum compiles just fine within a method body. For example:However, if you try to include the
CodingKeys
type in a function signature, you get a compiler error saying 'CodingKeys' is not a member type of 'Broken'. For example, the following code doesn't compile:You can work around the compile error by explicitly declaring the
CodingKeys
enum like so:The text was updated successfully, but these errors were encountered: