MethodWriter state inside Label is incorrectly reused on second tree traversal
MethodWriter stores state and forward reference information in the Label
object, which means that a given Label instance can only be used once to
generate code. tree.LabelNode, however, keeps a reference to the generated
Label, which means that if a tree is traversed twice to generate two copies of
a method the second traversal will incorrectly reuse the Label state from the
first code writing.
This manifests as either incorrectly generated code or an
ArrayIndexOutOfBoundsException from Label.resolve(). The javadoc for
Label.resolve() says that it will throw an IllegalArgumentException if it is
called from a MethodWriter other than the one that put() it, but that check is
not actually performed.
Attached is a class that we are using to work around the problem. It wraps a
MethodWriter, providing a one-to-one mapping between existing Label-s passed to
it and fresh Label-s passed to its wrapped mv.
Thank you - Nathan Bronson