Detect cycles in the toplevel's printer.

The patch attached to issue 6228.