This backports the commits from !4826 (closed) to the ghc-9.2 branch. Fixes #19042 (closed) for good.
ghc-9.2