Skip to content

Commit ebe38dd

Browse files
committed
APIDigester: Stringify inverses in GenericSigs
The presence of Copyable/Escapable conformances doesn't affect ABI. Only their absence in terms of suppressed requirements like `~Copyable` need to be output for diffing by the APIDigester. (cherry picked from commit 3b02f30)
1 parent 624fece commit ebe38dd

File tree

9 files changed

+36
-8
lines changed

9 files changed

+36
-8
lines changed

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static PrintOptions getTypePrintOpts(CheckerOptions CheckerOpts) {
1717
PrintOptions Opts;
1818
Opts.SynthesizeSugarOnTypes = true;
1919
Opts.UseOriginallyDefinedInModuleNames = true;
20+
Opts.PrintInverseRequirements = true; // Only inverses are relevant for ABI stability
2021
if (!CheckerOpts.Migrator) {
2122
// We should always print fully qualified type names for checking either
2223
// API or ABI stability.
@@ -1222,8 +1223,15 @@ Requirement getCanonicalRequirement(Requirement &Req) {
12221223
}
12231224
}
12241225

1226+
// Get an inverse requirement with the subject type canonicalized.
1227+
InverseRequirement getCanonicalInverseRequirement(InverseRequirement &Req) {
1228+
return {Req.subject->getCanonicalType(), Req.protocol, Req.loc};
1229+
}
1230+
12251231
static
1226-
StringRef printGenericSignature(SDKContext &Ctx, ArrayRef<Requirement> AllReqs,
1232+
StringRef printGenericSignature(SDKContext &Ctx,
1233+
ArrayRef<Requirement> AllReqs,
1234+
ArrayRef<InverseRequirement> Inverses,
12271235
bool Canonical) {
12281236
llvm::SmallString<32> Result;
12291237
llvm::raw_svector_ostream OS(Result);
@@ -1243,6 +1251,17 @@ StringRef printGenericSignature(SDKContext &Ctx, ArrayRef<Requirement> AllReqs,
12431251
else
12441252
Req.print(OS, Opts);
12451253
}
1254+
for (auto Inv: Inverses) {
1255+
if (!First) {
1256+
OS << ", ";
1257+
} else {
1258+
First = false;
1259+
}
1260+
if (Canonical)
1261+
getCanonicalInverseRequirement(Inv).print(OS, Opts);
1262+
else
1263+
Inv.print(OS, Opts);
1264+
}
12461265
OS << ">";
12471266
return Ctx.buffer(OS.str());
12481267
}
@@ -1251,8 +1270,10 @@ static StringRef printGenericSignature(SDKContext &Ctx, Decl *D, bool Canonical)
12511270
llvm::SmallString<32> Result;
12521271
llvm::raw_svector_ostream OS(Result);
12531272
if (auto *PD = dyn_cast<ProtocolDecl>(D)) {
1254-
return printGenericSignature(Ctx, PD->getRequirementSignature().getRequirements(),
1255-
Canonical);
1273+
SmallVector<Requirement, 2> reqs;
1274+
SmallVector<InverseRequirement, 2> inverses;
1275+
PD->getRequirementSignature().getRequirementsWithInverses(PD, reqs, inverses);
1276+
return printGenericSignature(Ctx, reqs, inverses, Canonical);
12561277
}
12571278
PrintOptions Opts = getTypePrintOpts(Ctx.getOpts());
12581279
if (auto *GC = D->getAsGenericContext()) {
@@ -1269,7 +1290,7 @@ static StringRef printGenericSignature(SDKContext &Ctx, Decl *D, bool Canonical)
12691290

12701291
static
12711292
StringRef printGenericSignature(SDKContext &Ctx, ProtocolConformance *Conf, bool Canonical) {
1272-
return printGenericSignature(Ctx, Conf->getConditionalRequirements(), Canonical);
1293+
return printGenericSignature(Ctx, Conf->getConditionalRequirements(), {}, Canonical);
12731294
}
12741295

12751296
static std::optional<uint8_t>

test/api-digester/Inputs/cake_baseline/cake.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,5 @@ open class AddingNewDesignatedInit {
232232
print(foo)
233233
}
234234
}
235+
236+
public func addingAnInverse<T>(_ t: borrowing T) {}

test/api-digester/Inputs/cake_current/cake.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,5 @@ public extension Float {
252252
}
253253

254254
infix operator <==> : AssignmentPrecedence
255+
256+
public func addingAnInverse<T: ~Copyable>(_ t: borrowing T) {}

test/api-digester/Outputs/Cake-abi.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ cake: Func P1.P1Constraint() has mangled name changing from '(extension in cake)
88
cake: Func S1.foo3() has mangled name changing from 'cake.S1.foo3() -> ()' to 'static cake.S1.foo3() -> ()'
99
cake: Func S1.foo5(x:y:) has mangled name changing from 'cake.S1.foo5(x: Swift.Int, y: Swift.Int) -> ()' to 'cake.S1.foo5(x: Swift.Int, y: Swift.Int, z: Swift.Int) -> ()'
1010
cake: Func Somestruct2.foo1(_:) has mangled name changing from 'static cake.Somestruct2.foo1(cake.C3) -> ()' to 'static cake.NSSomestruct2.foo1(cake.C1) -> ()'
11+
cake: Func addingAnInverse(_:) has generic signature change from <T> to <T where T : ~Copyable>
12+
cake: Func addingAnInverse(_:) has mangled name changing from 'cake.addingAnInverse<A>(A) -> ()' to 'cake.addingAnInverse<A where A: ~Swift.Copyable>(A) -> ()'
1113
cake: Func ownershipChange(_:_:) has mangled name changing from 'cake.ownershipChange(inout Swift.Int, __shared Swift.Int) -> ()' to 'cake.ownershipChange(Swift.Int, __owned Swift.Int) -> ()'
1214
cake: Func returnFunctionTypeOwnershipChange() has mangled name changing from 'cake.returnFunctionTypeOwnershipChange() -> (cake.C1) -> ()' to 'cake.returnFunctionTypeOwnershipChange() -> (__owned cake.C1) -> ()'
1315
cake: Protocol P3 has generic signature change from <Self : cake.P1, Self : cake.P2> to <Self : cake.P1, Self : cake.P4>

test/api-digester/Outputs/Cake.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
/* Generic Signature Changes */
33
cake: Func P1.P1Constraint() has generic signature change from <Self where Self : cake.P1, Self : cake.P2> to <Self where Self : cake.P1>
4+
cake: Func addingAnInverse(_:) has generic signature change from <T> to <T where T : ~Copyable>
45
cake: Protocol P3 has generic signature change from <Self : cake.P1, Self : cake.P2> to <Self : cake.P1, Self : cake.P4>
56

67
/* RawRepresentable Changes */

test/api-digester/compare-dump-abi-parsable-interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// RUN: %empty-directory(%t.module-cache)
77

88
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path %t.mod1/cake.swiftinterface %S/Inputs/cake_baseline/cake.swift -I %S/Inputs/APINotesLeft %clang-importer-sdk-nosource -parse-as-library -enable-library-evolution -disable-objc-attr-requires-foundation-module -module-cache-path %t.module-cache
9-
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path %t.mod2/cake.swiftinterface %S/Inputs/cake_current/cake.swift -I %S/Inputs/APINotesRight %clang-importer-sdk-nosource -parse-as-library -enable-library-evolution -disable-objc-attr-requires-foundation-module -module-cache-path %t.module-cache
9+
// RUN: %target-swift-frontend -enable-experimental-feature NoncopyableGenerics -typecheck -emit-module-interface-path %t.mod2/cake.swiftinterface %S/Inputs/cake_current/cake.swift -I %S/Inputs/APINotesRight %clang-importer-sdk-nosource -parse-as-library -enable-library-evolution -disable-objc-attr-requires-foundation-module -module-cache-path %t.module-cache
1010
// RUN: %api-digester -diagnose-sdk -print-module -module cake -BI %t.mod1 -BI %S/Inputs/APINotesLeft -I %t.mod2 -I %S/Inputs/APINotesRight -sdk %clang-importer-sdk-path -bsdk %clang-importer-sdk-path -module-cache-path %t.module-cache -o %t.result -abi
1111

1212
// RUN: %clang -E -P -x c %S/Outputs/Cake-abi.txt -o - | sed '/^\s*$/d' > %t.expected

test/api-digester/compare-dump-abi.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// RUN: %empty-directory(%t.baseline/ABI)
88

99
// RUN: %target-swift-frontend -disable-objc-attr-requires-foundation-module -emit-module -o %t.mod1/cake.swiftmodule %S/Inputs/cake_baseline/cake.swift -parse-as-library -enable-library-evolution -I %S/Inputs/APINotesLeft %clang-importer-sdk-nosource -emit-module-source-info -emit-module-source-info-path %t.mod1/cake.swiftsourceinfo 2> %t.compiler-diags
10-
// RUN: %target-swift-frontend -disable-objc-attr-requires-foundation-module -emit-module -o %t.mod2/cake.swiftmodule %S/Inputs/cake_current/cake.swift -parse-as-library -enable-library-evolution -I %S/Inputs/APINotesRight %clang-importer-sdk-nosource -emit-module-source-info -emit-module-source-info-path %t.mod2/cake.swiftsourceinfo
10+
// RUN: %target-swift-frontend -enable-experimental-feature NoncopyableGenerics -disable-objc-attr-requires-foundation-module -emit-module -o %t.mod2/cake.swiftmodule %S/Inputs/cake_current/cake.swift -parse-as-library -enable-library-evolution -I %S/Inputs/APINotesRight %clang-importer-sdk-nosource -emit-module-source-info -emit-module-source-info-path %t.mod2/cake.swiftsourceinfo
1111
// RUN: %api-digester -dump-sdk -module cake -output-dir %t.baseline -module-cache-path %t.module-cache %clang-importer-sdk-nosource -I %t.mod1 -I %S/Inputs/APINotesLeft -abi
1212
// RUN: %api-digester -diagnose-sdk -print-module -baseline-dir %t.baseline -module cake -I %t.mod2 -I %S/Inputs/APINotesLeft -module-cache-path %t.module-cache %clang-importer-sdk-nosource -abi -o %t.result
1313
// RUN: %clang -E -P -w -x c %S/Outputs/Cake-abi.txt -o - | sed '/^\s*$/d' > %t.abi.expected

test/api-digester/compare-dump-parsable-interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// RUN: %empty-directory(%t.module-cache)
77

88
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path %t.mod1/cake.swiftinterface %S/Inputs/cake_baseline/cake.swift -I %S/Inputs/APINotesLeft %clang-importer-sdk-nosource -parse-as-library -enable-library-evolution -disable-objc-attr-requires-foundation-module -module-cache-path %t.module-cache
9-
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path %t.mod2/cake.swiftinterface %S/Inputs/cake_current/cake.swift -I %S/Inputs/APINotesRight %clang-importer-sdk-nosource -parse-as-library -enable-library-evolution -disable-objc-attr-requires-foundation-module -module-cache-path %t.module-cache
9+
// RUN: %target-swift-frontend -enable-experimental-feature NoncopyableGenerics -typecheck -emit-module-interface-path %t.mod2/cake.swiftinterface %S/Inputs/cake_current/cake.swift -I %S/Inputs/APINotesRight %clang-importer-sdk-nosource -parse-as-library -enable-library-evolution -disable-objc-attr-requires-foundation-module -module-cache-path %t.module-cache
1010
// RUN: %api-digester -diagnose-sdk -print-module -module cake -BI %t.mod1 -BI %S/Inputs/APINotesLeft -I %t.mod2 -I %S/Inputs/APINotesRight -sdk %clang-importer-sdk-path -bsdk %clang-importer-sdk-path -module-cache-path %t.module-cache -o %t.result
1111

1212
// RUN: %clang -E -P -x c %S/Outputs/Cake.txt -o - | sed '/^\s*$/d' > %t.expected

test/api-digester/compare-dump.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// RUN: %empty-directory(%t.sdk)
66
// RUN: %empty-directory(%t.module-cache)
77
// RUN: %target-swift-frontend -disable-objc-attr-requires-foundation-module -emit-module -o %t.mod1/cake.swiftmodule %S/Inputs/cake_baseline/cake.swift -parse-as-library -enable-library-evolution -I %S/Inputs/APINotesLeft %clang-importer-sdk-nosource -module-name cake
8-
// RUN: %target-swift-frontend -disable-objc-attr-requires-foundation-module -emit-module -o %t.mod2/cake.swiftmodule %S/Inputs/cake_current/cake.swift -parse-as-library -enable-library-evolution -I %S/Inputs/APINotesRight %clang-importer-sdk-nosource -module-name cake
8+
// RUN: %target-swift-frontend -enable-experimental-feature NoncopyableGenerics -disable-objc-attr-requires-foundation-module -emit-module -o %t.mod2/cake.swiftmodule %S/Inputs/cake_current/cake.swift -parse-as-library -enable-library-evolution -I %S/Inputs/APINotesRight %clang-importer-sdk-nosource -module-name cake
99
// RUN: %api-digester -dump-sdk -module cake -o %t.dump1.json -module-cache-path %t.module-cache %clang-importer-sdk-nosource -I %t.mod1 -I %S/Inputs/APINotesLeft
1010
// RUN: %api-digester -dump-sdk -module cake -o %t.dump2.json -module-cache-path %t.module-cache %clang-importer-sdk-nosource -I %t.mod2 -I %S/Inputs/APINotesRight
1111
// RUN: %api-digester -diagnose-sdk -print-module --input-paths %t.dump1.json -input-paths %t.dump2.json -o %t.result

0 commit comments

Comments
 (0)