Is it safe to swap a G40 barrel into a GEN4 G20?
Based on the LoneWolf website I would say yes:
https://lonewolfdist.com/barrels/glock-compatible-barrels/g20-21-40-41-full-size/lwd-barrel-m-20-40-10mm-9-length/
They are advertising a 9" that will fit in both, so you should be good to go.
Thank You
Just because that company's (LWD) product is, doesn't mean that Glock's is... My guess would be that is is, but I'd recommend running a caliper over where it would rest when it is in the lockup position and ensure that area is the same size as the original barrel.