Tcl Source Code

View Ticket
Login
2026-11-18
18:32 • Closed ticket [7b51dbf6ca]: Code using namespace exported commands not regenerat... artifact: 7535e38d64 user: serhiy.storchaka
18:27 • Ticket [7b51dbf6ca]: 3 changes artifact: 0215a27188 user: serhiy.storchaka
18:22
Fix [7b51dbf6ca]: compiled code calling an imported command kept the old compiled form check-in: 69c0fdc481 user: serhiy.storchaka tags: core-8-6-branch
17:54 • Ticket [7b51dbf6ca] Code using namespace exported commands not regenerated corre... artifact: a24397582d user: jan.nijtmans
17:51
Fix [7b51dbf6ca]: compiled code calling an imported command kept the old compiled form check-in: b22e756d5a user: jan.nijtmans tags: core-9-0-branch
2026-11-13
12:32 • Pending ticket [7b51dbf6ca]: Code using namespace exported commands not regenera... artifact: b9e4522f79 user: jan.nijtmans
12:27
Fix [7b51dbf6ca]: compiled code calling an imported command kept the old compiled form check-in: 12c60e9c42 user: jan.nijtmans tags: trunk, main
2026-10-14
20:26 • Ticket [7b51dbf6ca] Code using namespace exported commands not regenerated corre... artifact: 94467f14eb user: serhiy.storchaka
19:09 • Ticket [7b51dbf6ca]: 3 changes artifact: 4b7338776a user: dgp
17:24 • Ticket [7b51dbf6ca]: 3 changes artifact: f419871a7b user: serhiy.storchaka
16:50 • Ticket [7b51dbf6ca]: 3 changes artifact: 024d6ebd7d user: dgp
15:25 • Ticket [7b51dbf6ca]: 2 changes artifact: ca26485856 user: serhiy.storchaka
15:25 • Ticket [7b51dbf6ca]: 3 changes artifact: e18710847b user: serhiy.storchaka
15:23
Fix [7b51dbf6ca]: compiled code calling an imported command kept the old compiled form after the ori... closed check-in: 866a927c0c user: serhiy.storchaka tags: compile-imported-cmd-redefined
2019-05-23
08:44 • Ticket [7b51dbf6ca] Code using namespace exported commands not regenerated corre... artifact: 3a22af6b5d user: sebres
07:09 • New ticket [7b51dbf6ca]. artifact: ea02d04661 user: beric

Ticket UUID: 7b51dbf6ca834b4e88b4be497b08e5fb08512f65
Title: Code using namespace exported commands not regenerated correctly
Type: Bug Created on: 2019-05-23 07:09:54
Submitter: beric Assigned to: jan.nijtmans
Subsystem: 47. Bytecode Compiler Severity: Critical
Priority: 5 Medium Last modified: 2026-11-18 18:32:48
Status: Closed Closed by: serhiy.storchaka
Resolution: Fixed Closed on: 2026-11-18 18:32:47
Version: 8.6.18, 9.0.5, 9.1.0
Description:

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 

User Comments:
sebres added on 2019-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.storchaka added on 2026-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.


dgp added on 2026-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.storchaka added on 2026-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.


dgp added on 2026-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.storchaka added on 2026-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.nijtmans added on 2026-11-13 12:32:23:

Merged to [12c60e9c42|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.nijtmans added on 2026-11-18 17:54:45:

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

Would be nice for 8.6.19 as well.


serhiy.storchaka added on 2026-11-18 18:27:58:

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