Use local cache more often

Use local cache when there are unbound type variables and where clauses are in scope.

Fixes #18209.

r? @nick29581 
This is a novel new coding style - are you experimenting? :-)