Fix a long standing bug in constraint solving (forward-port)
This is a forward-port of !4950 (closed) for master.
@simonpj @rae I assume that this should go in, even though deriveds will be removed? The testcase from #19315 (closed) does not reproduce the problem in master, but we don't have anything better.
Edited by Krzysztof Gogolewski