GithubHelp home page GithubHelp logo

Comments (6)

stefanos82 avatar stefanos82 commented on August 10, 2024

I don't know whether you have seen it or not, but I have just found it and it's kind of baffling:

int nelua_main(int argc, char** argv) {
  {
    nelua_print_1(tmp_f(5));
    {
      nlmulret_nlint64_nlboolean _tmp1 = tmp_g(5);
      nelua_print_2(_tmp1.r1, _tmp1.r2);
    }
  }
  return 0;
}

Should not this code be

int nelua_main(int argc, char** argv) {
  {
    nelua_print_1(tmp_f(5));
  }
    {
      nlmulret_nlint64_nlboolean _tmp1 = tmp_g(5);
      nelua_print_2(_tmp1.r1, _tmp1.r2);
    }
  return 0;
}

because print(f(5)) and print(g(5)) are executed as two separate defers behind the scenes, correct?

from nelua-lang.

jrfondren avatar jrfondren commented on August 10, 2024

The braces around nelua_print_2() are probably there to quickly free _tmp1 from occupying stack space - it's about multiple value returns from g() rather than anything getting deferred.

from nelua-lang.

stefanos82 avatar stefanos82 commented on August 10, 2024

OK, I have found the bug; the generated C code for defer takes place in wrong line(s):

/* Buggy version */
nlmulret_nlint64_nlboolean tmp_g(int64_t n) {
  nlmulret_nlint64_nlboolean _mulret_1;
  _mulret_1.r1 = n;
  _mulret_1.r2 = true;
  { /* defer */
    n = (n + 1);
  }
  return _mulret_1;
}

It should be

/* Fixed version*/
nlmulret_nlint64_nlboolean tmp_g(int64_t n) {
  nlmulret_nlint64_nlboolean _mulret_1;
  { /* defer */
    n = (n + 1);
  }
  _mulret_1.r1 = n;
  _mulret_1.r2 = true;
  return _mulret_1;
}

Update: After thinking about it, my fix violates the logic of defer mechanism that is supposed to take place as last. I wonder how @edubart could solve this peculiar case...maybe with the use of GOTO labels? 🤔

Update 2: On second thought, the code below should work too:

nlmulret_nlint64_nlboolean tmp_g(int64_t n) {
  nlmulret_nlint64_nlboolean _mulret_1;
  { /* defer */
    n = (n + 1);
  }
  return _mulret_1  = (nlmulret_nlint64_nlboolean){.r1=n, .r2=true};
}

Since we are deferring n only and then return n, true, a compound literal should work as shown above.

from nelua-lang.

edubart avatar edubart commented on August 10, 2024

This is a serious bug, I try to fix it next weekend. Thanks for reporting!

from nelua-lang.

stefanos82 avatar stefanos82 commented on August 10, 2024

@edubart keep up the good work brother 👍

@jrfondren Good job my friend with your thorough investigation; that makes us two now! 😃

from nelua-lang.

edubart avatar edubart commented on August 10, 2024

I fixed the misbehavior in dc479bf, by definition defer should only execute after evaluating all function statements, including return expressions.

from nelua-lang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.