Skip to content

[Sema] Crash fix for nested type named identically to coding key #14548

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
Feb 12, 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
21 changes: 10 additions & 11 deletions lib/Sema/DerivedConformanceCodable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,15 +598,15 @@ 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()) {
// Only ill-formed code would produce multiple results for this lookup.
// This would get diagnosed later anyway, so we're free to only look at
// the first result here.
auto matchingVars = targetDecl->lookupDirect(DeclName(elt->getName()));
VarDecl *varDecl;
for (auto decl : targetDecl->lookupDirect(DeclName(elt->getName())))
if ((varDecl = dyn_cast<VarDecl>(decl)))
break;

// self.x
auto *selfRef = createSelfDeclRef(encodeDecl);
auto *varExpr = new (C) MemberRefExpr(selfRef, SourceLoc(),
ConcreteDeclRef(matchingVars[0]),
ConcreteDeclRef(varDecl),
DeclNameLoc(), /*Implicit=*/true);

// CodingKeys.x
Expand All @@ -616,7 +616,7 @@ static void deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl) {

// encode(_:forKey:)/encodeIfPresent(_:forKey:)
auto methodName = C.Id_encode;
auto varType = cast<VarDecl>(matchingVars[0])->getType();
auto varType = varDecl->getType();
if (auto referenceType = varType->getAs<ReferenceStorageType>()) {
// This is a weak/unowned/unmanaged var. Get the inner type before
// checking optionality.
Expand Down Expand Up @@ -866,11 +866,10 @@ static void deriveBodyDecodable_init(AbstractFunctionDecl *initDecl) {
// Now need to generate `x = try container.decode(Type.self, forKey: .x)`
// for all existing properties. Optional properties get `decodeIfPresent`.
for (auto *elt : enumElements) {
// Only ill-formed code would produce multiple results for this lookup.
// This would get diagnosed later anyway, so we're free to only look at
// the first result here.
auto matchingVars = targetDecl->lookupDirect(DeclName(elt->getName()));
auto *varDecl = cast<VarDecl>(matchingVars[0]);
VarDecl *varDecl;
for (auto decl : targetDecl->lookupDirect(DeclName(elt->getName())))
if ((varDecl = dyn_cast<VarDecl>(decl)))
break;

// Don't output a decode statement for a var let with a default value.
if (varDecl->isLet() && varDecl->getParentInitializer() != nullptr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,8 @@ extension C.Inner {
struct GenericCodableStruct<T : Codable> : Codable {}

func foo(_: GenericCodableStruct<Int>.CodingKeys) // expected-error {{'CodingKeys' is inaccessible due to 'private' protection level}}

struct sr6886 {
struct Nested : Codable {}
let Nested: Nested // Don't crash with a coding key that is the same as a nested type name
}