Artifacts Associated With Ticket a1d08eb5353fd6fc
Ticket change [27564a0c90] (rid 153578) by GrAnd on 2017-05-05 15:51:20:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/html"
- comment initialized to:
<p> While doing my project migration from Tcl 8.5.9/Itcl 3.4 to Tcl 8.6.6/Itcl 4.0.5 I've faced with inconsistency of `$this` variable depending on how it's accessed. Here is the minimized testcase: </p> <pre> puts "Tcl version : $tcl_patchLevel" puts "Itcl version : [package require Itcl]" itcl::class Base { public { method base_process {script} { uplevel $m_main main_process [list $script] } method set_main {main} { set m_main $main } } protected { variable m_main } } itcl::class Main { inherit Base public { method main_process {script} { uplevel $script } } } itcl::class Worker { inherit Base public { method worker_process_direct {} { puts "Direct query: this = $this" } method worker_process_inderect {} { base_process {puts "Indirect query: this = $this"} } method worker_process_both {} { puts "Direct query: this = $this" base_process {puts "Indirect query: this = $this"} } } } Main main Worker worker worker set_main main puts "\n==== worker_process_direct ====" worker worker_process_direct puts "\n==== worker_process_indirect ====" worker worker_process_inderect puts "\n==== worker_process_both ====" worker worker_process_both </pre> <p> `worker_process_direct` and `worker_process_both` functions always provide correct results. But `worker_process_inderect` works correctly only with old version of Tcl/Itcl. For Tcl 8.6.6/Itcl 4.0.5 `$this` variable is strangely changed to the instance of `Main` class instead of `Worker`. </p> <p> Here is the output of the script above for two versions of Tcl/Itcl. </p> <hr width=150 align=left> <pre> Tcl version : 8.5.9 Itcl version : 3.4 ==== worker_process_direct ==== Direct query: this = ::worker ==== worker_process_indirect ==== Indirect query: this = ::worker <<<<<<<<<<<< CORRECT ==== worker_process_both ==== Direct query: this = ::worker Indirect query: this = ::worker </pre> <hr width=150 align=left> <pre> Tcl version : 8.6.6 Itcl version : 4.0.5 ==== worker_process_direct ==== Direct query: this = ::worker ==== worker_process_indirect ==== Indirect query: this = ::main <<<<<<<<<< INCORRECT ==== worker_process_both ==== Direct query: this = ::worker Indirect query: this = ::worker </code> <hr width=150 align=left> <p> Also, see Donal Fellows' comment here - <a href="https://stackoverflow.com/questions/43768288/itcl-inconsistency-of-this-variable#43778283">https://stackoverflow.com/questions/43768288/itcl-inconsistency-of-this-variable#43778283</a> </p> - foundin initialized to: "8.6.6"
- is_private initialized to: "0"
- login: "GrAnd"
- priority initialized to: "5 Medium"
- private_contact initialized to: "d2848f54ebf917fc6fb2a00b4ba4b2271563347e"
- resolution initialized to: "None"
- severity initialized to: "Severe"
- status initialized to: "Open"
- submitter initialized to: "GrAnd"
- subsystem initialized to: "07. Variables"
- title initialized to: "Itcl: inconsistency of $this variable"
- type initialized to: "Bug"
Ticket change [c2e1504f67] (rid 153584) by dgp on 2017-05-05 18:51:31:
- icomment:
Thank you for the report. Until I find time to look at it, could you try the combination of Itcl 3.4.3 with Tcl 8.6.6 which might help us isolate the Itcl changes from the Tcl changes.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [2d8acb124e] (rid 153591) by dgp on 2017-05-05 19:39:10:
- icomment:
The following output suggests to me this is an Itcl issue rather than a Tcl issue: Tcl version : 8.6.7 Itcl version : 3.4 ==== worker_process_direct ==== Direct query: this = ::worker ==== worker_process_indirect ==== Indirect query: this = ::worker ==== worker_process_both ==== Direct query: this = ::worker Indirect query: this = ::worker
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [87e1703e08] (rid 153592) by dgp on 2017-05-05 20:24:43:
- icomment:
There doesn't seem to be a need for Main to inherit Base . I remove that and nothing changes.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [e30484578a] (rid 153616) by dkf on 2017-05-06 16:50:19:
- icomment:
If I convert the code to basic TclOO as below, it appears to work correctly. I'm guessing therefore that the bug is in a resolver that itcl is installing. Variable resolvers are <i>astonishingly</i> difficult to write correctly. <pre> puts "Tcl version : $tcl_patchLevel" oo::class create Base { # <<< CHANGE <<< constructor {} { # <<< CHANGE <<< variable this [self]; # <<< CHANGE <<< }; # <<< CHANGE <<< variable m_main; # <<< CHANGE <<< method base_process {script} { uplevel [list $m_main main_process $script] } method set_main {main} { set m_main $main } } oo::class create Main { # <<< CHANGE <<< superclass Base; # <<< CHANGE <<< variable this; # <<< CHANGE <<< method main_process {script} { uplevel $script } } oo::class create Worker { # <<< CHANGE <<< superclass Base; # <<< CHANGE <<< variable indirect this; # <<< CHANGE <<< method worker_process_direct {} { puts "Direct query: this = $this" set indirect {puts "Indirect query: this = $this"} } method worker_process_inderect {} { my base_process $indirect; # <<< CHANGE <<< } method worker_process_both {} { puts "Direct query: this = $this" my base_process $indirect; # <<< CHANGE <<< } } Main create main; # <<< CHANGE <<< Worker create worker; # <<< CHANGE <<< worker set_main main puts "\n==== worker_process_direct ====" worker worker_process_direct puts "\n==== worker_process_indirect ====" worker worker_process_inderect puts "\n==== worker_process_both ====" worker worker_process_both </pre> However, I've studied the Tcl code in more depth and I now conclude that my dark mutterings about other bugs were in fact unfounded. True, I really don't know why the bytecode compilations vary so much, but change to the kinds of bytecodes issued was correct and deliberate. (Indeed, I remember doing the switch to using <code>loadStk</code> from <code>loadScalarStk</code>; that particular one was because we had two instructions doing the exactly identical thing.) <p> My <b><i>guess</i></b> is that Itcl's resolver is using the wrong stack frame in the interpreter to drive its resolution rules. (There are two confusingly-similar fields, for reasons I don't quite remember. I've been caught out by this in the past though, so I get it right now.) - login: "dkf"
- mimetype: "text/html"
- username: "dkf"
- icomment:
Ticket change [0c1b72107c] (rid 153659) by dgp on 2017-05-09 13:56:40:
- icomment:
See new Itcl ticket: https://core.tcl.tk/itcl/tktview/c45384364c The reduced demo there establishes... 1) Inheritance is not needed to see trouble. 2) Special handling of the 'this' variable plays a role.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [744fdf64f3] (rid 153778) by dgp on 2017-05-16 14:42:38:
- assignee changed to: "dgp"
- closedate changed to: "2457890.11294017"
- closer changed to: "dgp"
- icomment:
Fixed for release Itcl 4.1.0 to be bundled with Tcl 8.6.7.
- login: "dgp"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Closed"
- subsystem changed to: "81. Bundled Packages"