An example of weird template error messaging

Ethan gooberman at gmail.com
Sun Aug 4 15:18:40 UTC 2019


Cropped up with my code, I was making a registry system in a 
generic manner so that I could reuse the code later down the line 
on other projects if necessary.

Did some minimal reproduction in run.dlang.io and determined that 
the problem comes down to template constraints. If you compile 
the code you currently get the following error message:

Error: template instance onlineapp.Template!(ObjectOneImpl, 
ObjectBase) is used as a type

Well that tells me nothing. So let's dig. If you delete 
ApplyRight and import std.meta : ApplyRight you instead get:

Error: template instance `SmartAlias!(Template!(ObjectOneImpl, 
ObjectBase))` recursive template expansion

Okay. That gives me more information, but doesn't actually tell 
me what the problem is.

So let's poke at code. If you delete the constraint on 
ObjectRefImpl, everything compiles and runs exactly like you'd 
expect.

Ah ha. So that's what its beef is. By inspecting the type it 
needs a complete representation of the type. The type includes an 
ObjectRefImpl instantiated with a type that includes an 
ObjectRefImpl to the current type. So checking the constraints 
gets in to a recursive loop.

I want to submit this as a bug, but there's like several 
different issues here that need identifying so that I can report 
it properly.

----------

template ApplyRight( alias Template, Right... )
{
     alias ApplyRight( Left... ) = Template!( Left, Right );
}

struct ObjectRefImpl( Type, BaseType ) if( is( Type Super == 
super ) && is( Super == BaseType ) )
{
	Type refval;
}

alias ObjRef = ApplyRight!( ObjectRefImpl, ObjectBase );

class ObjectBase
{
	string name;
}

alias ObjectOne = ObjRef!ObjectOneImpl;

class ObjectOneImpl : ObjectBase
{
	ObjectTwo two;
}

alias ObjectTwo = ObjRef!ObjectTwoImpl;

class ObjectTwoImpl : ObjectBase
{
	ObjectOne one;
}

void main()
{
	import std.stdio : writeln;

	ObjectOne one = { new ObjectOneImpl() };
	writeln( "Oh hi Mark" );
}



More information about the Digitalmars-d mailing list