WIP: Fixed #2079

This PR fixes issue #2079, but at the moment it is still work in progress.
I would convert that if to an assert. It should always be True. If s isn't an apply node with a scan op, there is a problem somewhere and we shouldn't hide it.