Bug 15125 – Explicit pure needed even though pure: at the top of the file

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-09-28T14:41:41Z
Last change time
2022-10-13T07:28:45Z
Keywords
spec
Assigned to
No Owner
Creator
Atila Neves

Comments

Comment #0 by atila.neves — 2015-09-28T14:41:41Z
The code below fails to compile, saying that `throwMyException` can't call the MyException constructor because it's not pure, even though there's a `pure:` at the top. Adding `pure` explicitly to the constructor makes the error go away. @safe: pure: class MyException: Exception { this() { super(""); } } void throwMyException() { throw new MyException(); }
Comment #1 by razvan.nitu1305 — 2018-05-28T09:55:03Z
Although I cannot find any documentation about it, from what my experience using pure:, safe:, extern(something):, nogc:, nothrow: works only for top-level declaration (does not include the inner scopes of aggregateDeclarations). In your specific case pure and safe are applied to the MyExceptionClass but since safe and pure do not make sense for classes, they are silently ignored (not propagated to member functions). I have no idea if this is the intended behavior or not, but in my opinion, dmd's policy to silently ignore attributes when they do not make sense is not a good one. Other notable cases: extern(C) class A {} // compiler accepts it and ignores extern(C) pure class A {} // again no problem, but pure is not propagated to A's // methods @safe class A {} // ditto The explanation for this is that you can use `$attribute`: at the top of the module and only the declarations that make sense in conjunction with that attribute will be affected and changing this to actually issuing an error for those that don't would break too much code. I tried doing this for `extern(C) class` and look at the result [1]. In conclusion, this will most likely be closed as WONTFIX. [1] https://github.com/dlang/dmd/pull/7229