Skip to content

Commit 685b30c

Browse files
authored
Fix TensorFlow module compilation. (#28023)
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.
1 parent 8505fee commit 685b30c

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

lib/Sema/DerivedConformanceKeyPathIterable.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ deriveBodyKeyPathIterable_allKeyPaths(AbstractFunctionDecl *funcDecl, void *) {
7878
auto *parentDC = funcDecl->getDeclContext();
7979
auto *nominal = parentDC->getSelfNominalTypeDecl();
8080
auto &C = nominal->getASTContext();
81-
auto allKeyPathsInterfaceType = computeAllKeyPathsType(nominal);
82-
auto allKeyPathsType = parentDC->mapTypeIntoContext(allKeyPathsInterfaceType);
81+
auto partialKeyPathInterfaceType = computePartialKeyPathType(nominal);
82+
auto partialKeyPathType =
83+
parentDC->mapTypeIntoContext(partialKeyPathInterfaceType);
8384

8485
auto *nominalTypeExpr = TypeExpr::createForDecl(SourceLoc(), nominal,
8586
funcDecl, /*Implicit*/ true);
@@ -97,17 +98,17 @@ deriveBodyKeyPathIterable_allKeyPaths(AbstractFunctionDecl *funcDecl, void *) {
9798
auto *dotExpr = new (C)
9899
UnresolvedDotExpr(nominalTypeExpr, SourceLoc(), member->getFullName(),
99100
DeclNameLoc(), /*Implicit*/ true);
100-
auto *keyPathExpr =
101+
Expr *keyPathExpr =
101102
new (C) KeyPathExpr(SourceLoc(), dotExpr, nullptr, /*Implicit*/ true);
103+
// NOTE(TF-575): Adding an explicit coercion expression here is necessary
104+
// due to type-checker changes.
105+
keyPathExpr = new (C) CoerceExpr(
106+
keyPathExpr, SourceLoc(), TypeLoc::withoutLoc(partialKeyPathType));
102107
keyPathExprs.push_back(keyPathExpr);
103108
}
104109
// Return array of all key path expressions.
105110
Expr *keyPathsArrayExpr =
106111
ArrayExpr::create(C, SourceLoc(), keyPathExprs, {}, SourceLoc());
107-
// NOTE(TF-575): Adding an explicit coercion expression here is necessary due
108-
// to a missing regression.
109-
keyPathsArrayExpr = new (C) CoerceExpr(
110-
keyPathsArrayExpr, SourceLoc(), TypeLoc::withoutLoc(allKeyPathsType));
111112
auto *returnStmt = new (C) ReturnStmt(SourceLoc(), keyPathsArrayExpr);
112113
auto *body = BraceStmt::create(C, SourceLoc(), {returnStmt}, SourceLoc(),
113114
/*Implicit*/ true);

stdlib/public/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ endif()
7373
# dependency for `numpy.ndarray` bridging to `ShapedArray`/`Tensor`.
7474
if(SWIFT_BUILD_STDLIB AND SWIFT_ENABLE_TENSORFLOW)
7575
# TODO: Add TensorFlow support for iOS/Raspberry Pi.
76-
# add_subdirectory(CTensorFlow)
77-
# add_subdirectory(TensorFlow)
76+
add_subdirectory(CTensorFlow)
77+
add_subdirectory(TensorFlow)
7878
endif()
7979

8080
if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)

test/Sema/struct_key_path_iterable.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@ struct DummyOptimizer<P : KeyPathIterable, Scalar : BinaryFloatingPoint>
114114
}
115115
}
116116

117+
// TF-575: Test overloaded key path component name.
118+
protocol NameLookupConflictProtocol {}
119+
extension NameLookupConflictProtocol {
120+
func member() {}
121+
}
122+
struct NameLookupConflict: NameLookupConflictProtocol & KeyPathIterable {
123+
// Note: `NameLookupConflict.member` is overloaded with
124+
// `MemberNameConflictProtocol.member`.
125+
// This makes the following generated code fail:
126+
//
127+
// var allKeyPaths: [PartialKeyPath<Self>] {
128+
// [\Self.member]
129+
// }
130+
//
131+
// error: cannot convert value of type
132+
// 'WritableKeyPath<NameLookupConflict, Float>' to expected element type
133+
// 'PartialKeyPath<NameLookupConflict>'
134+
var member: Float
135+
}
136+
117137
// Test derived conformances in disallowed contexts.
118138

119139
// expected-error @+3 {{type 'OtherFileNonconforming' does not conform to protocol 'KeyPathIterable'}}

0 commit comments

Comments
 (0)