Better type inference in SubArray construction

Fixes #5100