Skip to content

[4.2] Eagerly create init(from:) when looking up 'init' on a Decodable type #17732

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
merged 1 commit into from
Jul 4, 2018
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
11 changes: 7 additions & 4 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9032,6 +9032,9 @@ void TypeChecker::synthesizeMemberForLookup(NominalTypeDecl *target,
//
// Returns whether the target conforms to the protocol.
auto evaluateTargetConformanceTo = [&](ProtocolDecl *protocol) {
if (!protocol)
return false;

auto targetType = target->getDeclaredInterfaceType();
if (auto ref = conformsToProtocol(
targetType, protocol, target,
Expand Down Expand Up @@ -9069,12 +9072,11 @@ void TypeChecker::synthesizeMemberForLookup(NominalTypeDecl *target,
}
} else {
auto argumentNames = member.getArgumentNames();
if (argumentNames.size() != 1)
if (member.isCompoundName() && argumentNames.size() != 1)
return;

auto argumentName = argumentNames.front();
if (baseName == DeclBaseName::createConstructor() &&
argumentName == Context.Id_from) {
(member.isSimpleName() || argumentNames.front() == Context.Id_from)) {
// init(from:) may be synthesized as part of derived conformance to the
// Decodable protocol.
// If the target should conform to the Decodable protocol, check the
Expand All @@ -9083,7 +9085,8 @@ void TypeChecker::synthesizeMemberForLookup(NominalTypeDecl *target,
(void)evaluateTargetConformanceTo(decodableProto);
} else if (!baseName.isSpecial() &&
baseName.getIdentifier() == Context.Id_encode &&
argumentName == Context.Id_to) {
(member.isSimpleName() ||
argumentNames.front() == Context.Id_to)) {
// encode(to:) may be synthesized as part of derived conformance to the
// Encodable protocol.
// If the target should conform to the Encodable protocol, check the
Expand Down
25 changes: 25 additions & 0 deletions test/decl/protocol/special/coding/class_codable_inherited.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s

class SR8083_Base: Codable {
var thing: String { return "Abstract" }
}

class SR8083_Sub: SR8083_Base {
override var thing: String { return "Yo" }
}

func sr8083(decoder: Decoder) throws {
_ = try SR8083_Sub(from: decoder)
}

// CHECK-LABEL: sil_vtable SR8083_Base {
// CHECK-DAG: #SR8083_Base.init!initializer.1: (SR8083_Base.Type) -> () -> SR8083_Base : @$S23class_codable_inherited11SR8083_BaseCACycfc // SR8083_Base.init()
// CHECK-DAG: #SR8083_Base.init!allocator.1: (SR8083_Base.Type) -> (Decoder) throws -> SR8083_Base : @$S23class_codable_inherited11SR8083_BaseC4fromACs7Decoder_p_tKcfC
// CHECK-DAG: #SR8083_Base.init!initializer.1: (SR8083_Base.Type) -> (Decoder) throws -> SR8083_Base : @$S23class_codable_inherited11SR8083_BaseC4fromACs7Decoder_p_tKcfc // SR8083_Base.init(from:)
// CHECK: {{^}$}}

// CHECK-LABEL: sil_vtable SR8083_Sub {
// CHECK-DAG: #SR8083_Base.init!initializer.1: (SR8083_Base.Type) -> () -> SR8083_Base : @$S23class_codable_inherited10SR8083_SubCACycfc [override] // SR8083_Sub.init()
// CHECK-DAG: #SR8083_Base.init!allocator.1: (SR8083_Base.Type) -> (Decoder) throws -> SR8083_Base : @$S23class_codable_inherited10SR8083_SubC4fromACs7Decoder_p_tKcfC [override] // SR8083_Sub.__allocating_init(from:)
// CHECK-DAG: #SR8083_Base.init!initializer.1: (SR8083_Base.Type) -> (Decoder) throws -> SR8083_Base : @$S23class_codable_inherited10SR8083_SubC4fromACs7Decoder_p_tKcfc [override] // SR8083_Sub.init(from:)
// CHECK: {{^}$}}