Reviewers: wingo,

Description:
Do not create unresolved variables when parsing arrow functions lazily

Arrow function parameter lists are parsed as expressions. When an identifier
is found a VariableProxy is created and added to the list of unresolved
variables for the scope. When parsing a function lazily, the scope has been
already resolved, so with this patch only the VariableProxy is created,
without adding it as an unresolved variable in the scope.

BUG=v8:3501
LOG=Y

Please review this at https://codereview.chromium.org/880253004/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+23, -6 lines):
  M src/parser.h
  M src/parser.cc
  A + test/mjsunit/regress/regress-3501.js


Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index dff7de9e6b25c9c9370c866ab5bd4f52646852a6..0c0fe36f1b9635a772a0251ade4a29dfdf0afbd8 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -723,7 +723,14 @@ Expression* ParserTraits::ExpressionFromIdentifier(const AstRawString* name,
     PrintF("# Variable %.*s ", name->length(), name->raw_data());
 #endif
   Interface* interface = Interface::NewUnknown(parser_->zone());
-  return scope->NewUnresolved(factory, name, interface, pos);
+
+  // Arrow function parameters are parsed as an expression. When
+  // parsing lazily, it is enough to create a VariableProxy in order
+  // for Traits::DeclareArrowParametersFromExpression() to be able to
+  // pick the names of the parameters.
+  return parser_->parsing_lazy_arrow_parameters_
+      ? factory->NewVariableProxy(name, false, interface, pos)
+      : scope->NewUnresolved(factory, name, interface, pos);
 }


@@ -788,6 +795,7 @@ Parser::Parser(CompilationInfo* info, ParseInfo* parse_info)
       target_stack_(NULL),
       cached_parse_data_(NULL),
       info_(info),
+      parsing_lazy_arrow_parameters_(false),
       has_pending_error_(false),
       pending_error_message_(NULL),
       pending_error_arg_(NULL),
@@ -1061,6 +1069,10 @@ FunctionLiteral* Parser::ParseLazy(Utf16CharacterStream* source) {
     bool ok = true;

     if (shared_info->is_arrow()) {
+ // The first expression being parsed is the parameter list of the arrow
+      // function. Setting this avoids prevents ExpressionFromIdentifier()
+      // from creating unresolved variables in already-resolved scopes.
+      parsing_lazy_arrow_parameters_ = true;
       Expression* expression = ParseExpression(false, &ok);
       DCHECK(expression->IsFunctionLiteral());
       result = expression->AsFunctionLiteral();
@@ -3341,6 +3353,10 @@ int ParserTraits::DeclareArrowParametersFromExpression(
     Expression* expression, Scope* scope, Scanner::Location* dupe_loc,
     bool* ok) {
   int num_params = 0;
+ // Always reset the flag: It only needs to be set for the first expression + // parsed as arrow function parameter list, becauseonly top-level functions
+  // are parsed lazily.
+  parser_->parsing_lazy_arrow_parameters_ = false;
   *ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params,
                                       dupe_loc);
   return num_params;
Index: src/parser.h
diff --git a/src/parser.h b/src/parser.h
index fe3a9a85225dace6f78806d52ea65acd8b8d55ca..2d248b14abb194343de5b7756b54fde447e5efa9 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -885,6 +885,7 @@ class Parser : public ParserBase<ParserTraits> {
   ParseData* cached_parse_data_;

   CompilationInfo* info_;
+ bool parsing_lazy_arrow_parameters_; // for lazily parsed arrow functions.

   // Pending errors.
   bool has_pending_error_;
Index: test/mjsunit/regress/regress-3501.js
diff --git a/test/mjsunit/compiler/regress-445732.js b/test/mjsunit/regress/regress-3501.js
similarity index 51%
copy from test/mjsunit/compiler/regress-445732.js
copy to test/mjsunit/regress/regress-3501.js
index 199a29a5f859dbe2e5d63c3cc6cfa06a76b0be99..4b449e458f545b5758a8a3ca579be1913becc29a 100644
--- a/test/mjsunit/compiler/regress-445732.js
+++ b/test/mjsunit/regress/regress-3501.js
@@ -2,10 +2,10 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.

-// Flags: --allow-natives-syntax --turbo-asm
+// Flags: --harmony-arrow-functions

-"use asm";
+// See: http://code.google.com/p/v8/issues/detail?id=3501

-%NeverOptimizeFunction(f);
-function f() { }
-f();
+"use strict";
+let lift = f => (x, k) => k (f (x));
+lift(isNaN);


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to