Skip to content

transform-es2015-parameters: arguments deoptimized when shadowed in nested function #5656

Closed

Description

@jpommerening

This is a feature request (I think). Arguments are not optimized if an inner function shadows the name with a parameter (or rest parameters in my case).

Input Code

const log = (...args) => console.log(...args);

function test_opt(...args) {
  log(...args);
}

function test_deopt(...args) {
  const fn = (...args) => log(...args);
  fn(...args);
}

Babel Configuration (.babelrc, package.json, cli command)

{
  "presets": [ "es2015" ]
}

Expected vs. Current Behavior

I'd expect the code to be optimizable to use .apply( thisArg, arguments ) throughout.
However, in test_deopt the outer ...args gets copied just to be passed into the inner fn.
I can verify that the problem disappears if I rename either the ...args of test_deopt or of the fn arrow function.

Environment

software version(s)
Babel 6.24.1
node 6.9.5
npm N/A (yarn 0.20.3)
Operating System macOS

Activity

  1. babel-bot commented on Apr 21, 2017

    @babel-bot
    Collaborator

    Hey @jpommerening! We really appreciate you taking the time to report an issue. The collaborators
    on this project attempt to help as many people as possible, but we're a limited number of volunteers,
    so it's possible this won't be addressed swiftly.

    If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
    community that typically always has someone willing to help. You can sign-up here
    for an invite.

  2. Qantas94Heavy commented on May 9, 2017

    @Qantas94Heavy
    Member

    I hope I'm not being stupid, but is this really meant to be "expected"?

    Test code:

    var y = function (foo, ...bar) {
      var x = function z(bar) {
        bar[1] = 5;
      };
    };

    Current "expected" result:

    var y = function (foo) {
      for (var _len2 = arguments.length, bar = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
        bar[_key2 - 1] = arguments[_key2];
      }
    
      var x = function z(bar) {
        bar[1] = 5;
      };
    };

    I would have thought that given the inner bar shadows the outer rest bar, we wouldn't need to copy the arguments.

  3. jpommerening commented on May 9, 2017

    @jpommerening
    Author

    Hey @Qantas94Heavy, thanks for pointing that out!
    I was under the impression that this was merely an oversight / missing optimization. I didn't know there was actually a test verifying the (arguably) incorrect behavior. I'm as confused as you are that the transform is expected to copy the arguments in the test case you linked.

  4. added
    outdated
    on May 4, 2018
  5. locked as resolved and limited conversation to collaborators on May 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has PRoutdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions