Skip to content

Instantly share code, notes, and snippets.

@timo
Last active July 18, 2020 23:45
Show Gist options
  • Save timo/9878a562fe9b8502e58dcf11e7f74310 to your computer and use it in GitHub Desktop.
Save timo/9878a562fe9b8502e58dcf11e7f74310 to your computer and use it in GitHub Desktop.
a Seq's first .iterator call shall be recorded with backtrace to be outputted when .iterator is called a second time ("seq already had its iterator taken and got consumed" or whatever)
making patch
will create seq
seq has which Seq|66864704
0
first user:
0.5
1
1.5
second user:
iterator already taken for this seq:
in method map at SETTING::src/core.c/Any-iterable-methods.pm6 line 40
in sub first-user at /tmp/wraptest.raku line 47
in block <unit> at /tmp/wraptest.raku line 56
--> encountered when this code tried to take the iterator another time:
in method map at SETTING::src/core.c/Any-iterable-methods.pm6 line 40
in sub second-user at /tmp/wraptest.raku line 50
in block <unit> at /tmp/wraptest.raku line 58
(btw this Seq is iterating a Rakudo::Iterator::ReifiedListIterator|66867248)
in sub second-user at /tmp/wraptest.raku line 50
in block <unit> at /tmp/wraptest.raku line 58
my $in-patch = 0;
role TappedSeq {
has $.introducer is rw;
has $.iterator-at-the-time is rw;
method iterator is hidden-from-backtrace {
$in-patch++;
#say "entering trouble reporting";
my $second-call = Backtrace.new(3).full;
my $text = $.introducer.full;
die qq:to/ERROR/;
iterator already taken for this seq:
$text.indent(4)
--> encountered when this code tried to take the iterator another time:
$second-call.indent(4)
(btw this Seq is iterating a $!iterator-at-the-time)
ERROR
LEAVE $in-patch--;
}
};
say "making patch";
Seq.^find_method("iterator").wrap(
sub wrapper( $self ) is hidden-from-backtrace {
#if $in-patch >= 2 { say "not patching " ~ $self.WHICH; LEAVE { $in-patch--; }; nextsame; say "oh my?" }
if $in-patch >= 2 { LEAVE { $in-patch--; }; nextsame; }
my $result := callwith($self);
$self does TappedSeq;
$self.introducer = Backtrace.new(7);
$self.iterator-at-the-time = $result.WHICH;
ENTER { $in-patch++; }
LEAVE { $in-patch--; }
return $result
});
say "will create seq";
my $s = (1, 2, 3).Seq;
say "seq has which " ~ $s.WHICH;
#if $me ~~ TappedSeq { note "this seq was already grabbed here:\n" ~ $me.introducer; } else { $me does TappedSeq; $me.introducer = Backtrace.new.full; callsame; LEAVE { $in-patch = 0 } } } } }); }; do-patch();
my $i1;
my $i2;
sub first-user {
.say for $s.map(* / 2)
}
sub second-user {
.say for $s.map(*+1)
}
say $in-patch;
say "first user:";
first-user();
say "second user:";
second-user();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment