Commit 946f78a0 authored by Adam Klein's avatar Adam Klein Committed by Commit Bot

[parsing] Add a UseCounter for labeled expression statements

This was suggested by bmeurer after running into the confusing
example of:

  x => {x:x}

which might appear to be an arrow function that returns an object
literal containing its argument, but instead is an arrow function
that does nothing.

While it's unclear whether the language would change to make this
probable programmer error an actual syntax error, we can at least
gather some data on the question of whether we see any such code
in the wild.

Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: I08202039ecf7a7a4c71ad95ecd839436b4ec2af8
Reviewed-on: https://chromium-review.googlesource.com/600888
Commit-Queue: Adam Klein <adamk@chromium.org>
Reviewed-by: 's avatarBenedikt Meurer <bmeurer@chromium.org>
Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47171}
parent e7a46253
......@@ -6969,6 +6969,7 @@ class V8_EXPORT Isolate {
kAssigmentExpressionLHSIsCallInStrict = 37,
kPromiseConstructorReturnedUndefined = 38,
kConstructorNonUndefinedPrimitiveReturn = 39,
kLabeledExpressionStatement = 40,
// If you add new values here, you'll also need to update Chromium's:
// UseCounter.h, V8PerIsolateData.cpp, histograms.xml
......
......@@ -5086,6 +5086,11 @@ ParserBase<Impl>::ParseExpressionOrLabelledStatement(
// Parsed expression statement, followed by semicolon.
ExpectSemicolon(CHECK_OK);
if (labels != nullptr) {
// TODO(adamk): Also measure in the PreParser by passing something
// non-NULL as |labels|.
impl()->CountUsage(v8::Isolate::kLabeledExpressionStatement);
}
return factory()->NewExpressionStatement(expr, pos);
}
......
......@@ -115,3 +115,28 @@ TEST(AssigmentExpressionLHSIsCall) {
CHECK_NE(0, use_counts[v8::Isolate::kAssigmentExpressionLHSIsCallInStrict]);
use_counts[v8::Isolate::kAssigmentExpressionLHSIsCallInStrict] = 0;
}
TEST(LabeledExpressionStatement) {
v8::Isolate* isolate = CcTest::isolate();
v8::HandleScope scope(isolate);
LocalContext env;
int use_counts[v8::Isolate::kUseCounterFeatureCount] = {};
global_use_counts = use_counts;
CcTest::isolate()->SetUseCounterCallback(MockUseCounterCallback);
CompileRun("typeof a");
CHECK_EQ(0, use_counts[v8::Isolate::kLabeledExpressionStatement]);
CompileRun("foo: null");
CHECK_EQ(1, use_counts[v8::Isolate::kLabeledExpressionStatement]);
CompileRun("foo: bar: baz: undefined");
CHECK_EQ(2, use_counts[v8::Isolate::kLabeledExpressionStatement]);
CompileRun(
"foo: if (false);"
"bar: { }"
"baz: switch (false) { }"
"bat: do { } while (false);");
CHECK_EQ(2, use_counts[v8::Isolate::kLabeledExpressionStatement]);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment