It seems to segfault in the op getlex when trying to locate &foo. Moving sub foo to within the BEGIN block helps and moving it outside of A gives: Cannot invoke this object in method <anon> at lib/A.pm6:5
Neither the rakudo optimizer nor spesh seem to be to blame.