D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2706 - invalid template instantiation (and declaration?) is not rejected
Summary: invalid template instantiation (and declaration?) is not rejected
Status: RESOLVED DUPLICATE of issue 2526
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2009-03-02 22:35 UTC by Tomas Lindquist Olsen
Modified: 2014-03-01 00:35 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomas Lindquist Olsen 2009-03-02 22:35:49 UTC
The following code doesn't make sense to me, yet DMD accepts it:

module k;

enum TYP { ch, c }

class TypeBasic {
  int oo;
  /// Allocates an instance of TypeBasic and assigns it to typeName.
  template newTB(char[] typeName)
  {
    const newTB = mixin("new TypeBasic(TYP."~typeName~")");
  }

  /// Initializes predefined types.
  static this()
  {
    newTB!("c");
    newTB!("ch");
  }
  this(int inp)
  {
  oo=inp;
  }
}

void main()
{
}



I got this from a LDC bug report in IRC, and TBH I can't think anything but WTF is going on here? DMD seems to emit two 'new' expressions in the static ctor.

I would think it should be:

error: 'new TypeBasic(TYP.c)' is not a constant expression
Comment 1 Tomas Lindquist Olsen 2009-03-02 22:37:34 UTC
Anoter test case:

template newTB(char[] a, char[] b)
{
    const newTB = mixin(a~"+"~b);
}

void main()
{
    int a, b;
    auto c = newTB!("a", "b");
}


Here the bogus error message:

Error: Array operations not implemented

is produced when it should be:

Error: a + b is not a constant expression.

or am I completely off here ?
Comment 2 Tomas Lindquist Olsen 2009-03-02 22:45:24 UTC
Ok the second one makes a little sense. but what about this one:

template newTB(char[] c, char[] d)
{
    const newTB = mixin(c~"+"~d);
}

int a=1, b=2;

void main()
{
    auto c = newTB!("a", "b");
    printf("%d\n", c);
    a = 4;
    b = 6;
    c = newTB!("a", "b");
    printf("%d\n", c);
}

extern(C) int printf(char*, ...);

/*
[tomas@myhost tests]$ dmd bar.d
[tomas@myhost tests]$ ./bar
3
10
*/

?
Comment 3 Don 2011-04-14 15:31:39 UTC
Updated test case for D2.052:
template newTB(string c, string d)
{
    enum newTB = mixin(c~"+"~d);
}

int a=1, b=2;

void main()
{
    auto c = newTB!("a", "b");
    a = 4;
    b = 6;
    c = newTB!("a", "b");
}
Comment 4 yebblies 2012-01-29 22:23:06 UTC

*** This issue has been marked as a duplicate of issue 2526 ***