Fix non-termination bug in class resolution

Record the classes we've resolved, and reduce the depth when we
encounter any; not just if it's we're resolving a class that's the same
as the parent.

Fixes #931