Closed Bug 348685 Opened 18 years ago Closed 18 years ago

Let-scoped variables in for loop can be referenced outside the loop

Categories

(Core :: JavaScript Engine, defect, P1)

defect

Tracking

()

VERIFIED FIXED
mozilla1.8.1beta2

People

(Reporter: nanto, Assigned: brendan)

Details

(Keywords: verified1.8.1)

Attachments

(1 file, 1 obsolete file)

Using let-scoped for loop variables outside the loop doesn't throw a ReferenceError exception under some conditions:

- In a function
- Repeat the loop more than one time
- Use let declarations/statements/expressions in the loop body
- Define functions or use function expressions in the loop body

And the variable is evalueated to:

- Last assigned value in the for loop
- A function that uses the variable as a argument
- Undefined if the variable is used as a argument and returned

js> function f() {
  for (let i = 0; i < 2; i++) { let j = 42; function g() {} }
  var a = i;
  print(a);
  print(i);
  return i;
}
js> print(f());
2
function print() {
    [native code]
}
undefined
Argh -- once we clone block objects and clear fp->blockChain, we must never again set it.  Otherwise, we end up with duplicate clones on fp->scopeChain after the second iteration!

/be
Assignee: general → brendan
Status: NEW → ASSIGNED
OS: Windows XP → All
Priority: -- → P1
Hardware: PC → All
Target Milestone: --- → mozilla1.8.1beta2
Attached patch fix (obsolete) — Splinter Review
Comment says it all.

/be
Attachment #233716 - Flags: review?(mrbkap)
If we kept count of cloned blocks on the scope chain, or counted remaining ones on every LEAVEBLOCK, we could clear JSFRAME_POP_BLOCKS and do better for cases that open new, unrelated blocks later, after a block contains a closure or other scope capturing construct.  Not sure this is worth optimizing.  We could optimize nested functions harder to avoid capturing run-time scope, too.

/be
Attachment #233716 - Attachment is obsolete: true
Attachment #233719 - Flags: review?(mrbkap)
Attachment #233716 - Flags: review?(mrbkap)
Comment on attachment 233719 [details] [diff] [review]
with correct assertion

Seems reasonable.
Attachment #233719 - Flags: review?(mrbkap) → review+
Fixed on trunk, already.

/be
Status: ASSIGNED → RESOLVED
Closed: 18 years ago
Flags: blocking1.8.1?
Resolution: --- → FIXED
Checking in regress-348685.js;
/cvsroot/mozilla/js/tests/js1_7/block/regress-348685.js,v  <--  regress-348685.js
initial revision: 1.1
Flags: in-testsuite+
Flags: blocking1.8.1? → blocking1.8.1+
Comment on attachment 233719 [details] [diff] [review]
with correct assertion

This is ready.

/be
Attachment #233719 - Flags: approval1.8.1?
Comment on attachment 233719 [details] [diff] [review]
with correct assertion

a=beltzner, on behalf of drivers for the 18branch
Attachment #233719 - Flags: approval1.8.1? → approval1.8.1+
Fixed on the 1.8 branch.

/be
Keywords: fixed1.8.1
verified fixed 1.8 20060818 windows/mac*/linux
not verified trunk crashed opt/dbg all platforms. I'll deal when I have fresh builds.
verified fixed 1.9 20060824 windows/mac*/linux
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: