On 13 November 2015 at 08:38, Iain Buclaw via Digitalmars-d <digitalmars-d@puremagic.com> wrote: > On 12 Nov 2015 10:25 pm, "David Nadlinger via Digitalmars-d" > <digitalmars-d@puremagic.com> wrote: >> >> On Thursday, 12 November 2015 at 21:16:25 UTC, Walter Bright wrote: >>> >>> It's more than that - dmd's optimizer is designed to make use of the >>> guarantees of a pure function. Since C/C++ do not have pure functions, >>> ldc/gdc's optimizer may not have that capability. >> >> >> Oh, GCC has had similar notions as a non-standard attribute for ages, and >> LLVM since its inception. >> >> At least for LDC, the reason why we do not currently lower many of the >> qualifiers like pure, nothrow, immutable, etc. is that LLVM will ruthlessly >> consider your code to exhibit undefined behavior if you try to be clever and >> violate them, subsequently optimizing based on that. In other words, if you >> cast away const/immutable and modify a variable, for instance, you might >> find that the entire function body magically disappears under your feet. >> >> Maybe it is time to revisit this, though, but last time I tried it broke >> druntime/Phobos in a couple of places. >> > > Same here, and for some very surprising reasons from what I recall.
These language mechanisms offer D a huge potential advantage, it would be really good to understand why we can't make use of them, and work towards fixing this. I don't think people should be surprised if the optimiser takes advantage of their code attribution. It may break existing code because violating these attributes never caused any problem before, but surely violating those attributes was never actually valid code, and it's reasonable that they expect their code to break in the future as compilers improve their ability to take advantage of these attributes? In the meantime, there probably needs to be strong warnings about violating attributes, and if patterns have emerged that rely on violating such attributes, we should publish a recommended alternative.