Skip to content

Commit

Permalink
Provide better error message for my %h = Callable
Browse files Browse the repository at this point in the history
If it is the *only* value stored in a Map, and it is a Callable,
provide an error message that points to the use of ; vs , in the
contents of the Callable ({ :a; :b } versus { :a, :b }).

Addresses #5170

This change may actually be a performance improvement, as it
reduces the binary size of the STORE methods involved, making
it more likely they can be inlined.
  • Loading branch information
lizmat committed Feb 23, 2023
1 parent 70a1620 commit 4695cbe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
9 changes: 1 addition & 8 deletions src/core.c/Hash.pm6
Expand Up @@ -88,14 +88,7 @@ my class Hash { # declared in BOOTSTRAP
$x.PUSH_FROM_MAP($temp),
nqp::if(
nqp::eqaddr(($y := $iter.pull-one),IterationEnd),
nqp::if(
nqp::istype($x,Failure),
$x.throw,
X::Hash::Store::OddNumber.new(
found => nqp::add_i(nqp::mul_i(nqp::elems($storage),2),1),
last => $x
).throw
),
$temp.store-odd-number($x),
$temp.STORE_AT_KEY($x,$y)
)
)
Expand Down
33 changes: 17 additions & 16 deletions src/core.c/Map.pm6
Expand Up @@ -400,6 +400,21 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
!! self!STORE_MAP_FROM_MAP(map)
}

method store-odd-number($x) is implementation-detail {
my int $elems = self.elems;
nqp::istype($x,Failure)
?? $x.throw
!! $elems || nqp::not_i(nqp::istype($x,Callable))
?? X::Hash::Store::OddNumber.new(
found => 2 * $elems + 1,
last => $x
).throw
!! die qq:to/ERROR/;
Cannot use a Callable as the only argument to store in a {self.^name}.
Did you mean to store a Hash but used ';' instead of ',' to separate values?
ERROR
}

# Store the contents of an iterator into the Map
method !STORE_MAP_FROM_ITERATOR_DECONT($iterator --> Map:D) is raw {
nqp::until(
Expand All @@ -416,14 +431,7 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
self!STORE_MAP_DECONT($x),
nqp::if(
nqp::eqaddr((my Mu $y := $iterator.pull-one),IterationEnd),
nqp::if(
nqp::istype($x,Failure),
$x.throw,
X::Hash::Store::OddNumber.new(
found => self.elems * 2 + 1,
last => $x
).throw
),
self.store-odd-number($x),
nqp::bindkey($!storage,$x.Str,nqp::decont($y))
)
)
Expand All @@ -446,14 +454,7 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
self!STORE_MAP($x),
nqp::if(
nqp::eqaddr((my Mu $y := $iterator.pull-one),IterationEnd),
nqp::if(
nqp::istype($x,Failure),
$x.throw,
X::Hash::Store::OddNumber.new(
found => self.elems * 2 + 1,
last => $x
).throw
),
self.store-odd-number($x),
nqp::bindkey($!storage,$x.Str,$y)
)
)
Expand Down

0 comments on commit 4695cbe

Please sign in to comment.