Bug 3924 – nothrow ignored in some situations

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-03-10T06:53:26Z
Last change time
2019-11-03T03:28:24Z
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-03-10T06:53:26Z
The following programs compile and raise exceptions even if the functions/delegates are marked as nothrow: ------------------ void main() { nothrow void delegate() c = { throw new Exception(""); }; c(); } ------------------ nothrow auto foo() { throw new Exception(""); } void main() { foo(); } ------------------ nothrow auto foo() { auto c = { throw new Exception(""); }; c(); } void main() { foo(); } --------------------------------- While this code that looks correct doesn't compile: pure void foo(pure nothrow void delegate(int) callable) { callable(5); } void main() { pure nothrow void bar(int x) {} foo(&bar); } With the first error: test1.d(1): basic type expected, not pure
Comment #1 by post — 2010-11-09T03:55:37Z
(In reply to comment #0) > While this code that looks correct doesn't compile: > > > pure void foo(pure nothrow void delegate(int) callable) { > callable(5); > } > > [...] > > With the first error: > test1.d(1): basic type expected, not pure Note that this works: pure void foo(void delegate(int) pure nothrow callable) { callable(5); }
Comment #2 by andrej.mitrovich — 2012-12-25T13:23:45Z
The workaround for all of these is to put the attributes after the function parameter list. When it's on the left side the compiler seems to ignore it, this of course has to be fixed.
Comment #3 by iamthewilsonator — 2019-11-03T03:28:24Z
Examples given no longer compile: void main() { nothrow void delegate() c = { throw new Exception(""); }; c(); } gives onlineapp.d(2): Error: cannot implicitly convert expression __lambda1 of type void delegate() pure @safe to void delegate() nothrow onlineapp.d(2): Error: cannot implicitly convert expression __lambda1 of type void delegate() pure @safe to void delegate() nothrow nothrow auto foo() { throw new Exception(""); } void main() { foo(); } gives onlineapp.d(2): Error: object.Exception is thrown but not caught onlineapp.d(1): Error: nothrow function onlineapp.foo may throw nothrow auto foo() { auto c = { throw new Exception(""); }; c(); } void main() { foo(); } onlineapp.d(3): Error: c is not nothrow onlineapp.d(1): Error: nothrow function onlineapp.foo may throw