Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-10-11
19:33 • Ticket [229fa65563] tailcall inside apply inside namespace status still Clos... artifact: 942e360e6f user: pooryorick
2016-07-09
08:48 • Closed ticket [229fa65563]. artifact: 5525d1bd72 user: dkf
2016-06-25
08:02 • Ticket [229fa65563]: 3 changes artifact: 70e9b7b80d user: aspect
2016-06-03
14:28 • New ticket [229fa65563]. artifact: 83b72afc7d user: pooryorick

tailcall inside apply inside namespace

Closed / Invalid229fa655638ab16d794ea819296cf9f3a9088619Created 11 years agoUpdated 2 years ago
Descriptionwiki2016-06-03 14:28:36
In the following script, there are two identical apply commands. The one inside namespace val n1 ... succeeds, while the other one fails:

namespace eval n1 {
    proc p2 {} {
        set name [lindex [info level 0] 0]
        set name [uplevel 1 [list ::namespace origin $name]]
    }
    puts [apply [list {} {tailcall p2} ::n1]]
}

puts [apply [list {} {tailcall p2} ::n1]]

Error:

invalid command name "p2"
    while executing
"::namespace origin p2"
...
</code>
aspectplain2016-06-25 08:02:20
Isn't this expected?

% namespace eval n0 {
    proc p0 {} {
        uplevel 1 {namespace current}
    }
}
% namespace eval n1 {
    puts [apply {{} {tailcall p0} ::n0}]
}
::n1

The lambda body evaluates in a level in ::n0.  [tailcall] looks up p0 in that context, finding ::n0::p0).  Then the apply's frame is discarded and replaced with a new call.

Thus {uplevel 1} doesn't find ::n0 because that level no longer exists - level -1 is where [apply] was invoked.

It's unfortunate that:

  [uplevel 1 [list ::namespace origin [lindex [info level 0] 0]]]

can fail to find a qualified name when used in a procedure that has been tailcalled.  Prior to [tailcall], I believe this idiom was 100% reliable (?).

If that is a significant loss, the only way I can see to restore it is if [tailcall] puts the fully-qualified name of the command on the stack .. but that might have other implications that break the utility of [lindex [info level 0] 0].  Meanwhile, I suspect [tailcall] already mixes badly with highly introspective procedures like this.
dkfhtml2016-07-09 08:48:24

I'd also expect this to not work for those reasons; tailcall replaces a stack frame entirely by design, so anything that would have required the stack frame to be there (such as introspection) won't work.

If it is causing problems in a specific case, replace tailcall ... with return [uplevel 1 [list ...]]

pooryorick Ticket [229fa65563]: 5 changes [History diff]2024-10-11 19:33:11
Log in (anonymous is fine) to add a comment.