Skip to content

If..return removes something it shouldn't #399

Closed

Description

@ziadkh0
(function listSnaps(snapsList) {
  if (!snapsList) {
    return;
  }

  let pendingIdText = '';

  function handleData(newText: string) {
    const snapIds = (pendingIdText + newText).split(',');

    pendingIdText = snapIds.pop();
    console.log(snapIds);
  }
  handleData('a,b');
}(true));

Results in

(function(b){function c(e){const f=(d+e).split(',');d=f.pop(),console.log(f)}if(b){c('a,b')}})(!0);

Which gives "d is not defined".
pendingIdText became d but the let pendingIdText = ''; was removed!

try it

Activity

  1. added
    bug
    on Feb 1, 2017
  2. boopathi commented on Feb 1, 2017

    @boopathi
    Member

    Some bug with if..return transform

  3. boopathi commented on Feb 1, 2017

    @boopathi
    Member

    Related #281

  4. boopathi commented on Feb 1, 2017

    @boopathi
    Member

    Fix for #281 and fix for this one feels like

  5. changed the title [-][DCE] Bug: Removes something it shouldn't[/-] [+]If..return removes something it shouldn't[/+] on Feb 1, 2017
  6. boopathi commented on Feb 3, 2017

    @boopathi
    Member

    /cc @kangax @hzoo

    Need some suggestions to handle this -

    function a() {
      if (foo) return;
      let x = 1;
      function b() { use(x); }
    }

    cannot be transformed to (which is happening now)

    function a() {
      function b() { use(x); /* x no longer refers to the same let binding */ }
      if (!foo) {
        let x = 1; // dead code
      }
    }

    Options are

    • Detect block scoped binding usages and deopt if_return transformation (since this transformation itself causes changes in block, and we have a babel issue with bindings and scopes, it MIGHT lead to other issues with bindings or detection)
    • Hoist let declaration: Cannot have const, and also here, we'll suppress a possible error by doing this as let is not hoisted.
    function a() {
      if (foo) return;
      use(x); // here it will throw as x is not yet a binding and let is not hoisted
      let x = 1;
      function b() { use(x) };
    }

    to

    function a() {
      function b() { use(x); }
      let x;
      if (!foo) {
        use(x); // here x is a binding and is undefined
        x = 1;
      }
    }

    Any other ideas or suggestions welcome.

  7. ziadkh0 commented on Feb 3, 2017

    @ziadkh0
    Author

    @boopathi Why not wrap the whole thing with the if?

    function a() {
      if (!foo) {
        let x = 1;
        function b() { use(x); }
      }
    }
  8. boopathi commented on Feb 3, 2017

    @boopathi
    Member

    @zaygraveyard

    function a() {
      if (b()) { // error
        function b() {} // let binding & hoisted within the block
      }
    }
  9. hzoo commented on Feb 3, 2017

    @hzoo
    Member

    I think it's best for us to deopt when possible and then go back and do optimizations? I think our goal is stability first, opt later?

    It is safe for us to hoist let/anything?

  10. boopathi commented on Feb 3, 2017

    @boopathi
    Member

    @hzoo True. But the only thing that's blocking is some bugs in babel's scope tracking - we solved most of these issues (in mangler and dce) - by doing traverse.clearCache() and scope.crawl(). We cannot do that in simplify, and we don't want simplify to be a single pass.

    Edit:
    Also, we have to change node.field = newField style to path.replaceWith as mutation doesn't update scope tracking and it's possible to insert a faulty node with mutating the node directly through assignment.

  11. added a commit that references this issue on Feb 8, 2017
    cd93ad9
  12. added a commit that references this issue on Feb 18, 2017
    7f98958
  13. added
    has PR
    on Feb 20, 2017
  14. added a commit that references this issue on Feb 27, 2017
    4ef735e
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

    bughas PR

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions