Handle conflicting import of items declared in the same module

Fixes #19498
Is this missing some cases - sorry to be vague, but it seems like you are only catching reflexive loops, but any loop could cause this issue (is that right?)