Cherry pick traincascade compilation fixes from master to 2.4

Previously discussed in PR #1774, this is two relevant commits cherry picked from master since the original submission didn't merge cleanly onto 2.4.