Skip to content

Commit c8fc5d5

Browse files
committed
perf(builtin,test): assert_true/assert_false/fail accept StringView msg
`fail`, `assert_true`, `assert_false`, and `@test.fail` all only ever read their `msg` argument — it's either formatted into a `Failure` error string via `"\{loc} FAILED: \{msg}"` or substituted as-is in an interpolation. No reason to require ownership. Expand the `msg` parameter of each to `StringView`. Every existing caller keeps compiling — `String` values coerce to `StringView` at argument passing, as do literal strings. Continues the view-safe expansion theme (#3459, #3460, #3461, #3462).
1 parent c73fb45 commit c8fc5d5

6 files changed

Lines changed: 17 additions & 13 deletions

File tree

builtin/assert.mbt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ pub fn[T : Eq + Show] assert_not_eq(
122122
/// ```
123123
#callsite(autofill(loc))
124124
#coverage.skip
125-
pub fn assert_true(x : Bool, msg? : String, loc~ : SourceLoc) -> Unit raise {
125+
pub fn assert_true(x : Bool, msg? : StringView, loc~ : SourceLoc) -> Unit raise {
126126
if !x {
127-
let fail_msg = match msg {
127+
let fail_msg : StringView = match msg {
128128
Some(msg) => msg
129129
None => "`\{x}` is not true"
130130
}
@@ -155,9 +155,13 @@ pub fn assert_true(x : Bool, msg? : String, loc~ : SourceLoc) -> Unit raise {
155155
/// ```
156156
#callsite(autofill(loc))
157157
#coverage.skip
158-
pub fn assert_false(x : Bool, msg? : String, loc~ : SourceLoc) -> Unit raise {
158+
pub fn assert_false(
159+
x : Bool,
160+
msg? : StringView,
161+
loc~ : SourceLoc,
162+
) -> Unit raise {
159163
if x {
160-
let fail_msg = match msg {
164+
let fail_msg : StringView = match msg {
161165
Some(msg) => msg
162166
None => "`\{x}` is not false"
163167
}

builtin/failure.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ pub(all) suberror Failure {
5454
/// Throws an error of type `Failure` with a message that includes both the
5555
/// source location and the provided error message.
5656
#callsite(autofill(loc))
57-
pub fn[T] fail(msg : String, loc~ : SourceLoc) -> T raise Failure {
57+
pub fn[T] fail(msg : StringView, loc~ : SourceLoc) -> T raise Failure {
5858
raise Failure("\{loc} FAILED: \{msg}")
5959
}

builtin/pkg.generated.mbti

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ pub fn[T] abort(String) -> T
99
pub fn[T : Eq + Show] assert_eq(T, T, msg? : String, loc~ : SourceLoc) -> Unit raise
1010

1111
#callsite(autofill(loc))
12-
pub fn assert_false(Bool, msg? : String, loc~ : SourceLoc) -> Unit raise
12+
pub fn assert_false(Bool, msg? : StringView, loc~ : SourceLoc) -> Unit raise
1313

1414
#deprecated
1515
#callsite(autofill(loc))
1616
pub fn[T : Eq + Show] assert_not_eq(T, T, msg? : String, loc~ : SourceLoc) -> Unit raise
1717

1818
#callsite(autofill(loc))
19-
pub fn assert_true(Bool, msg? : String, loc~ : SourceLoc) -> Unit raise
19+
pub fn assert_true(Bool, msg? : StringView, loc~ : SourceLoc) -> Unit raise
2020

2121
pub fn debug_assert(() -> Bool) -> Unit
2222

2323
#callsite(autofill(loc))
24-
pub fn[T] fail(String, loc~ : SourceLoc) -> T raise Failure
24+
pub fn[T] fail(StringView, loc~ : SourceLoc) -> T raise Failure
2525

2626
pub fn[T] ignore(T) -> Unit
2727

prelude/pkg.generated.mbti

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ pub fn[T] abort(String) -> T
1818
pub fn[T : @builtin.Eq + @builtin.Show] assert_eq(T, T, msg? : String, loc~ : @builtin.SourceLoc) -> Unit raise
1919

2020
#callsite(autofill(loc))
21-
pub fn assert_false(Bool, msg? : String, loc~ : @builtin.SourceLoc) -> Unit raise
21+
pub fn assert_false(Bool, msg? : StringView, loc~ : @builtin.SourceLoc) -> Unit raise
2222

2323
#callsite(autofill(loc))
2424
pub fn[T : @builtin.Eq + @builtin.Show] assert_not_eq(T, T, msg? : String, loc~ : @builtin.SourceLoc) -> Unit raise
2525

2626
#callsite(autofill(loc))
27-
pub fn assert_true(Bool, msg? : String, loc~ : @builtin.SourceLoc) -> Unit raise
27+
pub fn assert_true(Bool, msg? : StringView, loc~ : @builtin.SourceLoc) -> Unit raise
2828

2929
pub fn[T : @debug.Debug] debug(T) -> Unit
3030

@@ -38,7 +38,7 @@ pub fn debug_inspect(&@debug.Debug, content? : String, loc~ : @builtin.SourceLoc
3838
pub fn[T : @debug.Debug] dump(T, name? : String, loc~ : @builtin.SourceLoc) -> T
3939

4040
#callsite(autofill(loc))
41-
pub fn[T] fail(String, loc~ : @builtin.SourceLoc) -> T raise @builtin.Failure
41+
pub fn[T] fail(StringView, loc~ : @builtin.SourceLoc) -> T raise @builtin.Failure
4242

4343
pub fn[T] ignore(T) -> Unit
4444

test/pkg.generated.mbti

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn[T : @debug.Debug] assert_not_same_object(T, T, loc~ : SourceLoc) -> Unit
1919
pub fn[T : @debug.Debug] assert_same_object(T, T, loc~ : SourceLoc) -> Unit raise
2020

2121
#callsite(autofill(loc))
22-
pub fn[T] fail(String, loc~ : SourceLoc) -> T raise
22+
pub fn[T] fail(StringView, loc~ : SourceLoc) -> T raise
2323

2424
#alias(is_not, deprecated)
2525
#deprecated

test/test.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub fn[T : Debug] assert_not_same_object(
154154
///
155155
/// Produces an error message similar to @builtin.fail.
156156
#callsite(autofill(loc))
157-
pub fn[T] fail(msg : String, loc~ : SourceLoc) -> T raise {
157+
pub fn[T] fail(msg : StringView, loc~ : SourceLoc) -> T raise {
158158
@builtin.fail(msg, loc~)
159159
}
160160

0 commit comments

Comments
 (0)