Skip to content

Commit 8b73c98

Browse files
authored
Fixes #59100 (#67162)
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 00729ad commit 8b73c98

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
@@ -5786,8 +5786,10 @@ IndexSubset *DifferentiableAttributeTypeCheckRequest::evaluate(
57865786

57875787
// `@differentiable` attribute requires experimental differentiable
57885788
// programming to be enabled.
5789-
if (checkIfDifferentiableProgrammingEnabled(attr, D))
5789+
if (checkIfDifferentiableProgrammingEnabled(attr, D)) {
5790+
attr->setInvalid();
57905791
return nullptr;
5792+
}
57915793

57925794
// If `@differentiable` attribute is declared directly on a
57935795
// `AbstractStorageDecl` (a stored/computed property or subscript),
@@ -5797,8 +5799,10 @@ IndexSubset *DifferentiableAttributeTypeCheckRequest::evaluate(
57975799

57985800
// Resolve the original `AbstractFunctionDecl`.
57995801
auto *original = resolveDifferentiableAttrOriginalFunction(attr);
5800-
if (!original)
5802+
if (!original) {
5803+
attr->setInvalid();
58015804
return nullptr;
5805+
}
58025806

58035807
return typecheckDifferentiableAttrforDecl(original, attr);
58045808
}
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)