-idUnfoldingVars id
- = case idUnfolding id of
- CoreUnfolding { uf_tmpl = rhs, uf_guidance = InlineRule {} }
- -> exprFreeVars rhs
- DFunUnfolding _ args -> exprsFreeVars args
- _ -> emptyVarSet
+-- and we'll get exponential behaviour if we look at both unf and rhs!
+-- But do look at the *real* unfolding, even for loop breakers, else
+-- we might get out-of-scope variables
+idUnfoldingVars id = stableUnfoldingVars (realIdUnfolding id)
+
+stableUnfoldingVars :: Unfolding -> VarSet
+stableUnfoldingVars (CoreUnfolding { uf_tmpl = rhs, uf_src = src })
+ | isStableSource src = exprFreeVars rhs
+stableUnfoldingVars (DFunUnfolding _ _ args) = exprsFreeVars (dfunArgExprs args)
+stableUnfoldingVars _ = emptyVarSet