Thread overview
unittest + struct ctorю + nested mixin template + alias
May 07, 2013
ref2401
May 07, 2013
ref2401
May 07, 2013
Version D 2.062

class MyClass
{}

struct MyStruct(T)
if (is(T == class))
{
	string _message = "nothing";

	this(T obj)
	{
		if (obj is null){
			_message = "Null reference message";
		}
		else{
			_message = "All right message";
		}
	}

	mixin Foo!();

	private mixin template Foo()
	{
		void foo() {}
	}
}


unittest
{
	alias MyStruct!MyClass StructType;
	auto inst = StructType(null);

	string msg = inst._message;		// Wrong value!
}


The behavior varies between executions: sometimes msg contains empty string, in other times it holds "nothing".
If StructType is replaced with MyStruct!MyClass then everything works as expected.
	auto inst = MyStruct!MyClass(null);	// OK

If declaration of the Foo template is moved out of struct MyStruct(T) definition, everything works fine too.
This strange behavior is observed only in unittest block, not when executed in main().
Am I doing something incorrectly or is this a bug?
May 07, 2013
On Tue, 07 May 2013 10:58:09 -0400, ref2401 <refactor24@gmail.com> wrote:

> Version D 2.062
>
> class MyClass
> {}
>
> struct MyStruct(T)
> if (is(T == class))
> {
> 	string _message = "nothing";
>
> 	this(T obj)
> 	{
> 		if (obj is null){
> 			_message = "Null reference message";
> 		}
> 		else{
> 			_message = "All right message";
> 		}
> 	}
>
> 	mixin Foo!();
>
> 	private mixin template Foo()
> 	{
> 		void foo() {}
> 	}
> }
>
>
> unittest
> {
> 	alias MyStruct!MyClass StructType;
> 	auto inst = StructType(null);
>
> 	string msg = inst._message;		// Wrong value!
> }
>
>
> The behavior varies between executions: sometimes msg contains empty string, in other times it holds "nothing".
> If StructType is replaced with MyStruct!MyClass then everything works as expected.
> 	auto inst = MyStruct!MyClass(null);	// OK
>
> If declaration of the Foo template is moved out of struct MyStruct(T) definition, everything works fine too.
> This strange behavior is observed only in unittest block, not when executed in main().
> Am I doing something incorrectly or is this a bug?

How are you determining that _message is wrong?  I see no observable printouts or anything.

It may be that if you are using a debugger, the debugger can't handle the mixin and is looking at the wrong data, or maybe the compiler is outputting wrong debug info.

If I put this line in, it never asserts:

assert(msg == "Null reference message");

I think the compiler is outputting correct code.  I don't have 2.062, only the beta, and 2.061.  Tested with the latest beta.

-Steve
May 07, 2013
i'm using VisualD.

this assertion fails assert(msg == "Null reference message");
in my actual code instead of variable _message an exception is thrown if (obj is null) == true.
i'm using the unittest block to test exception throwing.

...
	this(T obj)
	{
		if (obj is null){
			throw new Exception("Null reference.");
		}
	}
...


unittest
{
	alias MyStruct!MyClass StructType;
	std.exception.assertThrown(StructType(null));	// Wrong assertion
}