Fix Trac #4361: be more discerning when inferring types