Skip to content

Commit

Permalink
Fix bug in MAYBE TRUE/FALSE on void, allow voids to ENSURE
Browse files Browse the repository at this point in the history
There is no real perceivable harm in letting voids pass tests if
passed to ENSURE, if the tests allow for it.

Also fixes a usage bug of ENSURE in LOAD.
  • Loading branch information
hostilefork committed Feb 16, 2017
1 parent f97dfca commit 604b727
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
7 changes: 2 additions & 5 deletions src/core/n-data.c
Expand Up @@ -207,7 +207,7 @@ REBNATIVE(maybe)
REBVAL *value = ARG(value);

if (IS_LOGIC(test)) {
if (!IS_VOID(test) && VAL_LOGIC(test) == IS_CONDITIONAL_TRUE(value))
if (!IS_VOID(value) && VAL_LOGIC(test) == IS_CONDITIONAL_TRUE(value))
goto type_matched;
return REF(q) ? R_FALSE : R_BLANK;
}
Expand Down Expand Up @@ -251,10 +251,7 @@ REBNATIVE(maybe)
// why /? (and its specialization MAYBE?) exist, but to help avoid
// likely mistakes this returns a void.
//
// Note that in the case of a void passing the test and needing to go
// through (e.g. `maybe :void? ()`) will be void also.
//
if (IS_VOID(value) || IS_CONDITIONAL_FALSE(value))
if (IS_CONDITIONAL_FALSE(value))
return R_VOID;

return R_OUT;
Expand Down
27 changes: 21 additions & 6 deletions src/mezz/base-funcs.r
Expand Up @@ -1023,12 +1023,27 @@ ensure: function [
{Pass through a value only if it matches types (or TRUE?/FALSE? state)}
return: [<opt> any-value!]
test [function! datatype! typeset! block! logic!]
arg [any-value!] ;-- should <opt> be allowed?
arg [<opt> any-value!]
][
unless maybe? test :arg [
fail/where [
"ENSURE expected arg to match" (test)
] 'arg
case [
void? temp: maybe test :arg [
assert [any [void? :arg | false? :arg]]

; The test passed but we want to avoid an accidental usage like
; `if ensure [logic!] some-false-thing [...]` where the test
; passed but the passthru gets used conditionally. So FALSE?
; things are converted to void.
;
()
]
blank? :temp [
fail/where [
"ENSURE expected arg to match" (test)
] 'arg
]
true [
assert [all [true? :temp | :arg = :temp]]
:temp
]
]
:arg
]
2 changes: 1 addition & 1 deletion src/mezz/sys-base.r
Expand Up @@ -148,7 +148,7 @@ do*: function [
;
; !!! Should the header always be locked by LOAD?
;
hdr: lock ensure [object! blank!] first code
hdr: lock to-value ensure [object! blank!] first code
is-module: 'module = select hdr 'type
code: next code

Expand Down

0 comments on commit 604b727

Please sign in to comment.