www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11461] New: `Error`s are not thrown as `pure nothrow` functions are optimized out with "-O -release"

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461

           Summary: `Error`s are not thrown as `pure nothrow` functions
                    are optimized out with "-O -release"
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: verylonglogin.reg gmail.com


--- Comment #0 from Denis Shelomovskij <verylonglogin.reg gmail.com> 2013-11-07
11:15:14 MSK ---
This program runs without errors if compiled with "-O -release" and `f` is
strongly pure and `nothrow`:
---
void f() pure nothrow
{ throw new Error(""); }

void main()
{ f(); }
---

So optimizer must not completely optimize out even `nothrow` strongly pure
function calls as an `Error` still could be thrown or there could be a HLT
instruction.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 07 2013
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461


Martin Nowak <code dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code dawg.eu


--- Comment #1 from Martin Nowak <code dawg.eu> 2013-11-14 18:36:52 PST ---
Well, if a function has no side effect and does not produce a result or the
result isn't used, there is no point in calling it. It's pretty harsh to demand
that the compiler always calls such functions only to support the minor
noreturn use-case.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461



--- Comment #2 from Denis Shelomovskij <verylonglogin.reg gmail.com> 2013-11-15
09:26:02 MSK ---
(In reply to comment #1)
 Well, if a function has no side effect and does not produce a result or the
 result isn't used, there is no point in calling it. It's pretty harsh to demand
 that the compiler always calls such functions only to support the minor
 noreturn use-case.

Minor or no it is documented `pure` functions can throw and the compiler already consider `pure` but not `nothrow` functions as doing this (i.e. they aren't optimized out). So `nothrow` must be treated the same way as `nothrow` just means no recoverable `Exception`s are thrown. Breaking language changes are welcome but in a separate enhancement issue. Currently `onOutOfMemoryError` is no-op because of this and the issue should be fixed. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461



--- Comment #3 from Martin Nowak <code dawg.eu> 2013-11-15 00:09:19 PST ---
Interesting btw, such a function will either never or always throw an Error.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra gmail.com


--- Comment #4 from monarchdodra gmail.com 2013-11-15 01:23:58 PST ---
(In reply to comment #2)
 (In reply to comment #1)
 Well, if a function has no side effect and does not produce a result or the
 result isn't used, there is no point in calling it. It's pretty harsh to demand
 that the compiler always calls such functions only to support the minor
 noreturn use-case.

Minor or no it is documented `pure` functions can throw and the compiler already consider `pure` but not `nothrow` functions as doing this (i.e. they aren't optimized out). So `nothrow` must be treated the same way as `nothrow` just means no recoverable `Exception`s are thrown. Breaking language changes are welcome but in a separate enhancement issue. Currently `onOutOfMemoryError` is no-op because of this and the issue should be fixed.

I don't know... an "Error" isn't really considered as "observable program behavior", is it? If a function is (either strongly or weakly) pure, takes no reference arguments, and is nothrow, then it calling it will not have observable side effect, and can (and IMO should) be optimized out. This can be workaround simply by forcing the function to look like it can have "observable side effects", as so: //---- void f(void* = null) pure nothrow { throw new Error(""); } void main() { f(); } //---- -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461



--- Comment #5 from Martin Nowak <code dawg.eu> 2013-11-15 01:35:56 PST ---
(In reply to comment #3)
 Interesting btw, such a function will either never or always throw an Error.

The fix is fairly simple, we need a way to annotate noreturn functions. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461



--- Comment #6 from github-bugzilla puremagic.com 2013-11-15 03:57:30 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/e68e2459bd9a977338837a7e784041de56b68516
Add issue 11461 workaround.

Issue URL: https://d.puremagic.com/issues/show_bug.cgi?id=11461

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461



--- Comment #7 from github-bugzilla puremagic.com 2013-11-15 10:33:56 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/0422e58d1bd6c86063f73b9ca372e5e16afd64b0
Add issue 11461 workaround.

Issue URL: https://d.puremagic.com/issues/show_bug.cgi?id=11461

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461


Temtaime <temtaime gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |temtaime gmail.com


--- Comment #8 from Temtaime <temtaime gmail.com> 2013-11-15 10:38:43 PST ---
Hi, guys.
Maybe i'm too stupid, but how nothrow function can throw ?

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461



--- Comment #9 from monarchdodra gmail.com 2013-11-15 11:53:06 PST ---
(In reply to comment #8)
 Hi, guys.
 Maybe i'm too stupid, but how nothrow function can throw ?

D defines both "Exception" and "Error", which derive from "Throwable". "Exception", is what you know from C++: The standard thing that gets thrown around to notify things like database/io problems: Things that "happen" and need to be dealt with. They unwind the stack, destroy things on the stack, etc... "Error" is an entire other beast: They are C++'s "assert" in more powerful: They represent a critical error of the program state: corruption, out of memory, failed assertions. They represent something you *cannot* recover from, and are telling you the program needs to halt. Unlike an "Exception", "Error" will blast through the call stack, bypassing destructors, bypassing "nothrow" function, bypassing "catch(Exception)"... They are not meant to happen general, and shouldn't be "handled" in the general sense of the term. So long story short, an Error can be thrown from a nothrow function, because an Error doesn't trigger stack cleanup, which means there is no need to generate _Exceptio_ handling code". => "nothrow" means "will not throw _Exceptions_". I'd link some references, but the best I know of is TDPL. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461



--- Comment #10 from Temtaime <temtaime gmail.com> 2013-11-15 12:33:55 PST ---
Clear.
Thanks very much for explanation.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461


Rainer Schuetze <r.sagitario gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |r.sagitario gmx.de


--- Comment #11 from Rainer Schuetze <r.sagitario gmx.de> 2014-03-22 04:48:07
PDT ---
(In reply to comment #7)
 Commit pushed to master at https://github.com/D-Programming-Language/druntime
 
 https://github.com/D-Programming-Language/druntime/commit/0422e58d1bd6c86063f73b9ca372e5e16afd64b0
 Add issue 11461 workaround.
 
 Issue URL: https://d.puremagic.com/issues/show_bug.cgi?id=11461

Any particular reason why "pure" isn't removed from the onOutOfMemoryError declaration in core.exception aswell? And for all the other error functions? -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 22 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #12 from bearophile_hugs eml.cc 2014-03-22 05:32:37 PDT ---
(In reply to comment #5)
 (In reply to comment #3)
 Interesting btw, such a function will either never or always throw an Error.

The fix is fairly simple, we need a way to annotate noreturn functions.

On this there is no DIP and perhaps not even an enhancement request. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 22 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461



--- Comment #13 from monarchdodra gmail.com 2014-03-22 14:59:11 PDT ---
Maybe the problem can be worked around by making the function(s) *weakly* pure?

(In reply to comment #11)
 (In reply to comment #7)
 Commit pushed to master at https://github.com/D-Programming-Language/druntime
 
 https://github.com/D-Programming-Language/druntime/commit/0422e58d1bd6c86063f73b9ca372e5e16afd64b0
 Add issue 11461 workaround.
 
 Issue URL: https://d.puremagic.com/issues/show_bug.cgi?id=11461

Any particular reason why "pure" isn't removed from the onOutOfMemoryError declaration in core.exception aswell? And for all the other error functions?

Instead of removing the "pure", which is a breaking change, since it would prevent pure functions from having an "onOutOfMemoryError", what about my suggestion for weak purity? void onOutOfMemoryError(void* dummy11461 = null) trusted pure nothrow; or just: void onOutOfMemoryError(void* = null) trusted pure nothrow; instead of void onOutOfMemoryError() trusted /* pure dmd BUG11461 */ nothrow; -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 22 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461



--- Comment #14 from Rainer Schuetze <r.sagitario gmx.de> 2014-03-23 02:32:33
PDT ---
I agree, it seems the better workaround. Clever compilers might notice that the
passed parameter introduces no sideeffects and might infer strong purity,
though.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 23 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461


Rainer Schuetze <r.sagitario gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #15 from Rainer Schuetze <r.sagitario gmx.de> 2014-03-23 03:28:42
PDT ---
https://github.com/D-Programming-Language/druntime/pull/753

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 23 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11461



--- Comment #16 from github-bugzilla puremagic.com 2014-03-23 18:11:42 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/2a50ceabb436d150ee4e6bfff299def58bc9e92c
Merge pull request #753 from rainers/workaround_bug11461

Workaround 11461: avoid strong purity inference by passing null pointer to
onOutOfMemoryError

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 23 2014