Fix handling of range selects on loop variables, fixes #1372