Artifacts Associated With Ticket fbc88fb645c5b2f4
Ticket change [cffd6b1618] (rid 154037) by emiliano on 2017-05-30 20:56:02:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/plain"
- comment initialized to:
The [variable] command, when used inside a procedure, doesn't follow the variable resolution rules as stated in namespace(n): "Variable names are always resolved by looking first in the current namespace, and then in the global namespace" When using relative names, it fails to look in the global namespace. In this interactive session, from the global namespace: % namespace eval a {variable a "a in [namespace current]"} % namespace eval foo {puts $a::a} a in ::a % namespace eval foo::a {variable a "a in [namespace current]"} % namespace eval foo {puts $a::a} a in ::foo::a % namespace eval foo {proc bar {} {puts $a::a}} % foo::bar a in ::foo::a % namespace delete foo::a % foo::bar a in ::a % namespace eval foo {proc bar {} {variable a::a; puts $a}} % foo::bar can't access "a::a": parent namespace doesn't exist % namespace eval foo::a {variable a "a in [namespace current]"} % foo::bar a in ::foo::a the error shown is not expected; it should instead print "a in ::a", same as using $a::a directly inside the proc. - foundin initialized to: "8.7"
- is_private initialized to: "0"
- login: "emiliano"
- priority initialized to: "5 Medium"
- private_contact initialized to: "0da21f2649e2f7139a4113668982586168b3d24e"
- resolution initialized to: "None"
- severity initialized to: "Important"
- status initialized to: "Open"
- submitter initialized to: "emiliano"
- subsystem initialized to: "07. Variables"
- title initialized to:
[variable] doesn't follow variable resolution rules
- type initialized to: "Bug"
Ticket change [2190cee623] (rid 154051) by sebres on 2017-05-31 06:57:35:
- closedate changed to: "2457904.78999284"
- closer changed to: "sebres"
- icomment:
You are wrong with your assessment: in your error-case the variable is not fully qualified (thus relative current namespace), so it references "::foo::a::a" which is indeed not available. Either you want to reference "::foo::a::a" (then you should create this var): <code><pre> % namespace eval foo {namespace eval a {set a "a in [namespace current]"}; proc bar {} {variable a::a; puts $a}} % foo::bar a in ::foo::a </pre></code> Rather you want to reference "::a::a" (then you should write it full-qualified): <code><pre> % namespace eval foo {proc bar {} {variable ::a::a; puts $a}} % foo::bar a in ::a </pre></code> Thus closed as invalid. - login: "sebres"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Invalid"
- status changed to: "Closed"
Ticket change [cdcd39a701] (rid 154106) by dgp on 2017-05-31 13:07:20:
- closer changed to: "nobody"
- icomment:
I asked for this ticket to be opened. If [variable] resolves varnames differently from $-substitution, that needs to be understood far more clearly.
- login: "dgp"
- mimetype: "text/plain"
- resolution changed to: "None"
- status changed to: "Open"
Ticket change [14386efd0b] (rid 154111) by sebres on 2017-05-31 16:09:08:
- icomment:
<blockquote>> If [variable] resolves varnames differently from $-substitution </blockquote> I don't think so... Following: <pre><code> set ::a "::a (of global)"; namespace eval ::a {set a "[namespace current]::a"} set cnt [namespace eval ::foo { namespace eval a {set a "[namespace current]::a"} set a "[namespace current]::a" proc init {} { set i 0 foreach {vmap rmap} { a ::foo::a a::a ::foo::a::a ::a ::a ::a::a ::a::a ::foo::a ::foo::a ::foo::a::a ::foo::a::a } { incr i set args [list [list i $i] [list vmap $vmap] [list rmap $rmap]] proc test$i $args { variable $vmap set vexp {"$a" eq "[set $rmap]"} puts -nonewline [format "%d: %15s is %-15s == " $i $vmap $rmap] puts [format " %5s == {%s eq %-15s} -- {%s}" [if $vexp {set _ OK} else {set _ WRONG}] {$a} $rmap [subst $vexp]] } puts "init: proc test$i { $args } ..." } return $i } init }] puts {}; set i 0; time {foo::test[incr i]} $cnt; set _ {} </code></pre> results to the: <pre><code> init: proc test1 { {i 1} {vmap a} {rmap ::foo::a} } ... init: proc test2 { {i 2} {vmap a::a} {rmap ::foo::a::a} } ... init: proc test3 { {i 3} {vmap ::a} {rmap ::a} } ... init: proc test4 { {i 4} {vmap ::a::a} {rmap ::a::a} } ... init: proc test5 { {i 5} {vmap ::foo::a} {rmap ::foo::a} } ... init: proc test6 { {i 6} {vmap ::foo::a::a} {rmap ::foo::a::a} } ... 6 % puts {}; set i 0; time {foo::test[incr i]} $cnt; set _ {} 1: a is ::foo::a == OK == {$a eq ::foo::a } -- {"::foo::a" eq "::foo::a"} 2: a::a is ::foo::a::a == OK == {$a eq ::foo::a::a } -- {"::foo::a::a" eq "::foo::a::a"} 3: ::a is ::a == OK == {$a eq ::a } -- {"::a (of global)" eq "::a (of global)"} 4: ::a::a is ::a::a == OK == {$a eq ::a::a } -- {"::a::a" eq "::a::a"} 5: ::foo::a is ::foo::a == OK == {$a eq ::foo::a } -- {"::foo::a" eq "::foo::a"} 6: ::foo::a::a is ::foo::a::a == OK == {$a eq ::foo::a::a } -- {"::foo::a::a" eq "::foo::a::a"} </code></pre> What I'm doing wrong, resp. which point I miss here? Respectively which resolver exactly acts differently? I hold it for impossible (as long as not some own interp/namespace variable-resolver used). - login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [85aa0e9d1e] (rid 154125) by dgp on 2017-06-01 17:01:15:
- icomment:
Didn't even try to decipher that. Look at the original ticket. First two commands demonstrate that when the current namespace is ::foo, the variable name "a::a" can resolve to ::a::a . It does so when ::foo::a::a does not exist and ::a::a exists. The 8th command demonstrates that the same resolution happens within a proc body under the same conditions. The 9th and 10th commands demonstrate that when the [variable] command is brought into the picture within a proc body where the current namespace is ::foo, and when ::foo::a::a does not exist and ::a::a does exist, when "a::a" is passed as an argument to [variable] it does not get the same resolution treatment. This is at least surprising and sure smells like a bug. At best it's an ugly inconsistency. Thus the title: [variable] doesn't follow variable resolution rules
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [91eace70f4] (rid 154127) by sebres on 2017-06-01 20:54:52:
- icomment:
Aaaa... now I've got it. Then more understandable example should be provided: <code><pre> % namespace eval a {variable a "a in [namespace current]"} % catch {namespace delete ::foo}; \ namespace eval foo { set i 0; time { incr i proc test1 {} {catch {set a::a} r; set r} puts -nonewline "${i}.a. direct: "; puts [test1] proc test2 {} {catch {upvar a::a a; set a} r; set r} puts -nonewline "${i}.b. as upvar: "; puts [test2] proc test3 {} {catch {variable a::a; set a} r; set r} puts -nonewline "${i}.c. variable: "; puts [test3] # repeat with existsing ::foo::a if {$i == 1} { puts "Create ::foo::a now ..." namespace eval a {variable a "a in [namespace current]"} } } 2 }; set _ {} 1.a. direct: a in ::a 1.b. as upvar: a in ::a 1.c. variable: can't access "a::a": parent namespace doesn't exist Create ::foo::a now ... 2.a. direct: a in ::foo::a 2.b. as upvar: a in ::foo::a 2.c. variable: a in ::foo::a </pre></code> Why test 1.c. fails? Resp. why resolving of var with "variable" acts differently, <b>if the relative namespace does not exists</b>? Actually it is so documented. It works as designed, just because the completely resolving of var-name by "variable" will be <b>always</b> mapped relative current namespace, thus to the "::foo::a::a" (but ns "::foo::a" not exists, so BOOM). Emphasis to "always" - regardless of whether this variable/namespace exists or not exists. More interesting question would be vice versa - why "upvar a::a a" resolves differently as "variable a::a"? And why it is documented differently... - login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [b157dd36fe] (rid 259702) by serhiy.storchaka on 2026-10-21 10:03:36:
- icomment:
Fixed in Tcl 9.0 by TIP #278 ([580ef6f047]): relative qualified variable names no longer fall back to the global namespace, so <tt>$a::a</tt> and <tt>variable a::a</tt> agree. 8.6 keeps the old rules for compatibility.
- login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"
- icomment: