Could you give repro instructions that don't require stack? I want to repro with a private build of HEAD, and I know how to make cabal do that -- but not stack. Thanks
Aha. This turns out to be a rather embarrassing bug in dmdAnalBindLetUp.
Here's a trivial fix:
diff --git a/compiler/GHC/Core/Opt/DmdAnal.hs b/compiler/GHC/Core/Opt/DmdAnal.hsindex 3738f8b3ed..0dddeeba87 100644--- a/compiler/GHC/Core/Opt/DmdAnal.hs+++ b/compiler/GHC/Core/Opt/DmdAnal.hs@@ -333,7 +333,8 @@ dmdAnalBindLetUp :: TopLevelFlag -> WithDmdType (DmdResult CoreBind a) dmdAnalBindLetUp top_lvl env id rhs anal_body = WithDmdType final_ty (R (NonRec id' rhs') (body')) where- WithDmdType body_ty body' = anal_body env+ body_env = env { ae_sigs = ae_sigs env `delVarEnv` id } -- The new `id` shadows anything in ae_sigs+ WithDmdType body_ty body' = anal_body body_env WithDmdType body_ty' id_dmd = findBndrDmd env body_ty id -- See Note [Finalising boxity for demand signatures]
@sgraf812 could you spare the time to make this into a MR and land it?
It is not easy to cook up a test case, becuase the bug only manifest if there is some shadowing going on, and that is difficult to introduce deliberately. So maybe we'll have to do without a test.
@wkoiking thank you so much for a great repro case. Made it easy to find.
Another instance where the GHC plugin idea from #17478 would have been useful. I think it would even have prevented this bug, because we'd have seen it much earlier.
Here's the MR, fixing other lurking shadowing issues, too: !9659 (closed)
commit e3fff7512bbf989386faaa1dccafdad1deabde84Author: Sebastian Graf <sebastian.graf@kit.edu>Date: Mon Jan 9 09:26:36 2023 +0100 Handle shadowing in DmdAnal (#22718) Previously, when we had a shadowing situation like ```hs f x = ... -- demand signature <1L><1L> main = ... \f -> f 1 ... ``` we'd happily use the shadowed demand signature at the call site inside the lambda. Of course, that's wrong and solution is simply to remove the demand signature from the `AnalEnv` when we enter the lambda. This patch does so for all binding constructs Core. In #22718 the issue was caused by LetUp not shadowing away the existing demand signature for the let binder in the let body. The resulting absent error is fickle to reproduce; hence no reproduction test case. #17478 would help. Fixes #22718. It appears that TcPlugin_Rewrite regresses by ~40% on Darwin. It is likely that DmdAnal was exploiting ill-scoped analysis results. Metric increase ['bytes allocated'] (test_env=x86_64-darwin-validate): TcPlugin_Rewrite compiler/GHC/Core/Opt/DmdAnal.hs | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-)