Only build stagedfunctions on leaf types (fix #8504)

I'm much less sure of this one, but it does fix #8504. @Keno? @jakebolewski?