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

local static inside switch is not static enough #11800

Closed
Antriel opened this issue Oct 23, 2024 · 3 comments
Closed

local static inside switch is not static enough #11800

Antriel opened this issue Oct 23, 2024 · 3 comments
Assignees

Comments

@Antriel
Copy link
Contributor

Antriel commented Oct 23, 2024

function main() {
	static var a = 0; // Works.
	for (i in 0...3) {
		switch i {
			case n if (n < 2):
				trace(++a);
				static var b = 0; // Not static.
				trace(++b); // Always `1`.
			case _:
		}
	}
}
@Simn
Copy link
Member

Simn commented Oct 24, 2024

Turns out our variable duplication doesn't copy the flags, argh...

Interestingly, this particular issue runs into a separate issue when we fix that:

 ERROR  source/Main.hx:9: characters 16-17

 9 |     static var b = 0; // Not static.
   |                ^
   | The expanded name of this local (main_b) conflicts with another static field

      | Conflicting field was found here

It took me a second to understand that the issue is the for (i in 0...3) because that unrolls the loop and thus duplicates the code, so I think we should disable loop unrolling when there are static variables in the body.

@Simn Simn added this to the 4.3 Hotfix candidates milestone Oct 24, 2024
@Simn Simn self-assigned this Oct 24, 2024
Simn added a commit that referenced this issue Oct 24, 2024
Also don't unroll loops that have static vars
closes #11800
@tobil4sk
Copy link
Member

I think we should disable loop unrolling when there are static variables in the body.

Would there be any issue with simply hoisting the variable declaration out of the loop? It's confusing when a feature silently disables an optimisation like this.

@Simn
Copy link
Member

Simn commented Oct 25, 2024

Hoisting changes the scope of a local variable, but that should be fine here because it still can only be accessed in its original scope as far as typing is concerned. Unrolling also happens before our local variable renaming step, so this should indeed be possible.

Simn added a commit that referenced this issue Oct 25, 2024
@Simn Simn closed this as completed in 25d36a6 Oct 25, 2024
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

3 participants