Skip to content

[codable] When generated encode for a property, distinguish in betwee… #16139

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
Apr 25, 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
14 changes: 10 additions & 4 deletions lib/Sema/DerivedConformanceCodable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,16 @@ static void deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl) {
// Now need to generate `try container.encode(x, forKey: .x)` for all
// existing properties. Optional properties get `encodeIfPresent`.
for (auto *elt : codingKeysEnum->getAllElements()) {
VarDecl *varDecl;
for (auto decl : targetDecl->lookupDirect(DeclName(elt->getName())))
if ((varDecl = dyn_cast<VarDecl>(decl)))
break;
VarDecl *varDecl = nullptr;
for (auto decl : targetDecl->lookupDirect(DeclName(elt->getName()))) {
if (auto *vd = dyn_cast<VarDecl>(decl)) {
if (!vd->isStatic()) {
varDecl = vd;
break;
}
}
}
assert(varDecl && "Should have found at least 1 var decl");

// self.x
auto *selfRef = createSelfDeclRef(encodeDecl);
Expand Down
14 changes: 14 additions & 0 deletions test/SILGen/codable/struct_codable_member_type_lookup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-swift-frontend -emit-silgen -enable-sil-ownership %s | %FileCheck %s

// Make sure we have an int, not a float.
//
// CHECK-LABEL: sil hidden @$S33struct_codable_member_type_lookup32StaticInstanceNameDisambiguationV6encode2to{{.*}}F : $@convention(method) (@in_guaranteed Encoder, StaticInstanceNameDisambiguation) -> @error Error {
// CHECK: bb0([[ENCODER:%.*]] : @trivial $*Encoder, [[INPUT:%.*]] : @trivial $StaticInstanceNameDisambiguation):
// CHECK: [[INT_VALUE:%.*]] = struct_extract [[INPUT]]
// CHECK: [[FUNC:%.*]] = function_ref @$Ss22KeyedEncodingContainerV6encode_6forKeyySi_xtKF : $@convention(method) <τ_0_0 where τ_0_0 : CodingKey> (Int, @in_guaranteed τ_0_0, @inout KeyedEncodingContainer<τ_0_0>) -> @error Error
// CHECK: try_apply [[FUNC]]<StaticInstanceNameDisambiguation.CodingKeys>([[INT_VALUE]],
// CHECK: } // end sil function '$S33struct_codable_member_type_lookup32StaticInstanceNameDisambiguationV6encode2toys7Encoder_p_tKF'
struct StaticInstanceNameDisambiguation : Codable {
static let version: Float = 0.42
let version: Int = 42
}
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,10 @@ struct sr6886 {
struct Nested : Codable {}
let Nested: Nested // Don't crash with a coding key that is the same as a nested type name
}

// Don't crash if we have a static property with the same name as an ivar that
// we will encode. We check the actual codegen in a SILGen test.
struct StaticInstanceNameDisambiguation : Codable {
static let version: Float = 0.42
let version: Int = 42
}