[Issue 9100] New: Weird behavior on template instance argument

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Nov 30 19:09:06 PST 2012


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

           Summary: Weird behavior on template instance argument
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: k.hara.pg at gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg at gmail.com> 2012-11-30 19:09:03 PST ---
On template instance argument, a template arguments *sometimes* is treated just
as a symbol.
That looks weird and inconsistent behavior.

----
template Id(alias A) { alias Id = A; }  // L1
template ErrId(alias A) { static assert(0); }
template TypeTuple(TL...) { alias TypeTuple = TL; }
class C {
  void fun(){}
  void tfun(T)(){}
  TypeTuple!(int, long) field;  // L7

  void test()
  {
    auto c = this;
    alias t1a = Id!(c.fun);         // OK
    alias t1b = Id!(this.fun);      // Weird error, bad
    // test1.d(1): Error: variable test1.A type void is inferred
    //   from initializer this.fun(), and variables cannot be of type void
    // -> internally given DotVarExp

    alias t2a = Id!(c.tfun);        // OK
    alias t2b = ErrId!(this.tfun);  // No error occurs, why?
    // -> internally given DotTemplateExp

    alias t3a = Id!(foo);           // OK
    alias t3b = Id!(mixin("foo"));  // Prints weird error, bad
    // test1.d(1): Error: variable test1.A type void is inferred
    //   from initializer foo(), and variables cannot be of type void
    // -> internally given TemplateExp

    alias t4b = TypeTuple!(field);      // L28, NG
    // test1.d(7): Error: expression this._field_field_0 is not a valid
template value argument
    // test1.d(7): Error: expression this._field_field_1 is not a valid
template value argument
    // test1.d(28): Error: template instance
test1.TypeTuple!(this._field_field_0, this._field_field_1) error instantiating
    alias t4a = TypeTuple!(GetField!0); // L32, NG
    // test1.d(42): Error: alias test1.GetField!(0).GetField cannot alias an
expression _field_field_0
    // test1.d(42): Error: alias test1.GetField!(0).GetField cannot alias an
expression _field_field_0
    // test1.d(32): Error: template instance test1.GetField!(0) error
instantiating
    // test1.d(42): Error: alias test1.GetField!(0).GetField cannot alias an
expression _field_field_0
    // test1.d(32): Error: C.field[0u] is not a type
  }
}

void foo()(){}
template GetField(size_t i) { alias GetField = C.field[i]; }  // L42

void main(){ (new C()).test(); }
----

>From the internal of dmd implementation, if failure to give just as a symbol,
the argument has been semantically analysed as an expression which refers a
symbol - DotVarExp, TemplateExp, and DotTemplteExp.

D does not support an aliasing of expression, so I think these *expression
template arguments* should be deduced to just a symbol. In such deduction, the
*context* expression for the symbol (e1, in DotVarExp or DotTemplateExp) should
be just removed.

It will increase consistency and will relax some limitations.

-- 
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