Tcl Source Code

View Ticket
Login
Ticket UUID: 3107983
Title: Glitch in the constant expression optimization.
Type: Bug Created on: 2010-11-12 15:32:45
Submitter: avl42 Assigned to: dgp
Subsystem: 47. Bytecode Compiler Severity:
Priority: 5 Medium Last modified: 2011-01-31 19:40:03
Status: Open Closed by:
Resolution: None Closed on:
Version: None
Description:
   % proc tcl::mathfunc::foo x { puts "foo:>>>$x<<<"; return $x }
   % set x 1; expr {foo($x?" 0x42 ":" 0x41" )}
   foo:>>> 0x42 <<<
   66
   % expr {foo(1?" 0x42 ":" 0x41" )}
   foo:>>>66<<<
   66
   % 

If the condition is a constant, then the result of the "?:" operator is numericalized even if it gets passed to an external function.
User Comments:
ferrieux added on 2010-11-12 22:34:54:
Analysis at http://groups.google.com/group/comp.lang.tcl/browse_frm/thread/c7888bc3ee8b7ad8/cbe4abc6190e7727#cbe4abc6190e7727

ferrieux added on 2010-11-12 22:44:52:
Two possible decisions:

   (1) stick to the convert-to-numeric spirit, and add extra TryCvtToNumeric on entry to user-provided mathfuncs

   (2) decide it is over-eager, and keep it only on entry to built-in numerics. Remove it from end-of-expr.

I guess (1) is more gentle, as it keeps compatibility. (2) may sound harsh, but it would be more EIAS-ly.

avl42 added on 2010-11-12 22:49:08:
My preference would be for (2).

avl42 added on 2010-11-12 22:56:41:
...but that might require a TIP, so the bug-fix probably should only change the particular shortcut-route to not use "expr" for pre-calculation, but perhaps some (for now internal)  "expr-without-tryCvtToNumeric".

dgp added on 2010-11-18 03:21:50:
According to TIP 232,

"The [expr] command shall be modified so that an expression of the form:

   f(arg1,arg2,...)

shall generate code equivalent to that generated by

   [tcl::mathfunc::f [expr { arg1 }] [expr { arg2 }] ...]
"

This means the constant optimizer case is actually
getting this done as prescribed, and it's the non-constant
case which is buggy.

The mathfunc.n manpage isn't quite as explicit, but suggests
the same thing.

dgp added on 2010-11-18 03:27:28:
Heh.  TIP 232 *also* says:

"There is no longer any restriction [on functions] ...
 that their arguments and results must be numeric."

Hoo boy.

ferrieux added on 2010-11-18 04:25:38:
Then, the tryCvtToNumeric emitted at the end of each [expr] is ... unwanted ?

dgp added on 2010-11-18 04:37:41:
I just know the issue of passing strings to/from
functions was raised and discussed when the
feature was in dev or new, but I cannot find it.