From 8bbdba450980c2b0054e19ae737eeaa3cfdb452d Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Tue, 22 Feb 2022 21:29:18 -0500
Subject: [PATCH] simplCore: Correctly extend in-scope set in rule matching

Note [Matching lets] in GHC.Core.Rules claims the following:

> We use GHC.Core.Subst.substBind to freshen the binding, using an
> in-scope set that is the original in-scope variables plus the
> rs_bndrs (currently floated let-bindings).

However, previously the implementation didn't actually do extend the
in-scope set with rs_bndrs. This appears to be a regression which was
introduced by 4ff4d434e9a90623afce00b43e2a5a1ccbdb4c05. Reintroduce
`rs_bndrs` into the in-scope set, ensuring that let-binders cannot
shadow one another due to rule rewrites.

Fixes #21122.

(cherry picked from commit 0f7dc6701cfb6be59deb8601da8da000fe70263f)
---
 compiler/GHC/Core/Rules.hs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler/GHC/Core/Rules.hs b/compiler/GHC/Core/Rules.hs
index ccfbbe6a7819..96f9bece4c85 100644
--- a/compiler/GHC/Core/Rules.hs
+++ b/compiler/GHC/Core/Rules.hs
@@ -955,7 +955,7 @@ match renv subst e1 (Let bind e2) mco
   | otherwise
   = Nothing
   where
-    (flt_subst', bind') = substBind (rv_fltR renv) bind
+    (flt_subst', bind') = substBind (rv_fltR renv `extendInScopeList` rs_bndrs subst) bind
     new_bndrs           = bindersOf bind'
 
 ------------------------  Lambdas ---------------------
-- 
GitLab