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

File tree

14 files changed

+69
-39
lines changed

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))

0 commit comments

Comments
 (0)