Skip to content

Commit a146cc0

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 b426c15 commit a146cc0

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5780,8 +5780,10 @@ IndexSubset *DifferentiableAttributeTypeCheckRequest::evaluate(
57805780

57815781
// `@differentiable` attribute requires experimental differentiable
57825782
// programming to be enabled.
5783-
if (checkIfDifferentiableProgrammingEnabled(attr, D))
5783+
if (checkIfDifferentiableProgrammingEnabled(attr, D)) {
5784+
attr->setInvalid();
57845785
return nullptr;
5786+
}
57855787

57865788
// If `@differentiable` attribute is declared directly on a
57875789
// `AbstractStorageDecl` (a stored/computed property or subscript),
@@ -5791,8 +5793,10 @@ IndexSubset *DifferentiableAttributeTypeCheckRequest::evaluate(
57915793

57925794
// Resolve the original `AbstractFunctionDecl`.
57935795
auto *original = resolveDifferentiableAttrOriginalFunction(attr);
5794-
if (!original)
5796+
if (!original) {
5797+
attr->setInvalid();
57955798
return nullptr;
5799+
}
57965800

57975801
return typecheckDifferentiableAttrforDecl(original, attr);
57985802
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
public func req() {}
16+
}

0 commit comments

Comments
 (0)