Skip to content

swift-module-digester: diagnose type witness type changes when checking ABI stability. #20058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsModuleDiffer.def
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ ERROR(func_self_access_change,none,"%0 has self access kind changing from %1 to

ERROR(param_ownership_change,none,"%0 has %1 changing from %2 to %3", (StringRef, StringRef, StringRef, StringRef))

ERROR(type_witness_change,none,"%0 has type witness type for %1 changing from %2 to %3", (StringRef, StringRef, StringRef, StringRef))

#ifndef DIAG_NO_UNDEF
# if defined(DIAG)
# undef DIAG
Expand Down
8 changes: 8 additions & 0 deletions test/api-digester/Inputs/cake1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,11 @@ public class FinalFuncContainer {
public func NewFinalFunc() {}
public final func NoLongerFinalFunc() {}
}

public protocol AssociatedTypesProtocol {
associatedtype T
}

public class TChangesFromIntToString: AssociatedTypesProtocol {
public typealias T = Int
}
8 changes: 8 additions & 0 deletions test/api-digester/Inputs/cake2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,11 @@ public class FinalFuncContainer {
public final func NewFinalFunc() {}
public func NoLongerFinalFunc() {}
}

public protocol AssociatedTypesProtocol {
associatedtype T
}

public class TChangesFromIntToString: AssociatedTypesProtocol {
public typealias T = String
}
1 change: 1 addition & 0 deletions test/api-digester/Outputs/Cake-abi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cake1: Struct Somestruct2 has been renamed to Struct NSSomestruct2

/* Type Changes */
cake1: AssociatedType AssociatedTypePro.T3 has default type change from C1 to C6
cake1: Class TChangesFromIntToString has type witness type for AssociatedTypesProtocol.T changing from Int to String
cake1: Constructor S1.init(_:) has parameter 0 type change from Int to Double
cake1: Func C1.foo2(_:) has parameter 0 type change from Int to () -> ()
cake1: Func C7.foo(_:_:) has removed default argument from parameter 0
Expand Down
1 change: 1 addition & 0 deletions test/api-digester/Outputs/Cake.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cake1: Func C7.foo(_:_:) has removed default argument from parameter 0
cake1: Func C7.foo(_:_:) has removed default argument from parameter 1
cake1: Func ownershipChange(_:_:) has parameter 0 changing from InOut to Default
cake1: Func ownershipChange(_:_:) has parameter 1 changing from Shared to Owned
cake1: TypeAlias TChangesFromIntToString.T has underlying type change from Int to String

/* Decl Attribute changes */
cake1: Func C1.foo1() is now not static
Expand Down
18 changes: 8 additions & 10 deletions tools/swift-api-digester/ModuleAnalyzerNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ StringRef SDKNodeType::getParamValueOwnership() const {

StringRef SDKNodeType::getTypeRoleDescription() const {
assert(isTopLevelType());
auto P = cast<SDKNodeDecl>(getParent());
auto P = getParent();
switch(P->getKind()) {
case SDKNodeKind::Root:
case SDKNodeKind::TypeNominal:
Expand All @@ -335,7 +335,6 @@ StringRef SDKNodeType::getTypeRoleDescription() const {
case SDKNodeKind::DeclType:
case SDKNodeKind::DeclOperator:
case SDKNodeKind::Conformance:
case SDKNodeKind::TypeWitness:
llvm_unreachable("Type Parent is wrong");
case SDKNodeKind::DeclFunction:
case SDKNodeKind::DeclConstructor:
Expand All @@ -350,6 +349,8 @@ StringRef SDKNodeType::getTypeRoleDescription() const {
return "underlying";
case SDKNodeKind::DeclAssociatedType:
return "default";
case SDKNodeKind::TypeWitness:
return "type witness type";
}
}

Expand All @@ -376,14 +377,6 @@ StringRef SDKNodeDecl::getScreenInfo() const {
return Ctx.buffer(OS.str());
}

bool SDKNodeDecl::isSDKPrivate() const {
if (getName().startswith("__"))
return true;
if (auto *PD = dyn_cast<SDKNodeDecl>(getParent()))
return PD->isSDKPrivate();
return false;
}

void SDKNodeDecl::printFullyQualifiedName(llvm::raw_ostream &OS) const {
std::vector<NodePtr> Parent;
for (auto *P = getParent(); isa<SDKNodeDecl>(P); P = P->getParent())
Expand Down Expand Up @@ -440,6 +433,11 @@ SDKNodeType *SDKNodeTypeWitness::getUnderlyingType() const {
return getOnlyChild()->getAs<SDKNodeType>();
}

StringRef SDKNodeTypeWitness::getWitnessedTypeName() const {
return Ctx.buffer((llvm::Twine(getParent()->getAs<SDKNodeConformance>()->
getName()) + "." + getName()).str());
}

Optional<SDKNodeDeclType*> SDKNodeDeclType::getSuperclass() const {
if (SuperclassUsr.empty())
return None;
Expand Down
2 changes: 1 addition & 1 deletion tools/swift-api-digester/ModuleAnalyzerNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ class SDKNodeDecl: public SDKNode {
DeclKind getDeclKind() const { return DKind; }
void printFullyQualifiedName(llvm::raw_ostream &OS) const;
StringRef getFullyQualifiedName() const;
bool isSDKPrivate() const;
bool isDeprecated() const { return IsDeprecated; };
bool isProtocolRequirement() const { return IsProtocolReq; }
bool hasDeclAttribute(DeclAttrKind DAKind) const;
Expand Down Expand Up @@ -499,6 +498,7 @@ class SDKNodeConformance: public SDKNode {
class SDKNodeTypeWitness: public SDKNode {
public:
SDKNodeTypeWitness(SDKNodeInitInfo Info);
StringRef getWitnessedTypeName() const;
SDKNodeType *getUnderlyingType() const;
static bool classof(const SDKNode *N);
};
Expand Down
1 change: 1 addition & 0 deletions tools/swift-api-digester/ModuleDiagsConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static StringRef getCategoryName(uint32_t ID) {
case LocalDiagID::decl_type_change:
case LocalDiagID::func_type_escaping_changed:
case LocalDiagID::param_ownership_change:
case LocalDiagID::type_witness_change:
return "/* Type Changes */";
case LocalDiagID::raw_type_change:
return "/* RawRepresentable Changes */";
Expand Down
19 changes: 15 additions & 4 deletions tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,7 @@ void swift::ide::api::SDKNodeDeclVar::diagnose(SDKNode *Right) {
}

static bool shouldDiagnoseType(SDKNodeType *T) {
return T->isTopLevelType() &&
!cast<SDKNodeDecl>(T->getParent())->isSDKPrivate();
return T->isTopLevelType();
}

void swift::ide::api::SDKNodeType::diagnose(SDKNode *Right) {
Expand All @@ -859,7 +858,21 @@ void swift::ide::api::SDKNodeType::diagnose(SDKNode *Right) {
if (!RT || !shouldDiagnoseType(this))
return;
assert(isTopLevelType());

// Diagnose type witness changes when diagnosing ABI breakages.
if (auto *Wit = dyn_cast<SDKNodeTypeWitness>(getParent())) {
auto *Conform = Wit->getParent()->getAs<SDKNodeConformance>();
if (Ctx.checkingABI() && getPrintedName() != RT->getPrintedName()) {
Diags.diagnose(SourceLoc(), diag::type_witness_change,
Conform->getNominalTypeDecl()->getScreenInfo(),
Wit->getWitnessedTypeName(),
getPrintedName(), RT->getPrintedName());
}
return;
}

StringRef Descriptor = getTypeRoleDescription();
assert(isa<SDKNodeDecl>(getParent()));
auto LParent = cast<SDKNodeDecl>(getParent());
assert(LParent->getKind() == RT->getParent()->getAs<SDKNodeDecl>()->getKind());

Expand Down Expand Up @@ -1900,8 +1913,6 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
}

void DiagnosisEmitter::visitDecl(SDKNodeDecl *Node) {
if (Node->isSDKPrivate())
return;
std::vector<NodeAnnotation> Scratch;
for (auto Anno : Node->getAnnotations(Scratch))
handle(Node, Anno);
Expand Down