Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler runs out of TempIdentifiers when using object initializers in array constructor (not released properly) #406

Closed
thomaswp opened this issue Dec 27, 2022 · 0 comments

Comments

@thomaswp
Copy link

I'm hitting a this error where the transpiler runs out of temp variables. Specifically, when I use object initializers inside of an array, the objects are created using temp variables. However, they are not released afterwards, so if I do this repeatedly in one function, the transpiler runs out of temp variables, even though I never use more than 17 in one list.

To reproduce:

    internal class BugTest
    {
        public int x;

        static void BreakStuff()
        {
            // First list with just 10 constructors
            var list1 = new BugTest[]
            {
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
            };
            // First list with 10 more
            var list2 = new BugTest[]
            {
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
                new BugTest() { x = 1 },
            };
        }
    }

It seems like the issue is here where the temp variables are bound to a function, instead of a scope, so you get 17 total for the whole function, even when you could reasonable reuse them. In the example above, for example, the first 10 temp variables go out of scope as soon as the first list is constructed, and they could be reused in the second. However, I don't hit this issue with List constructors (they release each temp variable after adding it to the list, so only 1 temp is used), so I'm guessing you have a workaround already that just needs to be extended to arrays.

Further, I'm not sure there any reason that temp variables have to be limited. Why not generate them dynamically with a prefix, like __temp__1, __temp__2, __temp__3, ... etc.? I suppose someone might happen to use that variable name, but in that case you could give the error, rather than to anyone using more than 17 temp variables.

Another solution for object initializers in particular would be to create a function that takes in an object and a table of key-value pairs and initializes the object to those values, then returns the object. Then you could inline the object initializer, rather than using a temp variable at all, e.g.:

list:Add(__initialize(class(), {'x': 1}))

Thanks as always for your work - this is a great tool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant