Skip to content
Tcl Source Code History Of Ticket 67bab20ac692c8e4
Login

History Of Ticket 67bab20ac692c8e4

Artifacts Associated With Ticket 67bab20ac692c8e4

  1. Ticket change [a045a1e6d4] (rid 168048) by sebres on 2018-12-21 15:21:39:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized to:
      Initially found by me (after some mistake/typo in code) as possible "legal" basic construct, like <code>"if"</code> without body, but with <code>"else"</code>:
      <code><pre style="padding-left:10pt">
      if {[catch { set no error }]} else {puts "negative case only"}
      </pre></code>
      or even still simple:
      <code><pre style="padding-left:10pt">
      if 0 else {puts "negative case only"}
      </pre></code>
      It is clear a mistake (but very unobvious if one just take a look at code), because <code>"else"</code> will be to body and the real body of <code>"else"</code> remains as negative case body (just because <code>"else"</code> operand is optional in <code>"if"</code>-construct). 
      This way it is similar to:
      <code><pre style="padding-left:10pt">
      if 0 {...} {puts "negative case only"}
      </pre></code>
      Obviously, it works this way without any problem, until expression results in true (where it will always fail, because trying execute <code>"else"</code> as body).
      <code><pre style="padding-left:10pt">
      % if 0 else {puts "negative case only"}
      negative case only
      % if 1 else {puts "negative case only"}
      <b style="color:red">invalid command name "else"</b>
      </pre></code>
      The command is implemented this way possibly since the beginning (tested and reproducible with Tcl >= 8.2.2).
      
      Discussing the issue in tcl-chat, we've get an idea to "fix" this bizarre corner-cases without harm of current code, and improve it retaining "backwards" compatibility (for some possible obscure old code-pieces with <code>"else"</code>-only and expressions currently always resulting in <code>"false"</code>, therefore still working until today.
      
      Remain <code>"else"</code> as optional keyword, but if this is provided as body (as 3rd argument, without <code>"then"</code>) it will be recognized as keyword of <code>"if"</code>-command. Following examples show how it may work:
      <code><pre style="padding-left:10pt">
      % if 0 else {puts "negative case only"}
      negative case only
      % if 1 else {puts "negative case only"}
      &lt;none&gt;
      % proc else args {puts "else command $args"}
      % if 1 then {else}
      else command
      % if 1 then
      <b style="color:red">wrong # args: no script following "then" argument</b>
      % if 1 else
      <b style="color:red">wrong # args: no script following "else" argument</b>
      </pre></code>
      
      There are another keywords like <code>"elseif"</code> which could be handled similar way (for consistency reasons).<br/>
      Here is an example (provided in chat from avl42), which would call <code>foo</code> only if both <code>$cond1</code> and <code>$cond2</code> were false:
      <code><pre style="padding-left:10pt">
      if $cond1 elseif $cond2 else {foo}  
      </pre></code>
      
      I've a branch covering this "bug", prevents mistakes like this resp. improves the readability and consistency of (compiled and not-compiled) <code>"if"</code>-command in Tcl.<br/>
      WiP: working on the test-cases now.
      
    5. foundin initialized to: "all"
    6. is_private initialized to: "0"
    7. login: "sebres"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Severe"
    11. status initialized to: "Open"
    12. submitter initialized to: "sebres"
    13. subsystem initialized to: "17. Commands I-L"
    14. title initialized to:
      if-construct: bizzare cases "without" body (mistake, typo, etc)
      
    15. type initialized to: "Bug"
  2. Ticket change [2778ed2003] (rid 168049) by sebres on 2018-12-21 15:33:01:

    1. comment changed to:
      Initially found by me (after some mistake/typo in code) as possible "legal" basic construct, like <code>"if"</code> without body, but with <code>"else"</code>:
      <code><pre style="padding-left:10pt">
      if {[catch { set no error }]} else {puts "negative case only"}
      </pre></code>
      or even still simple:
      <code><pre style="padding-left:10pt">
      if 0 else {puts "negative case only"}
      </pre></code>
      It is clear a mistake (but very unobvious if one just take a look at code), because <code>"else"</code> will be to body and the real body of <code>"else"</code> remains as negative case body (just because <code>"else"</code> operand is optional in <code>"if"</code>-construct). 
      This way it is similar to:
      <code><pre style="padding-left:10pt">
      if 0 {...} {puts "negative case only"}
      </pre></code>
      Obviously, it works this way without any problem, until expression results in true (where it will always fail, because trying execute <code>"else"</code> as body).
      <code><pre style="padding-left:10pt">
      % if 0 else {puts "negative case only"}
      negative case only
      % if 1 else {puts "negative case only"}
      <b style="color:red">invalid command name "else"</b>
      </pre></code>
      The command is implemented this way possibly since the beginning (tested and reproducible with Tcl >= 8.2.2).
      
      Discussing the issue in tcl-chat, we've get an idea to "fix" this bizarre corner-cases without harm of current code, and improve it retaining "backwards" compatibility (for some possible obscure old code-pieces with <code>"else"</code>-only and expressions currently always resulting in <code>"false"</code>, therefore still working until today.
      
      Remain <code>"else"</code> as optional keyword, but if this is provided as body (as 3rd argument, without <code>"then"</code>) it will be recognized as keyword of <code>"if"</code>-command.
      <code><pre style="padding-left:10pt">
      if $cond else {foo} 
      </pre></code>
      would be then valid (short) replacement for empty body cases like:
      <code><pre style="padding-left:10pt">
      if $cond {} else {foo} 
      if $cond then {} else {foo} 
      </pre></code>
      Following examples show how it may work:
      <code><pre style="padding-left:10pt">
      % if 0 else {puts "negative case only"}
      negative case only
      % if 1 else {puts "negative case only"}
      &lt;none&gt;
      % proc else args {puts "else command $args"}
      % if 1 then {else}
      else command
      % if 1 then
      <b style="color:red">wrong # args: no script following "then" argument</b>
      % if 1 else
      <b style="color:red">wrong # args: no script following "else" argument</b>
      </pre></code>
      
      There are another keywords like <code>"elseif"</code> which could be handled similar way (for consistency reasons).<br/>
      Here is an example (provided in chat from avl42), which would call <code>foo</code> only if both <code>$cond1</code> and <code>$cond2</code> were false:
      <code><pre style="padding-left:10pt">
      if $cond1 elseif $cond2 else {foo} 
      </pre></code>
      and would be then proper replacement for:
      <code><pre style="padding-left:10pt">
      if $cond1 then {} elseif $cond2 {} else {foo} 
      </pre></code>
      
      I've a branch covering this "bug", prevents mistakes like this resp. improves the readability and consistency of (compiled and not-compiled) <code>"if"</code>-command in Tcl.<br/>
      WiP: working on the test-cases now.
      
    2. login: "sebres"
    3. mimetype: "text/plain"