terralang / terra

Terra is a low-level system programming language that is embedded in and meta-programmed by the Lua programming language.
terralang.org
Other
2.72k stars 201 forks source link

printstats() on uncompiled function raises useless error message #396

Closed DarkWiiPlayer closed 5 years ago

DarkWiiPlayer commented 5 years ago
terra dostuff()
    return 10
end

-- dostuff:compile()
print(dostuff:printstats())

This code produces a useless error message

src/terralib.lua:561: bad argument #1 to 'pairs' (table expected, got nil)

Which wouldn't help at all in a more complex codebase where it's harder to figure out at which point a function is actually compiled.

It would make sense to either compile the function automatically when printstats is called on a yet uncompiled function, or to just return nil + error message so as to not crash the entire program.

elliottslaughter commented 5 years ago

Seems reasonable, and it shouldn't be that hard to implement if you'd like to take a pass at putting together a PR.

DarkWiiPlayer commented 5 years ago

Both options seem trivial to implement; which one would be preferable though?

elliottslaughter commented 5 years ago

I'd just make printstats call compile since it's normally something people would do for debugging so taking the hit (in time to wait for compilation to finish) shouldn't affect any large codes.

DarkWiiPlayer commented 5 years ago

Sounds reasonable; my was that compiling a function is a side effect and I personally prefer debug functionality to be free of side effect, so the programs behavior doesn't change from adding / removing a debug output. Correct me if I'm wrong, but after defining a function (and before compiling it) you can change other things that will affect the function, right?

elliottslaughter commented 5 years ago

This is less true than it used to be now that we use eager type checking (though possibly we may go back to being a little lazier at some point).

I think the key question is whether printstats is something that would be useful even if the function were not compiled? Like the disas function is clearly not, so it just compiles the function for you if it isn't already. I'd be inclined to put printstats in this category as well.

elliottslaughter commented 5 years ago

Merged the PR. Thanks again!