Use local cache more often

Use local cache when there are unbound type variables and where clauses are in scope.

Fixes #18209.

r? @nick29581 
Somewhat. I just found it more readable that way.