Skip to content

Commit f2f50d6

Browse files
author
Maxim Moiseev
committed
precondition => require
1 parent 9d0c912 commit f2f50d6

19 files changed

+73
-73
lines changed

Diff for: stdlib/private/StdlibUnittestFoundationExtras/StdlibUnittestFoundationExtras.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public func withOverriddenNSLocaleCurrentLocale<Result>(
2828
) -> Result {
2929
let oldMethod = class_getClassMethod(
3030
NSLocale.self, Selector("currentLocale"))
31-
precondition(oldMethod != nil, "could not find +[NSLocale currentLocale]")
31+
require(oldMethod != nil, "could not find +[NSLocale currentLocale]")
3232

3333
let newMethod = class_getClassMethod(
3434
NSLocale.self, Selector("_swiftUnittest_currentLocale"))
35-
precondition(newMethod != nil, "could not find +[NSLocale _swiftUnittest_currentLocale]")
35+
require(newMethod != nil, "could not find +[NSLocale _swiftUnittest_currentLocale]")
3636

37-
precondition(_temporaryNSLocaleCurrentLocale == nil,
37+
require(_temporaryNSLocaleCurrentLocale == nil,
3838
"nested calls to withOverriddenNSLocaleCurrentLocale are not supported")
3939

4040
_temporaryNSLocaleCurrentLocale = temporaryLocale
@@ -50,7 +50,7 @@ public func withOverriddenNSLocaleCurrentLocale<Result>(
5050
temporaryLocaleIdentifier: String,
5151
@noescape _ body: () -> Result
5252
) -> Result {
53-
precondition(
53+
require(
5454
NSLocale.availableLocaleIdentifiers().contains(temporaryLocaleIdentifier),
5555
"requested locale \(temporaryLocaleIdentifier) is not available")
5656

Diff for: stdlib/private/SwiftPrivateDarwinExtras/Subprocess.swift

+18-18
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func posixPipe() -> (readFD: CInt, writeFD: CInt) {
5454
(fds) in
5555
let ptr = fds.baseAddress
5656
if pipe(ptr) != 0 {
57-
preconditionFailure("pipe() failed")
57+
requirementFailure("pipe() failed")
5858
}
5959
}
6060
return (fds[0], fds[1])
@@ -66,46 +66,46 @@ public func spawnChild(args: [String])
6666
-> (pid: pid_t, stdinFD: CInt, stdoutFD: CInt, stderrFD: CInt) {
6767
var fileActions: posix_spawn_file_actions_t = nil
6868
if swift_posix_spawn_file_actions_init(&fileActions) != 0 {
69-
preconditionFailure("swift_posix_spawn_file_actions_init() failed")
69+
requirementFailure("swift_posix_spawn_file_actions_init() failed")
7070
}
7171

7272
let childStdin = posixPipe()
7373
// Close the write end of the pipe on the child side.
7474
if swift_posix_spawn_file_actions_addclose(
7575
&fileActions, childStdin.writeFD) != 0 {
76-
preconditionFailure("swift_posix_spawn_file_actions_addclose() failed")
76+
requirementFailure("swift_posix_spawn_file_actions_addclose() failed")
7777
}
7878

7979
// Remap child's stdin.
8080
if swift_posix_spawn_file_actions_adddup2(
8181
&fileActions, childStdin.readFD, STDIN_FILENO) != 0 {
82-
preconditionFailure("swift_posix_spawn_file_actions_adddup2() failed")
82+
requirementFailure("swift_posix_spawn_file_actions_adddup2() failed")
8383
}
8484

8585
let childStdout = posixPipe()
8686
// Close the read end of the pipe on the child side.
8787
if swift_posix_spawn_file_actions_addclose(
8888
&fileActions, childStdout.readFD) != 0 {
89-
preconditionFailure("swift_posix_spawn_file_actions_addclose() failed")
89+
requirementFailure("swift_posix_spawn_file_actions_addclose() failed")
9090
}
9191

9292
// Remap child's stdout.
9393
if swift_posix_spawn_file_actions_adddup2(
9494
&fileActions, childStdout.writeFD, STDOUT_FILENO) != 0 {
95-
preconditionFailure("swift_posix_spawn_file_actions_adddup2() failed")
95+
requirementFailure("swift_posix_spawn_file_actions_adddup2() failed")
9696
}
9797

9898
let childStderr = posixPipe()
9999
// Close the read end of the pipe on the child side.
100100
if swift_posix_spawn_file_actions_addclose(
101101
&fileActions, childStderr.readFD) != 0 {
102-
preconditionFailure("swift_posix_spawn_file_actions_addclose() failed")
102+
requirementFailure("swift_posix_spawn_file_actions_addclose() failed")
103103
}
104104

105105
// Remap child's stderr.
106106
if swift_posix_spawn_file_actions_adddup2(
107107
&fileActions, childStderr.writeFD, STDERR_FILENO) != 0 {
108-
preconditionFailure("swift_posix_spawn_file_actions_adddup2() failed")
108+
requirementFailure("swift_posix_spawn_file_actions_adddup2() failed")
109109
}
110110

111111
var pid: pid_t = -1
@@ -115,26 +115,26 @@ public func spawnChild(args: [String])
115115
}
116116
if spawnResult != 0 {
117117
print(String.fromCString(strerror(spawnResult)))
118-
preconditionFailure("swift_posix_spawn() failed")
118+
requirementFailure("swift_posix_spawn() failed")
119119
}
120120

121121
if swift_posix_spawn_file_actions_destroy(&fileActions) != 0 {
122-
preconditionFailure("swift_posix_spawn_file_actions_destroy() failed")
122+
requirementFailure("swift_posix_spawn_file_actions_destroy() failed")
123123
}
124124

125125
// Close the read end of the pipe on the parent side.
126126
if close(childStdin.readFD) != 0 {
127-
preconditionFailure("close() failed")
127+
requirementFailure("close() failed")
128128
}
129129

130130
// Close the write end of the pipe on the parent side.
131131
if close(childStdout.writeFD) != 0 {
132-
preconditionFailure("close() failed")
132+
requirementFailure("close() failed")
133133
}
134134

135135
// Close the write end of the pipe on the parent side.
136136
if close(childStderr.writeFD) != 0 {
137-
preconditionFailure("close() failed")
137+
requirementFailure("close() failed")
138138
}
139139

140140
return (pid, childStdin.writeFD, childStdout.readFD, childStderr.readFD)
@@ -156,7 +156,7 @@ internal func _readAll(fd: CInt) -> String {
156156
if readResult == 0 {
157157
break
158158
}
159-
preconditionFailure("read() failed")
159+
requirementFailure("read() failed")
160160
}
161161
return String._fromCodeUnitSequenceWithRepair(
162162
UTF8.self, input: buffer[0..<usedBytes]).0
@@ -193,15 +193,15 @@ public enum ProcessTerminationStatus : CustomStringConvertible {
193193
public func posixWaitpid(pid: pid_t) -> ProcessTerminationStatus {
194194
var status: CInt = 0
195195
if waitpid(pid, &status, 0) < 0 {
196-
preconditionFailure("waitpid() failed")
196+
requirementFailure("waitpid() failed")
197197
}
198198
if (WIFEXITED(status)) {
199199
return .Exit(Int(WEXITSTATUS(status)))
200200
}
201201
if (WIFSIGNALED(status)) {
202202
return .Signal(Int(WTERMSIG(status)))
203203
}
204-
preconditionFailure("did not understand what happened to child process")
204+
requirementFailure("did not understand what happened to child process")
205205
}
206206

207207
public func runChild(args: [String])
@@ -215,10 +215,10 @@ public func runChild(args: [String])
215215
let stderr = _readAll(stderrFD)
216216

217217
if close(stdoutFD) != 0 {
218-
preconditionFailure("close() failed")
218+
requirementFailure("close() failed")
219219
}
220220
if close(stderrFD) != 0 {
221-
preconditionFailure("close() failed")
221+
requirementFailure("close() failed")
222222
}
223223
let status = posixWaitpid(pid)
224224
return (stdout, stderr, status)

Diff for: stdlib/public/core/Assert.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
///
1515
/// Use this function for internal sanity checks that are active
1616
/// during testing but do not impact performance of shipping code.
17-
/// To check for invalid usage in Release builds; see `precondition`.
17+
/// To check for invalid usage in Release builds; see `require`.
1818
///
1919
/// * In playgrounds and -Onone builds (the default for Xcode's Debug
2020
/// configuration): if `condition` evaluates to false, stop program
@@ -58,7 +58,7 @@ public func assert(
5858
/// to satisfy that assumption in -Ounchecked builds is a serious
5959
/// programming error.
6060
@_transparent
61-
public func precondition(
61+
public func require(
6262
@autoclosure condition: () -> Bool,
6363
@autoclosure _ message: () -> String = String(),
6464
file: StaticString = __FILE__, line: UInt = __LINE__
@@ -81,7 +81,7 @@ public func precondition(
8181
/// reach the call (e.g. in the `default` case of a `switch` where you
8282
/// have knowledge that one of the other cases must be satisfied). To
8383
/// protect code from invalid usage in Release builds; see
84-
/// `preconditionFailure`.
84+
/// `requirementFailure`.
8585
///
8686
/// * In playgrounds and -Onone builds (the default for Xcode's Debug
8787
/// configuration) stop program execution in a debuggable state
@@ -121,7 +121,7 @@ public func assertionFailure(
121121
/// function will never be called. Failure to satisfy that assumption
122122
/// is a serious programming error.
123123
@_transparent @noreturn
124-
public func preconditionFailure(
124+
public func requirementFailure(
125125
@autoclosure message: () -> String = String(),
126126
file: StaticString = __FILE__, line: UInt = __LINE__
127127
) {

Diff for: stdlib/public/core/ErrorType.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public func _bridgeErrorProtocolToNSError(error: ErrorProtocol) -> AnyObject
5050
/// throws an error.
5151
@_silgen_name("swift_unexpectedError")
5252
public func _unexpectedError(error: ErrorProtocol) {
53-
preconditionFailure("'try!' expression unexpectedly raised an error: \(String(reflecting: error))")
53+
requirementFailure("'try!' expression unexpectedly raised an error: \(String(reflecting: error))")
5454
}
5555

5656
/// Invoked by the compiler when code at top level throws an uncaught error.

Diff for: stdlib/public/core/ExistentialCollection.swift.gyb

+2-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public struct ${Self} : ${Traversal}Index {
459459
internal var _box: _${Traversal}IndexBoxProtocol
460460

461461
public func _distanceTo(other: ${Self}) -> ${Self}.Distance {
462-
precondition(
462+
require(
463463
self._typeID == other._typeID,
464464
"distance: base index types differ.")
465465
return self._box._distanceTo(other._box)
@@ -473,7 +473,7 @@ public struct ${Self} : ${Traversal}Index {
473473
/// identical.
474474
@warn_unused_result
475475
public func == (lhs: ${Self}, rhs: ${Self}) -> Bool {
476-
precondition(lhs._typeID == rhs._typeID, "base index types differ.")
476+
require(lhs._typeID == rhs._typeID, "base index types differ.")
477477
return lhs._box.equals(rhs._box)
478478
}
479479
% end

Diff for: stdlib/public/core/Index.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public protocol ForwardIndex : _Incrementable {
8787
///
8888
/// The range check, if performed, is equivalent to:
8989
///
90-
/// precondition(bounds.contains(index))
90+
/// require(bounds.contains(index))
9191
///
9292
/// Use this function to perform a cheap range check for QoI purposes when
9393
/// memory safety is not a concern. Do not rely on this range check for
@@ -105,10 +105,10 @@ public protocol ForwardIndex : _Incrementable {
105105
///
106106
/// The range check, if performed, is equivalent to:
107107
///
108-
/// precondition(
108+
/// require(
109109
/// bounds.contains(range.startIndex) ||
110110
/// range.startIndex == bounds.endIndex)
111-
/// precondition(
111+
/// require(
112112
/// bounds.contains(range.endIndex) ||
113113
/// range.endIndex == bounds.endIndex)
114114
///

Diff for: stdlib/public/core/Mirror.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ extension Mirror.DisplayStyle {
452452
case .IndexContainer: self = .Collection
453453
case .KeyContainer: self = .Dictionary
454454
case .MembershipContainer: self = .Set
455-
case .Container: preconditionFailure("unused!")
455+
case .Container: requirementFailure("unused!")
456456
case .Optional: self = .Optional
457457
case .ObjCObject: self = .Class
458458
}

Diff for: stdlib/public/core/Sequence.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public protocol IteratorProtocol {
3333
/// since the copy was made, and no preceding call to `self.next()`
3434
/// has returned `nil`. Specific implementations of this protocol
3535
/// are encouraged to respond to violations of this requirement by
36-
/// calling `preconditionFailure("...")`.
36+
/// calling `requirementFailure("...")`.
3737
@warn_unused_result
3838
mutating func next() -> Element?
3939
}

Diff for: test/1_stdlib/BridgeStorage.swift.gyb

+6-6
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,24 @@ struct BridgeObject<NativeType : AnyObject, ObjCType : AnyObject>
8787
}
8888

8989
var nativeInstance: Native {
90-
precondition(isNative)
90+
require(isNative)
9191
return Builtin.bridgeFromRawPointer(rawObject)
9292
}
9393

9494
var nativeInstance_noSpareBits: Native {
95-
precondition(isNative)
96-
precondition(spareBits == 0)
95+
require(isNative)
96+
require(spareBits == 0)
9797
return Builtin.bridgeFromRawPointer(rawObject)
9898
}
9999

100100
mutating func isUniquelyReferenced_native_noSpareBits() -> Bool {
101-
precondition(isNative)
102-
precondition(spareBits == 0)
101+
require(isNative)
102+
require(spareBits == 0)
103103
return _isUnique_native(&object)
104104
}
105105

106106
var objCInstance: ObjC {
107-
precondition(isObjC)
107+
require(isObjC)
108108
return Builtin.bridgeFromRawPointer(rawObject)
109109
}
110110

Diff for: test/1_stdlib/ManagedBuffer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ final class TestManagedBuffer<T> : ManagedBuffer<CountAndCapacity,T> {
104104

105105
func append(x: T) {
106106
let count = self.count
107-
precondition(count + 2 <= capacity)
107+
require(count + 2 <= capacity)
108108

109109
withUnsafeMutablePointerToElements {
110110
(p: UnsafeMutablePointer<T>)->() in

Diff for: test/Constraints/diagnostics.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func rdar21080030() {
140140
}
141141

142142
// <rdar://problem/21248136> QoI: problem with return type inference mis-diagnosed as invalid arguments
143-
func r21248136<T>() -> T { preconditionFailure() } // expected-note 2 {{in call to function 'r21248136'}}
143+
func r21248136<T>() -> T { requirementFailure() } // expected-note 2 {{in call to function 'r21248136'}}
144144

145145
r21248136() // expected-error {{generic parameter 'T' could not be inferred}}
146146
let _ = r21248136() // expected-error {{generic parameter 'T' could not be inferred}}

Diff for: test/Constraints/function.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ testArgumentShuffle(third: 1, 2)
4242

4343
func rejectsAssertStringLiteral() {
4444
assert("foo") // expected-error {{cannot convert value of type 'String' to expected argument type 'Bool'}}
45-
precondition("foo") // expected-error {{cannot convert value of type 'String' to expected argument type 'Bool'}}
45+
require("foo") // expected-error {{cannot convert value of type 'String' to expected argument type 'Bool'}}
4646
}
4747

4848

0 commit comments

Comments
 (0)