Trapping of magma at midcrustal density discontinuities