From d6d59eef08d7cce5c2f1ca488e84ad105d40bbfa Mon Sep 17 00:00:00 2001 From: Dan Zheng Date: Fri, 1 Nov 2019 15:17:20 -0700 Subject: [PATCH 1/2] Fix TensorFlow module compilation. Fix `KeyPathIterable` derived conformances: `KeyPathIterable.allKeyPaths` synthesis logic now uses different key-path expression coercion logic to accommodate upstream type-checker changes. See TF-575 for more information. --- lib/Sema/DerivedConformanceKeyPathIterable.cpp | 15 ++++++++------- stdlib/public/CMakeLists.txt | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/Sema/DerivedConformanceKeyPathIterable.cpp b/lib/Sema/DerivedConformanceKeyPathIterable.cpp index 3a1273a98c483..e7474128645a1 100644 --- a/lib/Sema/DerivedConformanceKeyPathIterable.cpp +++ b/lib/Sema/DerivedConformanceKeyPathIterable.cpp @@ -78,8 +78,9 @@ deriveBodyKeyPathIterable_allKeyPaths(AbstractFunctionDecl *funcDecl, void *) { auto *parentDC = funcDecl->getDeclContext(); auto *nominal = parentDC->getSelfNominalTypeDecl(); auto &C = nominal->getASTContext(); - auto allKeyPathsInterfaceType = computeAllKeyPathsType(nominal); - auto allKeyPathsType = parentDC->mapTypeIntoContext(allKeyPathsInterfaceType); + auto partialKeyPathInterfaceType = computePartialKeyPathType(nominal); + auto partialKeyPathType = + parentDC->mapTypeIntoContext(partialKeyPathInterfaceType); auto *nominalTypeExpr = TypeExpr::createForDecl(SourceLoc(), nominal, funcDecl, /*Implicit*/ true); @@ -97,17 +98,17 @@ deriveBodyKeyPathIterable_allKeyPaths(AbstractFunctionDecl *funcDecl, void *) { auto *dotExpr = new (C) UnresolvedDotExpr(nominalTypeExpr, SourceLoc(), member->getFullName(), DeclNameLoc(), /*Implicit*/ true); - auto *keyPathExpr = + Expr *keyPathExpr = new (C) KeyPathExpr(SourceLoc(), dotExpr, nullptr, /*Implicit*/ true); + // NOTE(TF-575): Adding an explicit coercion expression here is necessary + // due to type-checker changes. + keyPathExpr = new (C) CoerceExpr( + keyPathExpr, SourceLoc(), TypeLoc::withoutLoc(partialKeyPathType)); keyPathExprs.push_back(keyPathExpr); } // Return array of all key path expressions. Expr *keyPathsArrayExpr = ArrayExpr::create(C, SourceLoc(), keyPathExprs, {}, SourceLoc()); - // NOTE(TF-575): Adding an explicit coercion expression here is necessary due - // to a missing regression. - keyPathsArrayExpr = new (C) CoerceExpr( - keyPathsArrayExpr, SourceLoc(), TypeLoc::withoutLoc(allKeyPathsType)); auto *returnStmt = new (C) ReturnStmt(SourceLoc(), keyPathsArrayExpr); auto *body = BraceStmt::create(C, SourceLoc(), {returnStmt}, SourceLoc(), /*Implicit*/ true); diff --git a/stdlib/public/CMakeLists.txt b/stdlib/public/CMakeLists.txt index 2f7a580ea96b5..945c141782adc 100644 --- a/stdlib/public/CMakeLists.txt +++ b/stdlib/public/CMakeLists.txt @@ -73,8 +73,8 @@ endif() # dependency for `numpy.ndarray` bridging to `ShapedArray`/`Tensor`. if(SWIFT_BUILD_STDLIB AND SWIFT_ENABLE_TENSORFLOW) # TODO: Add TensorFlow support for iOS/Raspberry Pi. - # add_subdirectory(CTensorFlow) - # add_subdirectory(TensorFlow) + add_subdirectory(CTensorFlow) + add_subdirectory(TensorFlow) endif() if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR) From 9efd87e35803ab6c5c89a10ccc452fe08f5956fe Mon Sep 17 00:00:00 2001 From: Dan Zheng Date: Fri, 1 Nov 2019 16:21:30 -0700 Subject: [PATCH 2/2] Add test. --- test/Sema/struct_key_path_iterable.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/Sema/struct_key_path_iterable.swift b/test/Sema/struct_key_path_iterable.swift index 1b3a7c8def252..a56ef211af6d4 100644 --- a/test/Sema/struct_key_path_iterable.swift +++ b/test/Sema/struct_key_path_iterable.swift @@ -114,6 +114,26 @@ struct DummyOptimizer

} } +// TF-575: Test overloaded key path component name. +protocol NameLookupConflictProtocol {} +extension NameLookupConflictProtocol { + func member() {} +} +struct NameLookupConflict: NameLookupConflictProtocol & KeyPathIterable { + // Note: `NameLookupConflict.member` is overloaded with + // `MemberNameConflictProtocol.member`. + // This makes the following generated code fail: + // + // var allKeyPaths: [PartialKeyPath] { + // [\Self.member] + // } + // + // error: cannot convert value of type + // 'WritableKeyPath' to expected element type + // 'PartialKeyPath' + var member: Float +} + // Test derived conformances in disallowed contexts. // expected-error @+3 {{type 'OtherFileNonconforming' does not conform to protocol 'KeyPathIterable'}}