@@ -598,15 +598,15 @@ static void deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl) {
598
598
// Now need to generate `try container.encode(x, forKey: .x)` for all
599
599
// existing properties. Optional properties get `encodeIfPresent`.
600
600
for (auto *elt : codingKeysEnum->getAllElements ()) {
601
- // Only ill-formed code would produce multiple results for this lookup.
602
- // This would get diagnosed later anyway, so we're free to only look at
603
- // the first result here.
604
- auto matchingVars = targetDecl-> lookupDirect ( DeclName (elt-> getName ())) ;
601
+ VarDecl *varDecl;
602
+ for ( auto decl : targetDecl-> lookupDirect ( DeclName (elt-> getName ())))
603
+ if ((varDecl = dyn_cast<VarDecl>(decl)))
604
+ break ;
605
605
606
606
// self.x
607
607
auto *selfRef = createSelfDeclRef (encodeDecl);
608
608
auto *varExpr = new (C) MemberRefExpr (selfRef, SourceLoc (),
609
- ConcreteDeclRef (matchingVars[ 0 ] ),
609
+ ConcreteDeclRef (varDecl ),
610
610
DeclNameLoc (), /* Implicit=*/ true );
611
611
612
612
// CodingKeys.x
@@ -616,7 +616,7 @@ static void deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl) {
616
616
617
617
// encode(_:forKey:)/encodeIfPresent(_:forKey:)
618
618
auto methodName = C.Id_encode ;
619
- auto varType = cast<VarDecl>(matchingVars[ 0 ]) ->getType ();
619
+ auto varType = varDecl ->getType ();
620
620
if (auto referenceType = varType->getAs <ReferenceStorageType>()) {
621
621
// This is a weak/unowned/unmanaged var. Get the inner type before
622
622
// checking optionality.
@@ -866,11 +866,10 @@ static void deriveBodyDecodable_init(AbstractFunctionDecl *initDecl) {
866
866
// Now need to generate `x = try container.decode(Type.self, forKey: .x)`
867
867
// for all existing properties. Optional properties get `decodeIfPresent`.
868
868
for (auto *elt : enumElements) {
869
- // Only ill-formed code would produce multiple results for this lookup.
870
- // This would get diagnosed later anyway, so we're free to only look at
871
- // the first result here.
872
- auto matchingVars = targetDecl->lookupDirect (DeclName (elt->getName ()));
873
- auto *varDecl = cast<VarDecl>(matchingVars[0 ]);
869
+ VarDecl *varDecl;
870
+ for (auto decl : targetDecl->lookupDirect (DeclName (elt->getName ())))
871
+ if ((varDecl = dyn_cast<VarDecl>(decl)))
872
+ break ;
874
873
875
874
// Don't output a decode statement for a var let with a default value.
876
875
if (varDecl->isLet () && varDecl->getParentInitializer () != nullptr )
0 commit comments