Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Code using namespace exported commands not regenerated correctly

Closed / Fixed7b51dbf6ca834b4e88b4be497b08e5fb08512f65Created 8 years agoUpdated 14 days ago
Descriptionwiki2019-05-23 07:09:54
While experimenting if namespace exported commands were optimized as direct commands when argument is "args" and body is empty, I have found that bytecode generated for the commands were not regenerated correctly:
   namespace eval debug {
     proc assert {args} {}
     namespace export assert
   }
   namespace import ::debug::assert
   proc test1 {} {assert anything} 

The generated bytecode shows that the assert call is optimized:

tcl::unsupported::disassemble proc test1
ByteCode 0x04910178, refCt 1, epoch 18, interp 0x0480C488 (epoch 18)
  Source "assert anything"
  Cmds 1, src 15, inst 3, litObjs 1, aux 0, stkDepth 1, code/src 0.00
  Proc 0x049051A0, refCt 1, args 0, compiled locals 0
  Commands 1:
      1: pc 0-1, src 0-14
  Command 1: "assert anything"
    (0) push1 0 	# ""
    (2) done 

But redefining the assert command:

   proc debug::assert {args} {puts $args}

The compile epoch has changed, but the assert call is still optimized:

tcl::unsupported::disassemble proc test1
ByteCode 0x04910178, refCt 1, epoch 19, interp 0x0480C488 (epoch 19)
  Source "assert anything"
  Cmds 1, src 15, inst 3, litObjs 1, aux 0, stkDepth 1, code/src 0.00
  Proc 0x049051A0, refCt 1, args 0, compiled locals 0
  Commands 1:
      1: pc 0-1, src 0-14
  Command 1: "assert anything"
    (0) push1 0 	# ""
    (2) done 
Referenced by4
  • 69c0fdc481Fix [7b51dbf6ca]: compiled code calling an imported command kept the old compiled form check-in2026-11-18 18:22:08
  • b22e756d5aFix [7b51dbf6ca]: compiled code calling an imported command kept the old compiled form check-in2026-11-18 17:51:56
  • 12c60e9c42Fix [7b51dbf6ca]: compiled code calling an imported command kept the old compiled form check-in2026-11-13 12:27:24
  • 866a927c0cFix [7b51dbf6ca]: compiled code calling an imported command kept the old compiled form after the original command was redefined or replaced, since the imported command keeps a copy of the compileProc. Compile an imported command as the command it refers to. Tests namespace-12.2 to namespace-12.6. check-in2026-10-14 15:23:50
sebreswiki2019-05-23 08:44:06

This has basically nothing with optimization and going to missing epoch "check" of compiled blocks with imported commands from other namespace...

% namespace inscope tcl::dict {namespace export get}
% namespace import tcl::dict::get
% proc t1 {} {get {a 1} a}
% t1; # get is compiled to dictGet 1
1
% proc tcl::dict::get args error; # overwrite command with proc
% t1; # still compiled as dictGet 1
1

serhiy.storchakawiki2026-10-14 15:25:07

The imported command gets a copy of the compileProc of the original command at import time, and keeps it when the original command is redefined, so the recompiled code still uses the old compileProc.

Proposed fix in branch compile-imported-cmd-redefined: an imported command is compiled as the command it refers to. Tests namespace-12.2 to namespace-12.6.

serhiy.storchaka Ticket [7b51dbf6ca]: 2 changes [History diff]2026-10-14 15:25:48
dgpplain2026-10-14 16:50:17
Please check whether the proposed change disturbs the
functioning of the [control::assert] command provided
by the "control" package that is included in the tcllib collection.
serhiy.storchakawiki2026-10-14 17:24:20

I checked [control::assert] from the current tcllib trunk with main and with the branch: called as [control::assert] and through [namespace import], from procs compiled before and after switching it with [control::control assert enabled], and with a callback. The results are identical, and do.test and no-op.test of the control module pass with both.

dgpplain2026-10-14 19:09:42
Thank you for checking.

More general comment is that [namespace import] has always
been a distinct operation from [interp alias].  A rename
of the "exported" command can change how the alias functions,
while the imported command remains linked to what was imported
in a stable way.

Any "fix" that disrupts this difference needs careful examination.
serhiy.storchakawiki2026-10-14 20:26:40

The branch keeps this difference: an imported command is compiled as the command it is linked to, found by following the import (TclGetOriginalCommand()), not by name. I checked with main and the branch: after renaming the exported command, the import still calls it under its new name (also after redefining it there), and a new command created under the old name is not used; an alias follows the name. The only difference from main is that compiled calls no longer use the compiled form of the old definition.

jan.nijtmanswiki2026-11-13 12:32:23

Merged to [trunk]

To me this looks OK, but - in case I'm missing something - let's keep this in trunk for a while (so don't backport for 9.0.5 yet).

Please test everything you can on trunk, and report here!

jan.nijtmanswiki2026-11-18 17:54:45

Now merged to core-9-0-branch as well

Would be nice for 8.6.19 as well.

serhiy.storchakawiki2026-11-18 18:27:58

Backported to core-8-6-branch: [69c0fdc481].

serhiy.storchaka Closed ticket [7b51dbf6ca]. [History diff]2026-11-18 18:32:48
Log in (anonymous is fine) to add a comment.