Skip to content
Tcl Source Code View Ticket
Login

View Ticket

inconsisency in behaviour of tailcall from (builtin) namespace ensembles

Closed / Fixedd87cb182053fd79b3fc16054526620f2dfb25d96Created 12 years agoUpdated 12 years ago
Descriptionplain2015-03-19 01:56:51
Using [tailcall] from a proc in a namespace ensemble's -map used to work as expected on 8.6.0.  On 8.6.1 and later, something changed:


# tailcall and uplevel identity:
proc it {} {
    tailcall try {
        puts it:[::incr x]
    }
}
proc iu {} {
    uplevel 1 {
        puts iu:[::incr x]
    }
}

proc aa {} {
    it
    iu
    return $x
}

puts aa:[aa]        ;# it:1 iu:2 aa:2 - as expected


# now try in an ensemble:
namespace eval ens {
    namespace ensemble create
}
namespace ensemble configure ens -map {it ::it ut ::ut}

proc ab {} {
    it
    iu
    return $x
}

puts ab:[ab]        ;# it:1 iu:2 ab:2 - as expected


# now try with an existing ensemble:
set map [namespace ensemble configure dict -map]
dict set map it ::it
dict set map iu ::iu
namespace ensemble configure dict -map $map

proc ac {} {
    dict it
    dict iu
    return $x
}

puts ac:[ac]        ;# iu:1 it:1 ac:  - not at all as expected!
Referenced by1
anonymous Ticket [d87cb18205]: 3 changes [History diff]2015-03-19 02:03:54
pooryorickplain2015-03-19 13:31:08
Here is the example, correcting what were likely a few typos/omissions.  It illustrates the same unexpected behaviour:

# tailcall and uplevel identity:
proc it {} {
    tailcall try {
        puts it:[::incr x]
    }
}
proc iu {} {
    uplevel 1 {
        puts iu:[::incr x]
    }
}

proc aa {} {
    it
    iu
    return $x
}

puts aa:[aa]        ;# it:1 iu:2 aa:2 - as expected


# now try in an ensemble:
namespace eval ens {
    namespace ensemble create
}
namespace ensemble configure ens -map {it ::it iu ::iu}

proc ab {} {
    ens it
    ens iu
    return $x
}

puts ab:[ab]        ;# it:1 iu:2 ab:2 - as expected


# now try with an existing ensemble:
set map [namespace ensemble configure dict -map]
dict set map it ::it
dict set map iu ::iu
namespace ensemble configure dict -map $map

proc ac {} {
    dict it
    dict iu
    return $x
}

puts ac:[ac]        ;# iu:1 it:1 ac:  - not at all as expected!
dgpplain2015-03-19 20:02:20
How in the world does the last case reverse
the order of output?!
dgpplain2015-03-19 20:07:35
Well, I was sure folks were just mis-pasting, but no,
the order is truly reversed:

% puts ac:[ac]    
iu:1
it:1
ac:

Now consider the next case:

% proc ad {} {
set dict dict
$dict it
$dict iu
return $x
}
% puts ad:[ad]
it:1
iu:2
ad:2

So it becomes clear the fault in is the bytecode
compiling of ensembles.
dgpplain2015-03-19 20:10:31
Continuing...

% tcl::unsupported::disassemble proc ac
ByteCode 0x0x1941f50, refCt 1, epoch 18, interp 0x0x18bc670 (epoch 18)
  Source "\n    dict it\n    dict iu\n    return "...
  Cmds 3, src 39, inst 48, litObjs 5, aux 0, stkDepth 3, code/src 0.00
  Proc 0x0x18d0fa0, refCt 1, args 0, compiled locals 1
      slot 0, scalar, "x"
  Commands 3:
      1: pc 0-12, src 5-11        2: pc 13-34, src 17-23
      3: pc 35-46, src 29-37
  Command 1: "dict it"...
    (0) push1 0         # "dict"
    (2) push1 1         # "it"
    (4) push1 2         # "::it"
    (6) invokeReplace 2 2 
    (12) pop 
  Command 2: "dict iu"...
    (13) startCommand +21 1     # next cmd at pc 34, 1 cmds start here
    (22) push1 0        # "dict"
    (24) push1 3        # "iu"
    (26) push1 4        # "::iu"
    (28) invokeReplace 2 2 
    (34) pop 
  Command 3: "return $x"...
    (35) startCommand +12 1     # next cmd at pc 47, 1 cmds start here
    (44) loadScalar1 %v0        # var "x"
    (46) done 
    (47) done 

% ac
iu:1
it:2
% ac
iu:1
it:3
dgpplain2015-03-20 20:22:37
I suspect the

        TclSkipTailcall(interp);

within the INST_INVOKE_REPLACE implementation
may be the skunk at this particular garden party.
dgpplain2015-03-20 20:31:02
Yes.  Comment that out and the demo script
submitted behaves as one would expect.

....and then I ran the complete test suite
with 0 failures, so if that call has an
actual purpose, it's a purpose not demonstrated
by any test.
dgpplain2015-03-20 20:57:05
I imagine the call is there anticipating trouble
when the target of the ensemble redirect is
itself the [tailcall] command, such as

dict set map tc tailcall

.  But I'm not able 
to actually demonstrate that.
msoferplain2015-03-20 23:16:53
TclSkipTailcall just means "when looking for a place to splice in the tailcall, do not do it here - skip this spot, look at the previous command". It is used so that tailcalling out of a redirector (alias, ensemble, etc) really empties the stack - go to the redirector, don't stay at the redirection target. If it is missing, the "bug" will be a resource leak in repeated tailcalls.

Not sure if this is tested, but if it is it would only show when tested in a memdebug build - as the leak tests rely on [memory]
dgpplain2015-03-21 00:34:34
Fix and test committed.
msoferplain2015-03-21 15:07:34
Reopening, as the fix caused a violation of the constant-space property of tailcalls. See the new test tailcall-0.5.1
msoferplain2015-03-23 00:34:52
Definitive fix committed: we needed TclMarkTailcall rather than TclSkipTailcall.
Log in (anonymous is fine) to add a comment.