fix #4505, scope of inner functions with static parameters

also incidentally fixes #4677