bug: tmp variable redeclared in generated code
mirza-s opened this issue ยท 5 comments
Hey ๐, if we try to generate code from the following definitions:
struct Fruit {
uint64 ID;
string Name;
}
struct Basket {
Fruit Apples;
Fruit Oranges;
Fruit Mangos;
}
we get the following:
tmp := (bbp.Apples)
at += tmp.Size()
bbp.Oranges, err = MakeFruitFromBytes(buf[at:])
if err != nil {
return err
}
tmp := (bbp.Oranges)
at += tmp.Size()
where tmp
is redeclared each time a struct is referenced.
I think I can fix this one either by checking that tmp
is declared only once or by encapsulating tmp
usage inside a { block } which does not require additional state keeping but produces uglier code.
Where would be a nice place to put a unit test for this?
Cheers,
Mirza
That's a fairly basic input that I'm surprised isn't covered by existing tests; what version or commit hash are you on, to verify? I have time to patch this, I suspect.
#45 adds blocking around the variables; I'm open to a prettier solution, i.e. tracking if tmp
needs a :=
or =
per function, but that might require a more significant overhaul of code generation to add some state tracking to a few functions.
@mirza-s If you'd like to take a stab at outputting some nicer code, and are happy to wait (or use 0.4.0 instead which shouldn't present this problem), I can hold off on merging that branch; what do you think?
Thanks for the quick fix. The current solution is fine by me ๐