BUG: downcasting is now more robust (related GH5174)

related #5174