v8/test/mjsunit/es6/for-each-in-catch.js
Mathias Bynens b645a259bd [parser] Allow try {} catch (e) { for (var e of x) {} }
This patch changes the parser to allow for-of initializer
var-redeclaration of non-destructured catch parameters.

Previously, the spec allowed var-redeclaration of a
non-destructured catch parameter…

    try {} catch (e) { var e; }

…except in the particular case where the var declaration is
a for-of initializer:

    try {} catch (e) { for (var e of whatever) {} }

https://github.com/tc39/ecma262/pull/1393 removes this strange
exceptional case. This patch implements that change.

BUG=v8:8759

Change-Id: Ia4e33ac1eab89085f8a5fdb547f479cfa38bbee5
Reviewed-on: https://chromium-review.googlesource.com/c/1444954
Reviewed-by: Toon Verwaest <verwaest@chromium.org>
Commit-Queue: Mathias Bynens <mathias@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59209}
2019-01-30 14:20:58 +00:00

197 lines
3.4 KiB
JavaScript

// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function checkIsRedeclarationError(code) {
try {
eval(`
checkIsRedeclarationError: {
break checkIsRedeclarationError;
${code}
}
`);
assertUnreachable();
} catch (e) {
assertInstanceof(e, SyntaxError);
assertTrue(e.toString().includes("has already been declared"));
}
}
function checkIsNotRedeclarationError(code) {
assertDoesNotThrow(() => eval(`
checkIsNotRedeclarationError_label: {
break checkIsNotRedeclarationError_label;
${code}
}
`));
}
let var_e = [
'var e',
'var {e}',
'var {f, e}',
'var [e]',
'var {f:e}',
'var [[[], e]]'
];
let not_var_e = [
'var f',
'var {}',
'var {e:f}',
'e',
'{e}',
'let e',
'const e',
'let {e}',
'const {e}',
'let [e]',
'const [e]',
'let {f:e}',
'const {f:e}'
];
// Check that both `for (var ... of ...)` and `for (var ... in ...)`
// can redeclare a simple catch variable.
for (let binding of var_e) {
checkIsNotRedeclarationError(`
try {
throw 0;
} catch (e) {
for (${binding} of []);
}
`);
checkIsNotRedeclarationError(`
try {
throw 0;
} catch (e) {
for (${binding} in []);
}
`);
}
// Check that the above applies even for nested catches.
for (let binding of var_e) {
checkIsNotRedeclarationError(`
try {
throw 0;
} catch (e) {
try {
throw 1;
} catch (f) {
try {
throw 2;
} catch ({}) {
for (${binding} of []);
}
}
}
`);
checkIsNotRedeclarationError(`
try {
throw 0;
} catch (e) {
try {
throw 1;
} catch (f) {
try {
throw 2;
} catch ({}) {
for (${binding} in []);
}
}
}
`);
}
// Check that the above applies if a declaration scope is between the
// catch and the loop.
for (let binding of var_e) {
checkIsNotRedeclarationError(`
try {
throw 0;
} catch (e) {
(()=>{for (${binding} of []);})();
}
`);
checkIsNotRedeclarationError(`
try {
throw 0;
} catch (e) {
(function() {
for (${binding} of []);
})();
}
`);
}
// Check that there is no error when not declaring a var named e.
for (let binding of not_var_e) {
checkIsNotRedeclarationError(`
try {
throw 0;
} catch (e) {
for (${binding} of []);
}
`);
}
// Check that there is an error for both for-in and for-of when redeclaring
// a non-simple catch parameter.
for (let binding of var_e) {
checkIsRedeclarationError(`
try {
throw 0;
} catch ({e}) {
for (${binding} of []);
}
`);
checkIsRedeclarationError(`
try {
throw 0;
} catch ({e}) {
for (${binding} in []);
}
`);
}
// Check that the above error occurs even for nested catches.
for (let binding of var_e) {
checkIsRedeclarationError(`
try {
throw 0;
} catch ({e}) {
try {
throw 1;
} catch (f) {
try {
throw 2;
} catch ({}) {
for (${binding} of []);
}
}
}
`);
checkIsRedeclarationError(`
try {
throw 0;
} catch ({e}) {
try {
throw 1;
} catch (f) {
try {
throw 2;
} catch ({}) {
for (${binding} in []);
}
}
}
`);
}