@@ -3473,6 +3473,18 @@ void Parser::setLocalDiscriminatorToParamList(ParameterList *PL) {
3473
3473
}
3474
3474
}
3475
3475
3476
+ // / Set the original declaration in `@differentiable` attributes.
3477
+ // /
3478
+ // / Necessary because `Parser::parseNewDeclAttribute` (which calls
3479
+ // / `Parser::parseDifferentiableAttribute`) does not have access to the
3480
+ // / parent declaration of parsed attributes.
3481
+ static void
3482
+ setOriginalDeclarationForDifferentiableAttributes (DeclAttributes attrs,
3483
+ Decl *D) {
3484
+ for (auto *attr : attrs.getAttributes <DifferentiableAttr>())
3485
+ const_cast <DifferentiableAttr *>(attr)->setOriginalDeclaration (D);
3486
+ }
3487
+
3476
3488
// / Parse a single syntactic declaration and return a list of decl
3477
3489
// / ASTs. This can return multiple results for var decls that bind to multiple
3478
3490
// / values, structs that define a struct decl and a constructor, etc.
@@ -3873,7 +3885,8 @@ Parser::parseDecl(ParseDeclOptions Flags,
3873
3885
if (DeclResult.isNonNull ()) {
3874
3886
Decl *D = DeclResult.get ();
3875
3887
if (!declWasHandledAlready (D))
3876
- Handler (DeclResult.get ());
3888
+ Handler (D);
3889
+ setOriginalDeclarationForDifferentiableAttributes (D->getAttrs (), D);
3877
3890
}
3878
3891
3879
3892
if (!DeclResult.isParseError ()) {
@@ -5581,6 +5594,11 @@ Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
5581
5594
5582
5595
accessors.record (*this , PrimaryVar, Invalid);
5583
5596
5597
+ // Set original declaration in `@differentiable` attributes.
5598
+ for (auto *accessor : accessors.Accessors )
5599
+ setOriginalDeclarationForDifferentiableAttributes (accessor->getAttrs (),
5600
+ accessor);
5601
+
5584
5602
return makeParserResult (PrimaryVar);
5585
5603
}
5586
5604
@@ -5836,6 +5854,10 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
5836
5854
VD->setStatic (StaticLoc.isValid ());
5837
5855
VD->getAttrs () = Attributes;
5838
5856
setLocalDiscriminator (VD);
5857
+
5858
+ // Set original declaration in `@differentiable` attributes.
5859
+ setOriginalDeclarationForDifferentiableAttributes (Attributes, VD);
5860
+
5839
5861
Decls.push_back (VD);
5840
5862
if (hasOpaqueReturnTy && sf && !InInactiveClauseEnvironment) {
5841
5863
sf->addUnvalidatedDeclWithOpaqueResultType (VD);
@@ -7083,6 +7105,11 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
7083
7105
7084
7106
accessors.record (*this , Subscript, (Invalid || !Status.isSuccess ()));
7085
7107
7108
+ // Set original declaration in `@differentiable` attributes.
7109
+ for (auto *accessor : accessors.Accessors )
7110
+ setOriginalDeclarationForDifferentiableAttributes (accessor->getAttrs (),
7111
+ accessor);
7112
+
7086
7113
// No need to setLocalDiscriminator because subscripts cannot
7087
7114
// validly appear outside of type decls.
7088
7115
return makeParserResult (Status, Subscript);
0 commit comments