Skip to content

Commit d6ff384

Browse files
author
marcrasi
authored
fix TF-489: stdlib ParseableInterface tests (swiftlang#27971)
Having `Array.DifferentiableView` only defined when `Element : Differentiable` makes the compiler quite confused, leading to issues like TF-489, TF-934. This PR works around the issue by making `Array.DifferentiableView` unconstrained. I reduced the problem to something that reproduces on master, and filed an issue for that: SR-11685. Once that issue is resolved, we can try reconstraining it.
1 parent 38bc954 commit d6ff384

File tree

6 files changed

+49
-48
lines changed

6 files changed

+49
-48
lines changed

lib/AST/Type.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3487,8 +3487,6 @@ TypeBase::getContextSubstitutions(const DeclContext *dc,
34873487
if (auto *ownerClass = dyn_cast<ClassDecl>(ownerNominal))
34883488
baseTy = baseTy->getSuperclassForDecl(ownerClass);
34893489

3490-
assert(ownerNominal == baseTy->getAnyNominal());
3491-
34923490
// Gather all of the substitutions for all levels of generic arguments.
34933491
auto genericSig = dc->getGenericSignatureOfContext();
34943492
if (!genericSig)
@@ -3498,6 +3496,9 @@ TypeBase::getContextSubstitutions(const DeclContext *dc,
34983496
unsigned n = params.size();
34993497

35003498
while (baseTy && n > 0) {
3499+
if (baseTy->is<ErrorType>())
3500+
break;
3501+
35013502
// For a bound generic type, gather the generic parameter -> generic
35023503
// argument substitutions.
35033504
if (auto boundGeneric = baseTy->getAs<BoundGenericType>()) {

stdlib/public/core/Array.swift

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,54 +1911,50 @@ internal struct _ArrayAnyHashableBox<Element: Hashable>
19111911
}
19121912

19131913
// SWIFT_ENABLE_TENSORFLOW
1914-
extension Array where Element : Differentiable {
1914+
// TODO(TF-938): Add 'Element : Differentiable' constraint.
1915+
extension Array {
19151916
/// The view of an array as the differentiable product manifold of `Element`
19161917
/// multiplied with itself `count` times.
19171918
@frozen
1918-
public struct DifferentiableView : Differentiable {
1919-
private var _base: [Element]
1920-
1921-
/// The viewed array.
1922-
// I'm implementing this as a computed property instead of directly
1923-
// exposing `_base` because the `@differentiable` annotation does not make
1924-
// the stored property actually differentiable. I think this is a bug.
1925-
// Maybe it's related to `@frozen`?
1926-
// TODO: Determine if that is a bug, and fix.
1927-
public var base: [Element] {
1928-
@differentiable(wrt: self, vjp: _vjpBase)
1929-
get { return _base }
1930-
_modify { yield &_base }
1931-
}
1919+
public struct DifferentiableView {
1920+
var _base: [Element]
1921+
}
1922+
}
19321923

1933-
@usableFromInline
1934-
func _vjpBase() ->
1935-
([Element], (Array<Element>.TangentVector) -> TangentVector) {
1936-
return (base, { $0 })
1937-
}
1924+
extension Array.DifferentiableView : Differentiable where Element : Differentiable {
1925+
/// The viewed array.
1926+
public var base: [Element] {
1927+
@differentiable(wrt: self, vjp: _vjpBase)
1928+
get { return _base }
1929+
_modify { yield &_base }
1930+
}
19381931

1939-
/// Creates a differentiable view of the given array.
1940-
@differentiable(wrt: base, vjp: _vjpInit)
1941-
public init(_ base: [Element]) { self._base = base }
1932+
@usableFromInline
1933+
func _vjpBase() ->
1934+
([Element], (Array<Element>.TangentVector) -> TangentVector) {
1935+
return (base, { $0 })
1936+
}
19421937

1943-
@usableFromInline
1944-
static func _vjpInit(_ base: [Element]) ->
1945-
(Array.DifferentiableView, (TangentVector) -> TangentVector) {
1946-
return (Array.DifferentiableView(base), { $0 })
1947-
}
1938+
/// Creates a differentiable view of the given array.
1939+
@differentiable(wrt: base, vjp: _vjpInit)
1940+
public init(_ base: [Element]) { self._base = base }
19481941

1949-
// MARK: - Differentiable conformance.
1942+
@usableFromInline
1943+
static func _vjpInit(_ base: [Element]) ->
1944+
(Array.DifferentiableView, (TangentVector) -> TangentVector) {
1945+
return (Array.DifferentiableView(base), { $0 })
1946+
}
19501947

1951-
public typealias TangentVector =
1952-
Array<Element.TangentVector>.DifferentiableView
1948+
public typealias TangentVector =
1949+
Array<Element.TangentVector>.DifferentiableView
19531950

1954-
public mutating func move(along direction: TangentVector) {
1955-
precondition(
1956-
base.count == direction.base.count,
1957-
"cannot move Array.DifferentiableView with count \(base.count) along " +
1958-
"direction with different count \(direction.base.count)")
1959-
for i in base.indices {
1960-
base[i].move(along: direction.base[i])
1961-
}
1951+
public mutating func move(along direction: TangentVector) {
1952+
precondition(
1953+
base.count == direction.base.count,
1954+
"cannot move Array.DifferentiableView with count \(base.count) along " +
1955+
"direction with different count \(direction.base.count)")
1956+
for i in base.indices {
1957+
base[i].move(along: direction.base[i])
19621958
}
19631959
}
19641960
}

stdlib/public/core/KeyPathIterable.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,20 @@ extension Array : KeyPathIterable {
104104
}
105105
}
106106

107-
extension Array.DifferentiableView : KeyPathIterable {
107+
// TODO(TF-938): Remove 'Element : Differentiable' constraint.
108+
extension Array.DifferentiableView : KeyPathIterable where Element : Differentiable {
108109
public typealias AllKeyPaths = [PartialKeyPath<Array.DifferentiableView>]
109110
public var allKeyPaths: [PartialKeyPath<Array.DifferentiableView>] {
110111
return [\Array.DifferentiableView.base]
111112
}
112113
}
113114

115+
// TODO(TF-938): Remove this.
116+
// This is neccessary now because otherwise the compiler complains:
117+
// error: conditional conformance of type 'Array<Element>.DifferentiableView' to protocol
118+
// 'KeyPathIterable' does not imply conformance to inherited protocol '_KeyPathIterableBase'
119+
extension Array.DifferentiableView : _KeyPathIterableBase where Element : Differentiable {}
120+
114121
extension Dictionary : KeyPathIterable {
115122
public typealias AllKeyPaths = [PartialKeyPath<Dictionary>]
116123
public var allKeyPaths: [PartialKeyPath<Dictionary>] {

utils/update_checkout/update-checkout-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@
489489
"clang-tools-extra": "swift-DEVELOPMENT-SNAPSHOT-2019-10-13-a",
490490
"libcxx": "swift-DEVELOPMENT-SNAPSHOT-2019-10-13-a",
491491
"tensorflow": "7c7d924821a8b1b20433c2f3f484bbd409873a84",
492-
"tensorflow-swift-apis": "f4f88d924d71eedb0709e26d0d5e4b7cfe94d830",
492+
"tensorflow-swift-apis": "e70b979b7a3c2ebd38457b8d4058a0b310b4e4b0",
493493
"tensorflow-swift-quote": "62c0a88dc64a201497a66cbbc087c0c7771edabc",
494494
"indexstore-db": "swift-DEVELOPMENT-SNAPSHOT-2019-10-13-a",
495495
"sourcekit-lsp": "swift-DEVELOPMENT-SNAPSHOT-2019-10-13-a"

validation-test/ParseableInterface/verify_all_overlays.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
continue
5151

5252
# SWIFT_ENABLE_TENSORFLOW
53-
# FIXME(TF-489): Re-enable this test after fixing `.swiftinterface` errors.
53+
# TODO(TF-939): Enable this on DifferentiationUnittest, Python, and
54+
# TensorFlow.
5455
if module_name in ["Swift", "SwiftLang", "DifferentiationUnittest", "Python", "TensorFlow"]:
5556
continue
5657

validation-test/ParseableInterface/verify_stdlib.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Note that this test should still "pass" when Swift.swiftinterface has not
22
// been generated.
33

4-
// SWIFT_ENABLE_TENSORFLOW
5-
// FIXME(TF-489): Re-enable this test after fixing `.swiftinterface` errors.
6-
// UNSUPPORTED: nonexecutable_test
7-
84
// RUN: %empty-directory(%t)
95
// RUN: not ls %platform-module-dir/Swift.swiftmodule/%target-cpu.swiftinterface || %target-swift-frontend -build-module-from-parseable-interface %platform-module-dir/Swift.swiftmodule/%target-cpu.swiftinterface -parse-stdlib -o %t/Swift.swiftmodule
106

0 commit comments

Comments
 (0)