hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.48k stars 242 forks source link

[SUGGESTION] More strict evaluation order of function parameters. #149

Closed mhermier closed 1 year ago

mhermier commented 1 year ago

This changes might fall under the "be more strict by default" umbrella.

As of current implementation, if I'm not mistaken in C++ std 7.6.1.3, the evaluation order of a parameter list is implementation specified. While it has no importance most of the time, it is not always true. In particular, it can have consequence in the lifetime of objects and more subtle unexpected behavior the compiler can't catch and can surprise the user. This issue can be mitigated by creating local variables, but can create more extra complications like requiring a copy/move constructor or adding more overloaded method calls.

I can see 2 solutions to this issue:

gregmarr commented 1 year ago

I have a feeling that Herb mentioned this very briefly during his talk, perhaps even in the Q&A. I think it was under the idea of "everything is left to right".

mhermier commented 1 year ago

It is possible, at least it gives an entry to discuss about the subject. Since it is a sensible subject, about speed, calling conventions and possibly platform specific implications, it has to be thought carefully.

hsutter commented 1 year ago

Yes, I absolutely agree that evaluation of function arguments should be ordered left-to-right. I still hope today's C++ will do this.

Paper P0145R3, the paper to consistently improve order or evaluation across the language, was adopted in 2016 except only for this one part, left-to-right evaluation of function arguments. So I think it's time to bring this back to the ISO C++ committee, and I've pinged the coauthors about it. Why now? Because (a) time has passed and perhaps the main concerns some had back in 2016 about this might already be addressed, and (b) the ordering surprises also can lead to bugs that are security vulnerabilities and I think there's more of a sense of urgency about prioritizing fixing those now.

In terms of expr.call paragraph 8, I think the fix would be to change this

… The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter. …

to this

… The initialization of a parameter, including every associated value computation and side effect, is sequenced before that of any subsequent parameter. …

If today's C++ doesn't adopt this, I will probably have to implement it in cppfront. That's not too hard, but I'd rather do it in today's C++ syntax also so that we "light up" this benefit in all existing C++ code too.