Bug (fixed in 1.20?) concerning func variables


I’ve discovered a bug which seems to have been fixed starting with version 1.20. However, the release notes don’t seem to mention anything similar, so I’d feel safer if someone could pinpoint it.

I’ve boiled it down to the following test. This test fails in version 1.19. However, uncommenting the first line of the function test() (which merely introduces two blank variables) makes the test pass (however, just introducing one blank variable is not enough). As I said, the test passes in version 1.20 regardless if the line is commented out or not. So what changed?


1 Like

This is a really interesting question that is driving me insane! In 1.19, when your empty struct variables line is commented out, test is inlined but the compiler is somehow missing/ignoring the a, b = b, a swap. I can’t tell how that’s happening, though! I tried compiling with GOSSAFUNC=main but even the AST at the beginning is already rewritten to inline the loop which drops the body of the second function!?

I highly recommend asking on Reddit. If you don’t, I will! I’d love to understand what’s happening here!

1 Like

Thanks! Inlining does seem to be the culprit somehow, doesn’t it? I had already considered asking Reddit; guess you helped me decide :slightly_smiling_face:

For the records: cmd/compile: incorrect inlining of function swapping two funcs · Issue #59108 · golang/go

From a comment on that issue:

The bug is introduced with 1.16, 1.15 still has both functions, starting with 1.16 to 1.19 the inlined b function is optimized away.

A backport of the fix to 1.19 is on the way.

Thank you @fabe-xx for moving this issue forward.

1 Like