Skip to content

Commit 0afafd1

Browse files
committed
swift-module-digester: diagnose let to var changes and vice versa as ABI breaking.
1 parent c5444a8 commit 0afafd1

File tree

9 files changed

+31
-10
lines changed

9 files changed

+31
-10
lines changed

include/swift/AST/DiagnosticsModuleDiffer.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ ERROR(nominal_type_kind_changed,none,"%0 has been changed to a %1", (StringRef,
7676

7777
ERROR(optional_req_changed,none,"%0 is %select{now|no longer}1 an optional requirement", (StringRef, bool))
7878

79+
ERROR(var_let_changed,none,"%0 changes from %select{var|let}1 to %select{let|var}1", (StringRef, bool))
80+
7981
#ifndef DIAG_NO_UNDEF
8082
# if defined(DIAG)
8183
# undef DIAG

test/api-digester/Inputs/cake1.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,6 @@ public protocol ObjCProtocol {
111111
@objc
112112
func addOptional()
113113
}
114+
115+
public let GlobalLetChangedToVar = 1
116+
public var GlobalVarChangedToLet = 1

test/api-digester/Inputs/cake2.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,6 @@ public protocol ObjCProtocol {
121121
@objc
122122
optional func addOptional()
123123
}
124+
125+
public var GlobalLetChangedToVar = 1
126+
public let GlobalVarChangedToLet = 1

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cake1: Constructor fixedLayoutStruct.init(b:a:) has been removed
1313
cake1: Func C4.foo() has been removed
1414
cake1: Func RequiementChanges.removedFunc() has been removed
1515
cake1: Subscript RemoveSetters.subscript(_:) has removed its setter
16+
cake1: Var GlobalVarChangedToLet has removed its setter
1617
cake1: Var RemoveSetters.Value has removed its setter
1718
cake1: Var RequiementChanges.removedVar has been removed
1819

@@ -42,6 +43,8 @@ cake1: Func S1.foo3() is now static
4243
cake1: Struct C6 is now with @_fixed_layout
4344
cake1: Var C1.CIIns1 changes from weak to strong
4445
cake1: Var C1.CIIns2 changes from strong to weak
46+
cake1: Var GlobalLetChangedToVar changes from let to var
47+
cake1: Var GlobalVarChangedToLet changes from var to let
4548

4649
/* Fixed-layout Type changes */
4750
cake1: EnumElement FrozenKind.Fixed in a non-resilient type changes position from 1 to 2

test/api-digester/Outputs/Cake.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cake1: Constructor fixedLayoutStruct.init(b:a:) has been removed
1212
cake1: Func C4.foo() has been removed
1313
cake1: Func RequiementChanges.removedFunc() has been removed
1414
cake1: Subscript RemoveSetters.subscript(_:) has removed its setter
15+
cake1: Var GlobalVarChangedToLet has removed its setter
1516
cake1: Var RemoveSetters.Value has removed its setter
1617
cake1: Var RequiementChanges.removedVar has been removed
1718

test/api-digester/compare-dump.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
// RUN: %api-digester -dump-sdk -module cake2 -o %t.dump2.json -module-cache-path %t.module-cache %clang-importer-sdk-nosource -I %t.mod -I %S/Inputs/APINotesRight -abi
1616
// RUN: %api-digester -diagnose-sdk -print-module --input-paths %t.dump1.json -input-paths %t.dump2.json -abi > %t.result
1717

18-
// RUN: %clang -E -P -x c %S/Outputs/Cake-abi.txt -o - | sed '/^\s*$/d' > %t.expected
19-
// RUN: %clang -E -P -x c %t.result -o - | sed '/^\s*$/d' > %t.result.tmp
20-
// RUN: diff -u %t.expected %t.result.tmp
18+
// RUN: %clang -E -P -x c %S/Outputs/Cake-abi.txt -o - | sed '/^\s*$/d' > %t.abi.expected
19+
// RUN: %clang -E -P -x c %t.result -o - | sed '/^\s*$/d' > %t.abi.result.tmp
20+
// RUN: diff -u %t.abi.expected %t.abi.result.tmp

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@ bool SDKNode::operator==(const SDKNode &Other) const {
648648
if (LV->getFixedBinaryOrder() != RV->getFixedBinaryOrder())
649649
return false;
650650
}
651+
if (LV->isLet() != RV->isLet())
652+
return false;
651653
}
652654
}
653655
}

tools/swift-api-digester/ModuleDiagsConsumer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static StringRef getCategoryName(uint32_t ID) {
4242
return "/* Renamed Decls */";
4343
case LocalDiagID::decl_attr_change:
4444
case LocalDiagID::decl_new_attr:
45+
case LocalDiagID::var_let_changed:
4546
return "/* Decl Attribute changes */";
4647
case LocalDiagID::default_arg_removed:
4748
case LocalDiagID::decl_type_change:

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -760,13 +760,19 @@ static void detectDeclChange(NodePtr L, NodePtr R, SDKContext &Ctx) {
760760
// Detect re-ordering if they're from structs with a fixed layout.
761761
auto *LV = dyn_cast<SDKNodeDeclVar>(L);
762762
auto *RV = dyn_cast<SDKNodeDeclVar>(R);
763-
if (LV && RV &&
764-
LV->hasFixedBinaryOrder() && RV->hasFixedBinaryOrder() &&
765-
LV->getFixedBinaryOrder() != RV->getFixedBinaryOrder()) {
766-
Ctx.getDiags().diagnose(SourceLoc(), diag::decl_reorder,
767-
LV->getScreenInfo(),
768-
LV->getFixedBinaryOrder(),
769-
RV->getFixedBinaryOrder());
763+
if (LV && RV) {
764+
if (LV->hasFixedBinaryOrder() && RV->hasFixedBinaryOrder() &&
765+
LV->getFixedBinaryOrder() != RV->getFixedBinaryOrder()) {
766+
Ctx.getDiags().diagnose(SourceLoc(), diag::decl_reorder,
767+
LV->getScreenInfo(),
768+
LV->getFixedBinaryOrder(),
769+
RV->getFixedBinaryOrder());
770+
}
771+
if (LV->isLet() != RV->isLet()) {
772+
Ctx.getDiags().diagnose(SourceLoc(), diag::var_let_changed,
773+
LV->getScreenInfo(),
774+
LV->isLet());
775+
}
770776
}
771777
}
772778

0 commit comments

Comments
 (0)