Add a `memoized` function and use it throughout rustc

This can't be `&|&: &T|`?