Fix to rewire types

t6574 has a new test where we produce identical code in an if-then-else. This broke
the rewiring logic before, and is fixed now.

Also, more comments and test cases.