19674, "twesterhout", "Array type deduction results in a function being called twice ", "2022-04-20T19:23:45Z"
Summary of Problem
In the following example, the function enumerateStatesFixedHamming_v2 is called twice, but it should only be called once. Removing return type annotation in localEnumerateRepresentatives_v2 fixes the problem. Storing return value in a variable first looks like another workaround.
Maybe that'll provide some clues to others who know what is happening underneath the hood.
Thomas Rolinger:
From what I can tell, the compiler sees that you have a partially generic array return, so it inserts a call to chpl__checkRetEltTypeMatch, which will try to validate the return type by calling the function. Not sure if that is the intended behavior there
Elliot Ronaghan:
Yeah, there is some code to determine the shape of arrays, but I would hope we'd eliminate that code and not call a function twice at execution time.
Thomas Rolinger:
Digging a bit deeper since this is kinda of interesting: when it builds up the call to chpl__checkRetEltTypeMatch, it passes in the declared return type, which is the uint(64) stuff and it passes in the actual return type, which is (according to the compiler) the call to enumerateStatesFixedHamming_v2. So when chpl__checkRetEltTypeMatch is resolved/called, its first argument is a call itself, which ends up calling the function. I imagine this would go away if you stored the return to a variable and then returned that variable instead. EDIT: yes, that does seem to work and will only call the function once.
Also pinging @thomasrolinger and @ronawho who participated in the discussion on Gitter.