Skip to content

Commit 673a5ee

Browse files
committed
Fixes #59100
The above referenced issue was causing a compiler crash when writing differentiable protocols and corresponding implementers, without importing the `_Differentiation` module. Changes in this CR fix the issue by marking any `@differentiable` attributes as invalid, if the `_Differentiation` module has not been imported. This ignores the `@differentiable` attributes when the protocol witnesses are being verified. Witness verification was previously leading to an error (due to missing `@differentiable` attribute on the protocol requirement implementer), and the corresponding diagnostic emission code was then leading to a crash, because it was expecting the `_Differentiation` module to be present.
1 parent 2db4a03 commit 673a5ee

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5789,7 +5789,8 @@ IndexSubset *DifferentiableAttributeTypeCheckRequest::evaluate(
57895789
void AttributeChecker::visitDifferentiableAttr(DifferentiableAttr *attr) {
57905790
// Call `getParameterIndices` to trigger
57915791
// `DifferentiableAttributeTypeCheckRequest`.
5792-
(void)attr->getParameterIndices();
5792+
if (attr->getParameterIndices() == nullptr)
5793+
attr->setInvalid();
57935794
}
57945795

57955796
/// Type-checks the given `@derivative` attribute `attr` on declaration `D`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
protocol P {
4+
@differentiable(reverse) // expected-error {{@differentiable attribute used without importing module '_Differentiation'}}
5+
func req()
6+
}
7+
8+
struct S: P {
9+
// Had the `_Differentiation` module been imported, this conformance
10+
// requirement would have generated an error, complaining about the
11+
// missing `@derivative` attribute. However, because the `_Differentiation`
12+
// module import is missing, the `@differential` attribute on `P` is marked
13+
// invalid and therefore not used while validating witnesses (specifically `S.req()`)
14+
// for the `P.req()` requirement.
15+
//
16+
// **NOTE** - This test can likely be removed when/if the differentiation module is
17+
// available by default in the Swift stdlib.
18+
public func req() {}
19+
}

0 commit comments

Comments
 (0)