Backports for 9.2.2
Backport:
- 4ff4d434, fixing #20200 (closed) and easing later backports
- 590a2918, fixing #19790 (closed)
- !7625 (closed), fixing #21122 (closed)
- Various other scoping fixes that I found while debugging #21122 (closed)
Edited by Ben Gamari