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

Less local variable renaming in compiled code #9304

Conversation

RealyUniqueName
Copy link
Member

See #9035
Closes #9296

Finally I've managed to implement something bearable.

This takes into account target rules for local variables scoping.
For example this sample:

var tmp = 0;
var x = 1;
tmp = x;
var x = 2;
tmp = x;
var x = 3;
tmp = x;

when compiled for js:

var tmp = 0;
var x = 1;
tmp = x;
var x = 2;
tmp = x;
var x = 3;
tmp = x;

to cs (no shadowing allowed):

int tmp = 0;
int x = 1;
tmp = x;
int x1 = 2;
tmp = x1;
int x2 = 3;
tmp = x2;

In conjunction with #9280 this will reduce amount of local variables generated for JS even more.

@RealyUniqueName RealyUniqueName added this to the Release 4.1 milestone Apr 10, 2020
@nadako
Copy link
Member

nadako commented Apr 10, 2020

In conjunction with #9280 this will reduce amount of local variables generated for JS even more.

Wouldn't that re-introduce numbering though? Because you can't do let x in the same block multiple times.

@RealyUniqueName
Copy link
Member Author

Wouldn't that re-introduce numbering though? Because you can't do let x in the same block multiple times.

Indeed.
Well, at least you won't see variables from sibling blocks in debugger )

@RealyUniqueName
Copy link
Member Author

numbering

Actually, that depends on a user code.
E.g. this

var x = 123;
if(condition) {
  var x = 'hello';
  trace(x);
}
trace(x);

with #9280 will be compiled to

let x = 123;
if(condition) {
  let x = "hello";
  console.log(x);
}
console.log(x);

while without #9280 it will be

var x = 123;
if(condition) {
  var x1 = "hello";
  console.log(x1);
}
console.log(x);

@nadako
Copy link
Member

nadako commented Apr 10, 2020

Yes, and that's probably a more real-world case, so go for it :) It's just that the original example with same-block shadowing will not really be improved by let :)

@skial skial mentioned this pull request Apr 12, 2020
1 task
@RealyUniqueName RealyUniqueName merged commit de834cf into HaxeFoundation:development Apr 13, 2020
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

Successfully merging this pull request may close these issues.

Wrong var shadowing
2 participants