[perl #42300] [PATCH] t/pmc/sub.t: test for creation of lex by clone op

0 views
Skip to first unread message

Yehoshua Sapir

unread,
Apr 4, 2007, 10:26:19 AM4/4/07
to bugs-bi...@rt.perl.org
# New Ticket Created by "Yehoshua Sapir"
# Please include the string: [perl #42300]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=42300 >

clone-lex-test.patch

Matt Diephouse via RT

unread,
Apr 26, 2007, 1:43:42 AM4/26/07
to perl6-i...@perl.org, all...@perl.org
First, the test (rearranged to include only the relevant parts):

+.sub main :main
+ .local string ok, not_ok
+ ok = "ok"
+ not_ok = "not ok"
+
+ # if 'not ok' is printed, it means that the lexical environment
+ # for the first closure in each pair, (where "out" = "ok")
+ # was overwritten by the lexical environment created for the
+ # second closure (where "out" = "not ok")
+
+ $P10 = makebar_clone(ok)
+ $P20 = makebar_clone(not_ok)
+ $P10()
+.end
+
+.sub makebar_clone
+ .param pmc out
+ .lex 'out', out
+ .const .Sub s = 'bar'
+ $P0 = clone s
+ .return($P0)
+.end
+
+.sub bar :outer(makebar_clone)
+ $P0 = find_lex 'out'
+ say $P0
+.end

(This prints "not ok". The test in the patch expects "ok".)

You're arguing that the different copies of "bar" that are returned from makebar_clone
should have different lexical environments. I'm pretty sure that this is not the case. Without
using "newclosure", there's no closure so the lexical environments are the same.

What the :outer does in this case is rearrange the lexical stack so that "makebar_clone"
appears in the lexical stack for "bar". So we're using the lexical environment from the last
time that "makebar_clone" was called. It's bizarre that this even works because without the
closure, I'd think that the lexical environment would have destroyed.

I'm not sure how intentional this is. The PDD isn't clear (to me) about what :outer means in
the absence of "newclosure". I'd definitely be interested in seeing why this would be a useful
feature. More detail in the PDD would be nice.

Thanks for the interesting patch.

--
Matt Diephouse


Yehoshua Sapir

unread,
Apr 26, 2007, 1:50:22 PM4/26/07
to parrotbug...@parrotcode.org
Now it makes sense. :) Anyway, I found this by following the Compiler FAQ,
which says that a new closure should be created by cloning the sub. I think
it should be changed to say newclosure, or even explain this (because you
might really want to clone the Sub in some cases.)

Bernhard Schmalhofer via RT

unread,
Sep 8, 2007, 10:07:14 AM9/8/07
to perl6-i...@perl.org
On Do. 26. Apr. 2007, 16:45:08, yas...@gmail.com wrote:


> Now it makes sense. :) Anyway, I found this by following the Compiler FAQ,
> which says that a new closure should be created by cloning the sub. I
think
> it should be changed to say newclosure, or even explain this (because you
> might really want to clone the Sub in some cases.)

Hi,

in r21136 I have changed compiler_faq.pod, so that it now uses
'newclosure' instead of 'clone'.
More explanation could also be added to 'examples/tutorial/80_closure.pir'.

As the original issue has been clarified, can I close this ticket?

Regards,
Bernhard

--
/* Bernhard.S...@gmx.de */

James Keenan via RT

unread,
Mar 16, 2008, 12:36:17 PM3/16/08
to perl6-i...@perl.org
On Sat Sep 08 07:07:13 2007, bernhard wrote:

>
> As the original issue has been clarified, can I close this ticket?
>

Since no one has suggested otherwise since September, I'll close it for
you :-)

kid51

Reply all
Reply to author
Forward
0 new messages