Even simpler test case.
--
template <int N> class X {} ;
template <long P> void foo(X<P>) ; // this is the killer line
template <int N> void bar(X<N>) {return;};
int main() {
X<2> x;
bar(x);
}
--
Note that deleting the killer line or moving it below the other template
makes everything work.
This is really, really, broken.