Bug 1496331 - Do not alter declaration kind from FormalParameter to BodyLevelFunction. r=Yoric
authorTooru Fujisawa <arai_a@mac.com>
Mon, 15 Oct 2018 23:51:50 +0900
changeset 489565 a298e5b921f7dde9e3e36f25fae60a950e94a20b
parent 489564 c3c3a9571f6306dd0df49e779977f0ee45a37c8e
child 489566 3293a3393b4a68e942b7f8fd5f2dd77b337b8676
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersYoric
bugs1496331
milestone64.0a1
Bug 1496331 - Do not alter declaration kind from FormalParameter to BodyLevelFunction. r=Yoric
js/src/frontend/BinSource.cpp
js/src/frontend/Parser.cpp
js/src/frontend/Parser.h
--- a/js/src/frontend/BinSource.cpp
+++ b/js/src/frontend/BinSource.cpp
@@ -237,17 +237,23 @@ BinASTParser<Tok>::buildFunctionBox(Gene
     RootedAtom atom(cx_);
     if (name) {
         atom = name->name();
     }
 
     if (parseContext_ && syntax == FunctionSyntaxKind::Statement) {
         auto ptr = parseContext_->varScope().lookupDeclaredName(atom);
         MOZ_ASSERT(ptr);
-        ptr->value()->alterKind(DeclarationKind::BodyLevelFunction);
+        // FIXME: Should be merged with ParseContext::tryDeclareVarHelper
+        //        (bug 1499044).
+        DeclarationKind declaredKind = ptr->value()->kind();
+        if (DeclarationKindIsVar(declaredKind)) {
+            MOZ_ASSERT(declaredKind != DeclarationKind::VarForAnnexBLexicalFunction);
+            ptr->value()->alterKind(DeclarationKind::BodyLevelFunction);
+        }
     }
 
     // Allocate the function before walking down the tree.
     RootedFunction fun(cx_);
     BINJS_TRY_VAR(fun, !parseContext_
         ? lazyScript_->functionNonDelazifying()
         : AllocNewFunction(cx_, atom, syntax, generatorKind, functionAsyncKind, nullptr));
     MOZ_ASSERT_IF(parseContext_, fun->explicitName() == atom);
--- a/js/src/frontend/Parser.cpp
+++ b/js/src/frontend/Parser.cpp
@@ -1392,17 +1392,17 @@ PerHandlerParser<ParseHandler>::noteDest
         ReportOutOfMemory(context);
         return false;
     }
 
     handler.addFunctionFormalParameter(funNode, destruct);
     return true;
 }
 
-static bool
+bool
 DeclarationKindIsVar(DeclarationKind kind)
 {
     return kind == DeclarationKind::Var ||
            kind == DeclarationKind::BodyLevelFunction ||
            kind == DeclarationKind::VarForAnnexBLexicalFunction ||
            kind == DeclarationKind::ForOfVar;
 }
 
--- a/js/src/frontend/Parser.h
+++ b/js/src/frontend/Parser.h
@@ -1695,12 +1695,16 @@ mozilla::Maybe<VarScope::Data*>
 NewVarScopeData(JSContext* context, ParseContext::Scope& scope, LifoAlloc& alloc, ParseContext* pc);
 mozilla::Maybe<LexicalScope::Data*>
 NewLexicalScopeData(JSContext* context, ParseContext::Scope& scope, LifoAlloc& alloc, ParseContext* pc);
 
 JSFunction*
 AllocNewFunction(JSContext* cx, HandleAtom atom, FunctionSyntaxKind kind, GeneratorKind generatorKind, FunctionAsyncKind asyncKind,
                  HandleObject proto, bool isSelfHosting = false, bool inFunctionBox = false);
 
+// Returns true if the declaration is `var` or equivalent.
+bool
+DeclarationKindIsVar(DeclarationKind kind);
+
 } /* namespace frontend */
 } /* namespace js */
 
 #endif /* frontend_Parser_h */