rubinius/rubinius — pull request #1826 by inukshuk (2012-07-24 07:38:56+00:00)
Title: Support non-tIVAR attribute names in Struct._specialize

Currently, Struct._specialize does not distinguish between attribute names that are valid tIVAR tokens and can therefore be written as normal ivar references and optimized and names which are not. To protect against invalid code SyntaxErrors are caught and ignored when the generated code is included into the class. There are two issues because of this:

* A single attribute name that cannot be written as a syntactically correct ivar reference will cause the entire struct not to use the specialized code.

* There are some names that, when written as ivar references, will produce syntactically correct code which will either fail at runtime or lead to unexpected results.

Especially the latter is problematic, because it means that it is possible to generate Struct classes whose instance objects will be unusable. A good example are hyphenated attribute names:

Struct.new(:'foo-bar')

Will produce the following constructor code:

@foo-bar = a0

Another good example is a name starting with '@':

Struct.new(:'@foo')

Which will produce something like:

@@foo = a0

in the constructor.

This commit tries to address both issues by differentiating between names which look like tIVAR tokens and names which do not. The generated code will use ivar references for the former and `instance_variable_(get|set)` for the latter.

Please see the [original pull request](https://github.com/rubinius/rubinius/pull/1811) for more details. There was no more feedback so I'm opening this as a new pull request – I hope that's OK.