fix Issue 12485 - [REG2.065] DMD crashes when recursive template expansion

Fix https://issues.dlang.org/show_bug.cgi?id=12485

Partial reversion of https://github.com/D-Programming-Language/dmd/pull/2982/files