Skip to content

Commit 7abf8ae

Browse files
author
Marc Rasi
committed
actually set the lazy resolver
1 parent 2d1436e commit 7abf8ae

File tree

6 files changed

+14
-15
lines changed

6 files changed

+14
-15
lines changed

include/swift/AST/Attr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,8 @@ class DerivativeAttr final
19261926
}
19271927
AbstractFunctionDecl *getOriginalFunction(ASTContext &context) const;
19281928
void setOriginalFunction(AbstractFunctionDecl *decl);
1929+
void setOriginalFunctionResolver(LazyMemberLoader *resolver,
1930+
uint64_t resolverContextData);
19291931

19301932
AutoDiffDerivativeFunctionKind getDerivativeKind() const {
19311933
assert(Kind && "Derivative function kind has not yet been resolved");

lib/AST/Attr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,13 @@ void DerivativeAttr::setOriginalFunction(AbstractFunctionDecl *decl) {
17641764
OriginalFunction = decl;
17651765
}
17661766

1767+
void DerivativeAttr::setOriginalFunctionResolver(
1768+
LazyMemberLoader *resolver, uint64_t resolverContextData) {
1769+
assert(!OriginalFunction && "cannot overwrite original function");
1770+
OriginalFunction = resolver;
1771+
ResolverContextData = resolverContextData;
1772+
}
1773+
17671774
TransposeAttr::TransposeAttr(bool implicit, SourceLoc atLoc,
17681775
SourceRange baseRange, TypeRepr *baseTypeRepr,
17691776
DeclNameRefWithLoc originalName,

lib/Serialization/Deserialization.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4379,7 +4379,6 @@ llvm::Error DeclDeserializer::deserializeDeclAttributes() {
43794379

43804380
DeclNameRefWithLoc origName{
43814381
DeclNameRef(MF.getDeclBaseName(origNameId)), DeclNameLoc()};
4382-
auto *origDecl = cast<AbstractFunctionDecl>(MF.getDecl(origDeclId));
43834382
auto derivativeKind =
43844383
getActualAutoDiffDerivativeFunctionKind(rawDerivativeKind);
43854384
if (!derivativeKind)
@@ -4392,7 +4391,7 @@ llvm::Error DeclDeserializer::deserializeDeclAttributes() {
43924391
auto *derivativeAttr =
43934392
DerivativeAttr::create(ctx, isImplicit, SourceLoc(), SourceRange(),
43944393
/*baseType*/ nullptr, origName, indices);
4395-
derivativeAttr->setOriginalFunction(origDecl);
4394+
derivativeAttr->setOriginalFunctionResolver(&MF, origDeclId);
43964395
derivativeAttr->setDerivativeKind(*derivativeKind);
43974396
Attr = derivativeAttr;
43984397
break;
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
public struct Struct {
2-
public init() {}
32
public func method(_ x: Float) -> Float { x }
4-
public static func +(_ lhs: Self, rhs: Self) -> Self {
5-
lhs
6-
}
73
}

test/AutoDiff/Sema/DerivativeRegistrationCrossModule/Inputs/b.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,4 @@ extension Struct: Differentiable {
1010
func vjpMethod(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
1111
(x, { $0 })
1212
}
13-
14-
@usableFromInline
15-
@derivative(of: +)
16-
static func vjpAdd(_ lhs: Self, rhs: Self) -> (
17-
value: Self, pullback: (TangentVector) -> (TangentVector, TangentVector)
18-
) {
19-
(lhs + rhs, { v in (v, v) })
20-
}
2113
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend -emit-module -primary-file %S/Inputs/a.swift -emit-module-path %t/a.swiftmodule
33
// RUN: %target-swift-frontend -emit-module -primary-file %S/Inputs/b.swift -emit-module-path %t/b.swiftmodule -I %t
4-
// RUN: %target-swift-frontend-typecheck -verify -I %t %s
4+
// "-verify-ignore-unknown" is for "<unknown>:0: note: 'init()' declared here"
5+
// RUN: %target-swift-frontend-typecheck -verify -verify-ignore-unknown -I %t %s
56

67
// SR-12526: Fix cross-module deserialization crash involving `@derivative` attribute.
78

89
import a
910
import b
1011

1112
func foo(_ s: Struct) {
13+
// Without this error, SR-12526 does not trigger.
14+
// expected-error @+1 {{'Struct' initializer is inaccessible due to 'internal' protection level}}
1215
_ = Struct()
1316
_ = s.method(1)
1417
}

0 commit comments

Comments
 (0)