[Issue 3092] Indexing a tuple produces a tuple containing the indexed element

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Oct 10 06:56:26 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=3092



--- Comment #5 from Manuel König <manuelk89 at gmx.net> 2010-10-10 06:55:54 PDT ---
Some updates:

template TupleBug(values...)
{
    pragma(msg, "values: ", values);
}

alias TupleBug!(int, int) _0; // prints 'values: (int, int)'
alias TupleBug!(int, 1)   _1; // prints 'values: tuple((int), 1)'

After investigating the frontend, i can explain this behaviour:

(int, int) is treated as a type by dmd, not as a tuple, but the mixed tuple
(int, 1) is treated as a tuple. To see that, go to DsymbolExp::semantic in
expression.c and insert the printf's like this:

    t = s->getType();
    if (t)
    {
        // entered for TupleBug!(int, int)
        printf("'%s' is type\n", t->toChars()); // prints "'(int, int)' is
type"
        return new TypeExp(loc, t);
    }

    TupleDeclaration *tup = s->isTupleDeclaration();
    if (tup)
    {
        // entered for TupleBug!(int, 1)
        printf("'%s' is tuple\n", tup->toChars()); // prints "'values' is
tuple"
        e = new TupleExp(loc, tup);
        printf("e = %s\n", e->toChars()); // prints 'e = tuple((int), 1)'
        e = e->semantic(sc);
        printf("e->semantic(sc) = %s\n", e->toChars()); // prints 'e->semantic
= tuple((int), 1)
        return e;
    }

I don't know if it's intended behaviour to treat type tuples separately from
mixed tuples even in the frontend, but that's how it is. Type tuples don't seem
to be buggy (they are not affected by any aliasing, indexing or pragma(msg,...)
bugs described before). So what's really buggy are the 'true' mixed tuples.

I can at least explain what causes the pragma(msg, values) bug, which currently
prints tuple((int), 1). The bug is not, as it seems, that (int) is a singleton
tuple containing an int, it's actually a bug in e->toChars(), which calls
TupleExp::toCBuffer [that prints the 'tuple(' ')' frame], which calls
argsToCBuffer, which treats every argument as an expression, but it shouldn't
do that, the arguments are (int, 1), and 'int' is not an expression. When
argsToCBuffer calls expToCBuffer on the int 'expression', it thinks the
expression has an operator precedence lower than PREC_assign, so it put's the
parantheses around the expr and produces '(int)'.

A workaround is to do 'e->op = TOK_assign' in TupleExp::TupleExp when pushing a
type to the tuple exps, but that's only a hack.

I will try to find a proper fix and also investigate into the other bugs that
mixed tuples have.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list