Fix ICE assigning methods to local variables

This might need a rebase if #18177 goes in first.

Closes #18412