Skip to content

Commit 725aef7

Browse files
committed
swift-api-digester: reference ownership changes considered source-breaking.
1 parent 4475b07 commit 725aef7

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

test/api-digester/Inputs/cake1.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public struct S1 {
88
public class C1 {
99
public class func foo1() {}
1010
public func foo2(_ : Int) {}
11+
public weak var CIIns1 : C1?
12+
public var CIIns2 : C1?
1113
}

test/api-digester/Inputs/cake2.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public struct S1 {
88
public class C1 {
99
public func foo1() {}
1010
public func foo2(_ : ()->()) {}
11+
public var CIIns1 : C1?
12+
public weak var CIIns2 : C1?
1113
}

test/api-digester/Outputs/Cake.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Constructor S1.init(_:) has 1st parameter type change from Int to Double
1010
Func C1.foo2(_:) has 1st parameter type change from Int to () -> ()
1111

1212
==================================================== Decl Attribute changes ====================================================
13+
Var C1.CIIns1 changes from weak to strong
14+
Var C1.CIIns2 changes from strong to weak
1315
Func C1.foo1() is now not static
1416
Func S1.foo3() is now static
1517
Func S1.foo1() is now mutating

tools/swift-api-digester/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ NODE_ANNOTATION(RenameNewName)
5656
NODE_ANNOTATION(NowThrowing)
5757
NODE_ANNOTATION(NowMutating)
5858
NODE_ANNOTATION(StaticChange)
59+
NODE_ANNOTATION(OwnershipChange)
5960

6061
DECL_ATTR(deprecated)
6162

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ bool SDKNode::operator==(const SDKNode &Other) const {
888888
auto Right = (&Other)->getAs<SDKNodeDecl>();
889889
if (Left->isStatic() ^ Right->isStatic())
890890
return false;
891+
if (Left->getOwnership() != Right->getOwnership())
892+
return false;
891893
SWIFT_FALLTHROUGH;
892894
}
893895
case SDKNodeKind::Root:
@@ -1944,20 +1946,13 @@ class UpdatedNodesMap : public MatchedNodeListener {
19441946
}
19451947
};
19461948

1947-
static void detectThrowing(NodePtr L, NodePtr R) {
1949+
static void detectFuncDeclChange(NodePtr L, NodePtr R) {
19481950
assert(L->getKind() == R->getKind());
19491951
if (auto LF = dyn_cast<SDKNodeAbstractFunc>(L)) {
19501952
auto RF = R->getAs<SDKNodeAbstractFunc>();
19511953
if (!LF->isThrowing() && RF->isThrowing()) {
19521954
LF->annotate(NodeAnnotation::NowThrowing);
19531955
}
1954-
}
1955-
}
1956-
1957-
static void detectMutating(NodePtr L, NodePtr R) {
1958-
assert(L->getKind() == R->getKind());
1959-
if (auto LF = dyn_cast<SDKNodeAbstractFunc>(L)) {
1960-
auto RF = R->getAs<SDKNodeAbstractFunc>();
19611956
if (!LF->isMutating() && RF->isMutating()) {
19621957
LF->annotate(NodeAnnotation::NowMutating);
19631958
}
@@ -1975,12 +1970,15 @@ static void detectRename(NodePtr L, NodePtr R) {
19751970
}
19761971
}
19771972

1978-
static void detectStaticUpdate(NodePtr L, NodePtr R) {
1973+
static void detectDeclChange(NodePtr L, NodePtr R) {
19791974
assert(L->getKind() == R->getKind());
19801975
if (auto LD = dyn_cast<SDKNodeDecl>(L)) {
1981-
if (LD->isStatic() ^ R->getAs<SDKNodeDecl>()->isStatic()) {
1976+
auto *RD = R->getAs<SDKNodeDecl>();
1977+
if (LD->isStatic() ^ RD->isStatic())
19821978
L->annotate(NodeAnnotation::StaticChange);
1983-
}
1979+
if (LD->getOwnership() != RD->getOwnership())
1980+
L->annotate(NodeAnnotation::OwnershipChange);
1981+
detectRename(L, R);
19841982
}
19851983
}
19861984

@@ -2040,10 +2038,8 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
20402038
Right->getKind() != SDKNodeKind::Nil);
20412039
assert(Kind == SDKNodeKind::Root || *Left != *Right);
20422040

2043-
detectRename(Left, Right);
2044-
detectThrowing(Left, Right);
2045-
detectMutating(Left, Right);
2046-
detectStaticUpdate(Left, Right);
2041+
detectDeclChange(Left, Right);
2042+
detectFuncDeclChange(Left, Right);
20472043

20482044
switch(Kind) {
20492045
case SDKNodeKind::Root:
@@ -2755,9 +2751,14 @@ class DiagnosisEmitter : public SDKNodeVisitor {
27552751
struct DeclAttrDiag {
27562752
DeclKind Kind;
27572753
StringRef DeclName;
2758-
StringRef AttrName;
2759-
DeclAttrDiag(DeclKind Kind, StringRef DeclName, StringRef AttrName) :
2760-
Kind(Kind), DeclName(DeclName), AttrName(AttrName) {}
2754+
StringRef AttrBefore;
2755+
StringRef AttrAfter;
2756+
DeclAttrDiag(DeclKind Kind, StringRef DeclName, StringRef AttrBefore,
2757+
StringRef AttrAfter) : Kind(Kind), DeclName(DeclName),
2758+
AttrBefore(AttrBefore), AttrAfter(AttrAfter) {}
2759+
DeclAttrDiag(DeclKind Kind, StringRef DeclName, StringRef AttrAfter) :
2760+
DeclAttrDiag(Kind, DeclName, StringRef(), AttrAfter) {}
2761+
27612762
bool operator<(DeclAttrDiag Other) const;
27622763
void output() const;
27632764
static void theme(raw_ostream &OS) { OS << "Decl Attribute changes"; };
@@ -2887,8 +2888,12 @@ bool DiagnosisEmitter::DeclAttrDiag::operator<(DeclAttrDiag Other) const {
28872888
}
28882889

28892890
void DiagnosisEmitter::DeclAttrDiag::output() const {
2890-
llvm::outs() << Kind << " " << printName(DeclName) << " is now " <<
2891-
printDiagKeyword(AttrName) << "\n";
2891+
if (AttrBefore.empty())
2892+
llvm::outs() << Kind << " " << printName(DeclName) << " is now " <<
2893+
printDiagKeyword(AttrAfter)<< "\n";
2894+
else
2895+
llvm::outs() << Kind << " " << printName(DeclName) << " changes from " <<
2896+
printDiagKeyword(AttrBefore) << " to "<< printDiagKeyword(AttrAfter)<< "\n";
28922897
}
28932898

28942899
void DiagnosisEmitter::diagnosis(NodePtr LeftRoot, NodePtr RightRoot,
@@ -2935,6 +2940,21 @@ void DiagnosisEmitter::visitDecl(SDKNodeDecl *Node) {
29352940
Node->getFullyQualifiedName(),
29362941
InsertToBuffer(Node->isStatic() ? "not static" : "static"));
29372942
}
2943+
if (Node->isAnnotatedAs(NodeAnnotation::OwnershipChange)) {
2944+
auto getOwnershipDescription = [](swift::Ownership O) {
2945+
switch (O) {
2946+
case Ownership::Strong: return InsertToBuffer("strong");
2947+
case Ownership::Weak: return InsertToBuffer("weak");
2948+
case Ownership::Unowned: return InsertToBuffer("unowned");
2949+
case Ownership::Unmanaged: return InsertToBuffer("unowned(unsafe)");
2950+
}
2951+
};
2952+
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>();
2953+
AttrChangedDecls.Diags.emplace_back(Node->getDeclKind(),
2954+
Node->getFullyQualifiedName(),
2955+
getOwnershipDescription(Node->getOwnership()),
2956+
getOwnershipDescription(Count->getOwnership()));
2957+
}
29382958
}
29392959
void DiagnosisEmitter::visitType(SDKNodeType *Node) {
29402960
auto *Parent = Node->getParent()->getAs<SDKNodeDecl>();

0 commit comments

Comments
 (0)