Skip to content

Commit 2242a3e

Browse files
authored
Merge pull request #61095 from eeckstein/new-assert
Define our own `assert` implementation for the swift compiler sources
2 parents d400394 + 4554939 commit 2242a3e

14 files changed

+69
-39
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@
1313
@_exported import BasicBridging
1414
import std
1515

16+
/// The assert function to be used in the compiler.
17+
///
18+
/// This overrides the standard Swift assert for two reasons:
19+
/// * We also like to check for assert failures in release builds. Although this could be
20+
/// achieved with `precondition`, it's easy to forget about it and use `assert` instead.
21+
/// * We need to see the error message in crashlogs of release builds. This is even not the
22+
/// case for `precondition`.
23+
@_transparent
24+
public func assert(_ condition: Bool, _ message: @autoclosure () -> String,
25+
file: StaticString = #fileID, line: UInt = #line) {
26+
if !condition {
27+
print("### basic")
28+
fatalError(message(), file: file, line: line)
29+
}
30+
}
31+
32+
/// The assert function (without a message) to be used in the compiler.
33+
///
34+
/// Unforuntately it's not possible to just add a default argument to `message` in the
35+
/// other `assert` function. We need to defined this overload.
36+
@_transparent
37+
public func assert(_ condition: Bool, file: StaticString = #fileID, line: UInt = #line) {
38+
if !condition {
39+
fatalError("", file: file, line: line)
40+
}
41+
}
42+
43+
1644
//===----------------------------------------------------------------------===//
1745
// StringRef
1846
//===----------------------------------------------------------------------===//

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import Basic
1413
import SIL
1514

1615
/// Removes redundant ObjectiveC <-> Swift bridging calls.

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/StackPromotion.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func tryPromoteAlloc(_ allocRef: AllocRefInstBase,
125125
var (innerRange, outerBlockRange) = computeInnerAndOuterLiferanges(instruction: allocRef, in: outerDominatingBlock, domTree: domTree, context: context)
126126
defer { innerRange.deinitialize(); outerBlockRange.deinitialize() }
127127

128-
precondition(innerRange.blockRange.isValid, "inner range should be valid because we did a dominance check")
128+
assert(innerRange.blockRange.isValid, "inner range should be valid because we did a dominance check")
129129

130130
if !outerBlockRange.isValid {
131131
// This happens if we fail to find a correct outerDominatingBlock.
@@ -282,7 +282,7 @@ private extension BasicBlockRange {
282282
}
283283

284284
for lifeBlock in inclusiveRange {
285-
precondition(otherRange.inclusiveRangeContains(lifeBlock), "range must be a subset of other range")
285+
assert(otherRange.inclusiveRangeContains(lifeBlock), "range must be a subset of other range")
286286
for succ in lifeBlock.successors {
287287
if isOnlyInOtherRange(succ) {
288288
return true

SwiftCompilerSources/Sources/Optimizer/InstructionPasses/SimplifyBeginCOWMutation.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import Basic
1413
import SIL
1514

1615
/// Simplify begin_cow_mutation instructions.

SwiftCompilerSources/Sources/Optimizer/PassManager/ModulePassContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct ModulePassContext {
4242
var endIndex: Int { return Int(bridged.count) }
4343

4444
subscript(_ index: Int) -> VTable {
45-
precondition(index >= 0 && index < bridged.count)
45+
assert(index >= 0 && index < bridged.count)
4646
return VTable(bridged: bridged.vTables![index])
4747
}
4848
}

SwiftCompilerSources/Sources/Optimizer/TestPasses/RangeDumper.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let rangeDumper = FunctionPass(name: "dump-ranges", {
2929
if let sli = inst as? StringLiteralInst {
3030
switch sli.string {
3131
case "begin":
32-
precondition(begin == nil, "more than one begin instruction")
32+
assert(begin == nil, "more than one begin instruction")
3333
begin = sli
3434
case "end":
3535
ends.append(sli)
@@ -62,16 +62,16 @@ let rangeDumper = FunctionPass(name: "dump-ranges", {
6262
verify(instRange.blockRange, context)
6363

6464
for i in ins {
65-
precondition(instRange.contains(i))
66-
precondition(instRange.inclusiveRangeContains(i))
65+
assert(instRange.contains(i))
66+
assert(instRange.inclusiveRangeContains(i))
6767
}
6868
for e in ends {
69-
precondition(!instRange.contains(e))
70-
precondition(instRange.inclusiveRangeContains(e))
69+
assert(!instRange.contains(e))
70+
assert(instRange.inclusiveRangeContains(e))
7171
}
7272
for o in outs {
73-
precondition(!instRange.contains(o))
74-
precondition(!instRange.inclusiveRangeContains(o))
73+
assert(!instRange.contains(o))
74+
assert(!instRange.inclusiveRangeContains(o))
7575
}
7676
})
7777

@@ -89,8 +89,8 @@ private func verify(_ blockRange: BasicBlockRange, _ context: PassContext) {
8989
}
9090

9191
for b in blockRange.begin.function.blocks {
92-
precondition(blockRange.contains(b) == inRange.contains(b))
93-
precondition(blockRange.inclusiveRangeContains(b) == inInclusiveRange.contains(b))
92+
assert(blockRange.contains(b) == inRange.contains(b))
93+
assert(blockRange.inclusiveRangeContains(b) == inInclusiveRange.contains(b))
9494
}
9595
}
9696

SwiftCompilerSources/Sources/Optimizer/Utilities/EscapeInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ fileprivate struct EscapeInfoWalker<V: EscapeInfoVisitor> : ValueDefUseWalker,
303303
}
304304

305305
mutating func start(forAnalyzingAddresses: Bool) {
306-
precondition(walkDownCache.isEmpty && walkUpCache.isEmpty)
306+
assert(walkDownCache.isEmpty && walkUpCache.isEmpty)
307307
analyzeAddresses = forAnalyzingAddresses
308308
}
309309

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ extension Value {
4444
/// in a loop while this value is not in that loop, the value has to be copied for
4545
/// each loop iteration.
4646
func makeAvailable(in destBlock: BasicBlock, _ context: PassContext) -> Value {
47-
precondition(uses.isEmpty)
48-
precondition(ownership == .owned)
47+
assert(uses.isEmpty)
48+
assert(ownership == .owned)
4949

5050
let beginBlock = definingBlock
5151
var useToDefRange = BasicBlockRange(begin: beginBlock, context)

SwiftCompilerSources/Sources/SIL/BasicBlock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public struct SuccessorArray : RandomAccessCollection, FormattedLikeArray {
9797
public var endIndex: Int { return Int(succArray.numElements) }
9898

9999
public subscript(_ index: Int) -> BasicBlock {
100-
precondition(index >= 0 && index < endIndex)
100+
assert(index >= 0 && index < endIndex)
101101
let s = BridgedSuccessor(succ: succArray.data! + index &* BridgedSuccessorSize);
102102
return SILSuccessor_getTargetBlock(s).block
103103
}

SwiftCompilerSources/Sources/SIL/Operand.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public struct OperandArray : RandomAccessCollection, CustomReflectable {
5454
public var endIndex: Int { return Int(opArray.numElements) }
5555

5656
public subscript(_ index: Int) -> Operand {
57-
precondition(index >= 0 && index < endIndex)
57+
assert(index >= 0 && index < endIndex)
5858
return Operand(BridgedOperand(op: opArray.data! + index &* BridgedOperandSize))
5959
}
6060

6161
public func getIndex(of operand: Operand) -> Int {
6262
let idx = (operand.bridged.op - UnsafeRawPointer(opArray.data!)) /
6363
BridgedOperandSize
64-
precondition(self[idx].bridged.op == operand.bridged.op)
64+
assert(self[idx].bridged.op == operand.bridged.op)
6565
return idx
6666
}
6767

@@ -74,8 +74,8 @@ public struct OperandArray : RandomAccessCollection, CustomReflectable {
7474
///
7575
/// Note: this does not return a Slice. The first index of the returnd array is always 0.
7676
public subscript(bounds: Range<Int>) -> OperandArray {
77-
precondition(bounds.lowerBound >= 0)
78-
precondition(bounds.upperBound <= endIndex)
77+
assert(bounds.lowerBound >= 0)
78+
assert(bounds.upperBound <= endIndex)
7979
return OperandArray(opArray: BridgedArrayRef(
8080
data: opArray.data! + bounds.lowerBound &* BridgedOperandSize,
8181
numElements: bounds.upperBound - bounds.lowerBound))

SwiftCompilerSources/Sources/SIL/SmallProjectionPath.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -541,14 +541,14 @@ extension SmallProjectionPath {
541541
let p1 = SmallProjectionPath(.structField, index: 3)
542542
.push(.classField, index: 12345678)
543543
let (k2, i2, p2) = p1.pop()
544-
precondition(k2 == .classField && i2 == 12345678)
544+
assert(k2 == .classField && i2 == 12345678)
545545
let (k3, i3, p3) = p2.pop()
546-
precondition(k3 == .structField && i3 == 3)
547-
precondition(p3.isEmpty)
546+
assert(k3 == .structField && i3 == 3)
547+
assert(p3.isEmpty)
548548
let (k4, i4, _) = p2.push(.enumCase, index: 876).pop()
549-
precondition(k4 == .enumCase && i4 == 876)
549+
assert(k4 == .enumCase && i4 == 876)
550550
let p5 = SmallProjectionPath(.anything)
551-
precondition(p5.pop().path.isEmpty)
551+
assert(p5.pop().path.isEmpty)
552552
}
553553

554554
func parsing() {
@@ -581,9 +581,9 @@ extension SmallProjectionPath {
581581
func testParse(_ pathStr: String, expect: SmallProjectionPath) {
582582
var parser = StringParser(pathStr)
583583
let path = try! parser.parseProjectionPathFromSIL()
584-
precondition(path == expect)
584+
assert(path == expect)
585585
let str = path.description
586-
precondition(str == pathStr)
586+
assert(str == pathStr)
587587
}
588588

589589
func merging() {
@@ -609,9 +609,9 @@ extension SmallProjectionPath {
609609
let expect = try! expectParser.parseProjectionPathFromSIL()
610610

611611
let result = lhs.merge(with: rhs)
612-
precondition(result == expect)
612+
assert(result == expect)
613613
let result2 = rhs.merge(with: lhs)
614-
precondition(result2 == expect)
614+
assert(result2 == expect)
615615
}
616616

617617
func matching() {
@@ -643,7 +643,7 @@ extension SmallProjectionPath {
643643
var rhsParser = StringParser(rhsStr)
644644
let rhs = try! rhsParser.parseProjectionPathFromSIL()
645645
let result = lhs.matches(pattern: rhs)
646-
precondition(result == expect)
646+
assert(result == expect)
647647
}
648648

649649
func overlapping() {
@@ -673,9 +673,9 @@ extension SmallProjectionPath {
673673
var rhsParser = StringParser(rhsStr)
674674
let rhs = try! rhsParser.parseProjectionPathFromSIL()
675675
let result = lhs.mayOverlap(with: rhs)
676-
precondition(result == expect)
676+
assert(result == expect)
677677
let reversedResult = rhs.mayOverlap(with: lhs)
678-
precondition(reversedResult == expect)
678+
assert(reversedResult == expect)
679679
}
680680

681681
func predicates() {
@@ -696,7 +696,7 @@ extension SmallProjectionPath {
696696
var parser = StringParser(pathStr)
697697
let path = try! parser.parseProjectionPathFromSIL()
698698
let result = property(path)
699-
precondition(result == expect)
699+
assert(result == expect)
700700
}
701701

702702
func path2path() {
@@ -727,9 +727,9 @@ extension SmallProjectionPath {
727727
if let expect = expect {
728728
var expectParser = StringParser(expect)
729729
let expectPath = try! expectParser.parseProjectionPathFromSIL()
730-
precondition(result == expectPath)
730+
assert(result == expectPath)
731731
} else {
732-
precondition(result == nil)
732+
assert(result == nil)
733733
}
734734
}
735735
}

SwiftCompilerSources/Sources/SIL/Utils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
import SILBridging
1414

15+
// Need to export "Basic" to make `Basic.assert` available in the Optimizer module.
16+
// Otherwise The Optimizer would fall back to Swift's assert implementation.
17+
@_exported import Basic
18+
1519
//===----------------------------------------------------------------------===//
1620
// Lists
1721
//===----------------------------------------------------------------------===//

SwiftCompilerSources/Sources/SIL/VTable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public struct VTable : CustomStringConvertible, CustomReflectable {
3737
public var endIndex: Int { return Int(bridgedArray.numElements) }
3838

3939
public subscript(_ index: Int) -> Entry {
40-
precondition(index >= 0 && index < endIndex)
40+
assert(index >= 0 && index < endIndex)
4141
return Entry(bridged: BridgedVTableEntry(ptr: bridgedArray.data! + index &* BridgedVTableEntrySize))
4242
}
4343
}

SwiftCompilerSources/Sources/SIL/WitnessTable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public struct WitnessTable : CustomStringConvertible, CustomReflectable {
6060
public var endIndex: Int { return Int(bridged.numElements) }
6161

6262
public subscript(_ index: Int) -> Entry {
63-
precondition(index >= 0 && index < endIndex)
63+
assert(index >= 0 && index < endIndex)
6464
return Entry(bridged: BridgedWitnessTableEntry(ptr: bridged.data! + index &* BridgedWitnessTableEntrySize))
6565
}
6666
}

0 commit comments

Comments
 (0)