Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix mysterious "Seq already iterated" errors for Seq containing Seqs
When a Seq contains another Seq, the latter often throwed errors about
having already been iterated even if the code (like List.join) really
only iterated once.

The reason is that a Seq when sunk will iterate and sink all its items.
We need to take care whenever we push values while iterating because
push returns the pused value and thus may .sink it.

Fixes for example ('ABC'.ords xx 1).join

Regressions in S05-modifier/counted.t and counted-match.t but I really
have no idea how that could happen.
  • Loading branch information
niner committed Aug 26, 2015
1 parent 0bfeaff commit c87c203
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/core/Iterator.pm
Expand Up @@ -26,7 +26,7 @@ my role Iterator {
while $i < $n {
$pulled := self.pull-one();
last if $pulled =:= IterationEnd;
$target.push($pulled);
my $ = $target.push($pulled); # we may not .sink $pulled here, since it can be a Seq
$i = $i + 1;
}
$pulled =:= IterationEnd
Expand Down
12 changes: 6 additions & 6 deletions src/core/List.pm
Expand Up @@ -107,7 +107,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
}
}
else {
$!reification-target.push(current);
my $ = $!reification-target.push(current);
}
}
nqp::elems($!reified);
Expand All @@ -130,7 +130,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
}
}
else {
$!reification-target.push(current);
my $ = $!reification-target.push(current);
}
}
$!future := Mu unless nqp::elems($!future);
Expand All @@ -148,7 +148,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
my \current = nqp::shift($!future);
nqp::istype(current, Slip) && nqp::isconcrete(current)
?? current.iterator.push-all($!reification-target)
!! $!reification-target.push(current);
!! my $ = $!reification-target.push(current);
}
$!future := Mu;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
my int $n = nqp::elems(vm-tuple);
while $i < $n {
my \consider = nqp::atpos(vm-tuple, $i);
nqp::push(future, nqp::iscont(consider)
my $ = nqp::push(future, nqp::iscont(consider)
?? consider
!! nqp::istype(consider, Iterable)
?? consider.flat.Slip
Expand All @@ -223,7 +223,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
my \iterbuffer = IterationBuffer.CREATE;
nqp::bindattr(list, List, '$!reified', iterbuffer);
for @things {
iterbuffer.push($_);
my $ = iterbuffer.push($_);
}
list
}
Expand Down Expand Up @@ -348,7 +348,7 @@ my class List does Iterable does Positional { # declared in BOOTSTRAP
!! nqp::elems($!reified);
my int $i = $!i;
while $i < $n {
$target.push(nqp::ifnull(nqp::atpos($!reified, $i), Any));
my $ = $target.push(nqp::ifnull(nqp::atpos($!reified, $i), Any));
$i = $i + 1;
}
$!i = $n;
Expand Down
10 changes: 5 additions & 5 deletions src/core/Seq.pm
Expand Up @@ -315,7 +315,7 @@ sub GATHER(&block) {
iter!start-slip-wanted($taken),
($wanted = nqp::getattr_i(iter, self, '$!wanted'))),
nqp::stmts(
nqp::getattr(iter, self, '$!push-target').push($taken),
my $ = nqp::getattr(iter, self, '$!push-target').push($taken),
($wanted = nqp::bindattr_i(iter, self, '$!wanted',
nqp::sub_i(nqp::getattr_i(iter, self, '$!wanted'), 1))))),
nqp::if(nqp::iseq_i($wanted, 0),
Expand All @@ -326,7 +326,7 @@ sub GATHER(&block) {
)
}
nqp::bindattr(iter, self, '&!resumption', {
nqp::handle(&block(), 'TAKE', $taker());
my $ = nqp::handle(&block(), 'TAKE', $taker());
nqp::continuationcontrol(0, PROMPT, -> | {
nqp::bindattr(iter, self, '&!resumption', Callable)
});
Expand Down Expand Up @@ -368,12 +368,12 @@ sub GATHER(&block) {
method !start-slip-wanted(\slip) {
my $value := self.start-slip(slip);
unless $value =:= IterationEnd {
$!push-target.push($value);
my $ = $!push-target.push($value);
my int $i = 1;
my int $n = $!wanted;
while $i < $n {
last if ($value := self.slip-one()) =:= IterationEnd;
$!push-target.push($value);
my $ = $!push-target.push($value);
$i = $i + 1;
}
$!wanted = $!wanted - $i;
Expand All @@ -386,7 +386,7 @@ sub GATHER(&block) {
my $value;
while $i < $n {
last if ($value := self.slip-one()) =:= IterationEnd;
$!push-target.push($value);
my $ = $!push-target.push($value);
$i = $i + 1;
}
$!wanted = $!wanted - $i;
Expand Down
5 changes: 4 additions & 1 deletion src/core/control.pm
Expand Up @@ -68,7 +68,10 @@ my &take := -> | {
#?if !jvm
proto sub take(|) { * }
multi sub take() { die "take without parameters doesn't make sense" }
multi sub take(\x) { THROW(nqp::const::CONTROL_TAKE,nqp::p6recont_ro(x)); x }
multi sub take(\x) {
my $ = THROW(nqp::const::CONTROL_TAKE, nqp::p6recont_ro(x));
x
}
multi sub take(|) {
my $list := RETURN-LIST(nqp::p6argvmarray());
THROW( nqp::const::CONTROL_TAKE, nqp::p6recont_ro($list) );
Expand Down

0 comments on commit c87c203

Please sign in to comment.