Tcl Source Code

View Ticket
Login
Ticket UUID: 1893052
Title: Object type info can be lost after comparison (e.g. INST_EQ)
Type: Bug Created on: 2008-02-13 20:02:31
Submitter: nobody Assigned to: dgp
Subsystem: 47. Bytecode Compiler Severity:
Priority: 5 Medium Last modified: 2008-02-14 23:13:56
Status: Closed Closed by: dgp
Resolution: Invalid Closed on: 2008-02-14 16:13:56
Version: None
Description:
set a ...
set b ...
if {$a == $b} {
    ...
}

Regardless of the actual types of objects a and b,
numerical comparison is coerced by Tcl engine if at
all possible, which includes the case when string
representations of both objects look like numbers.
Both arguments are then converted to numeric types
(e.g. via Tcl_GetWideIntFromObj), and the original
object type information is irretrievably lost. This
is a problem when custom type of an object cannot
be restored from its string representation.
User Comments:
dgp added on 2008-02-14 03:20:44:
Logged In: YES 
user_id=80530
Originator: NO


Of course, strictly speaking, all Tcl
values are string-based in their semantics, so
an inability to restore from string rep
is an unsupported (ab)use of the Tcl_ObjType
system.

That said, folks do make this sort of thing
work acceptably well.  Due to what is reported
here, and other places where the core may
shimmer intreps, if you have such a value
that isn't fully specified by the string
rep you have chosen, it is best if you choose
a string rep that can't be converted to
the known core types.  You can only have
your fragile value get converted via
Tcl_GetWideIntFromObj() if you've chosen
a string rep that is a valid wide integer.
Don't Do That.

hobbs added on 2008-02-14 04:28:54:
Logged In: YES 
user_id=72656
Originator: NO

In addition, this is commonly handled by creating the string rep in the form of sprintf(buf, "foo%p", internalPtrVal), recognizing your handle by scan of the same and caching it in a hash table.  You have to deal with refcounting and such, but this is just the nature of custom Tcl_Objs.

nobody added on 2008-02-14 08:08:04:
Logged In: NO 

A couple of comments:
1. I have a feeling that semantically read-only operations, such as comparison,
have no right whatsoever to erase type information in an object of an unknown
type, because there is no guarantee that the type conversion is reversible.
To the best of my understanding, there is nothing in the Tcl library reference
that would suggest that Tcl_SetFromAnyProc MUST always succeed.
2. Any assumption about performance gain due to conversion to integer, although
generally correct for plain strings, may prove very wrong when applied universally
to custom types, even if the conversion is reversible.
3. String representation of an object of a custom type may, in fact, be meaningful,
as opposed to "foo%p".

dgp added on 2008-02-14 23:13:56:
Logged In: YES 
user_id=80530
Originator: NO


Your error is in thinking about
this as "type information".  That's
not what it is.  Tcl values are untyped,
or string typed if you prefer.  The
internal rep is only a cache of the
most recent conversion done so that
if that same conversion is needed again
(frequently the case) it need not be
repeated.  The objPtr->typePtr indicates
which kind of conversion has been cached;
nothing more.

From this perspective, your claim "there
is no guarantee that the type conversion is
reversible" is simply false.  The string value
is the value.  If you can't do what you need
with the string value alone (plus possibly some
state you've arranged to associate with the interp),
then at best you have an abuse of the Tcl_Obj system,
which might work if you take special care and jump
through some hoops, but isn't the supported model.

One hoop you can jump through that will help avoid
this issue is to craft string reps that are not
valid values of other known Tcl_ObjTypes.

Finally, back to your original post, it may well be
that you can avoid the immediate problem by using the
"eq" operator instead of the "==" operator:

set a ...
set b ...
if {$a eq $b} {...}