Bug 20137 – A program crashes at runtime (should be compile error)
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-08-18T06:47:33Z
Last change time
2019-08-26T09:33:16Z
Keywords
safe
Assigned to
No Owner
Creator
Victor Porton
Comments
Comment #0 by porton — 2019-08-18T06:47:33Z
The following program crashes at runtime. There should be instead a compile error.
DMD64 D Compiler v2.087.1
///////////////////////
import std.typecons;
class SoundCard {
void setDivider() { }
}
void main(string[] args) {
SoundCard sc = scoped!SoundCard();
sc.setDivider();
}
Comment #1 by razvan.nitu1305 — 2019-08-19T12:30:11Z
I'm not sure this is a bug. Even if it is, it is not a dmd one. `scoped` is implemented as a template function that returns a `struct Scoped`. `struct Scoped` has an alias this to the class instance and it somehow gets messed up.
I suspect this is due to an internal bug in scoped.
Comment #2 by dfj1esp02 — 2019-08-21T09:30:37Z
He assigns scoped to SoundCard, and scoped gets deallocated before next statement. This is by design, mark main with @safe attribute to prevent this.
Comment #3 by porton — 2019-08-21T16:54:03Z
This "by design" is very disgracing. Need to do something.
If to make it more reliable it's needed compiler changes, a new compiler "directive" should be added.
@safe being missing should make harmlessly looking seemingly good program crash without any warning in an unpredictable way!
It is a reliability BUG, not just a misfeature. As it is a bug, fix it.
No, I propose to fix the bug, not to remove a feature.
The measures of https://dlang.org/spec/memory-safe-d.html are not enough, because Scoped is a very special case and need to be done in some special way, to eliminate this blatant reliability BUG.
If it requires compiler changes specifically for Scoped implementation, it should be done, Scoped is a very important construct and deserves special compiler support.
Comment #6 by simen.kjaras — 2019-08-23T15:35:29Z
Scoped!T is like goto - the default advice is "don't use it". When you're ready, you'll know.
Scoped!T should probably be marked as dark magic in the documentation, because that's what it is. It's a useful tool, but you need to know what you're doing, and this should be properly conveyed by documentation.
Beyond that, there is no bug here - the language is doing what it should. This is equivalent to you putting your finger in the circular saw and demanding it be replaced with a hand saw.
Comment #7 by porton — 2019-08-23T15:52:45Z
It is the same as if you were to say: C does what it should do, it crashes programs. I utterly disagree.
Crash on
T x = Scoped!T();
is a BUG in reliability. Not a bug in the implementation, but a bug in reliability. These are two different kinds of bugs, but both categories are bugs.
By the way, we should raise the task to completely replace Ada. There should be no unpredictable crashes.
Comment #8 by simen.kjaras — 2019-08-23T18:19:37Z
There is no unpredictable crash in this case - the crash is perfectly predictable. And no, it's not like saying C does what it should - it's like saying Rust does what it should because you can do unsafe stuff in unsafe blocks. You're punching yourself in the face and telling the doctor it hurts when you do so. The solution is to stop punching yourself in the face.
Comment #9 by dfj1esp02 — 2019-08-26T09:25:25Z
(In reply to Victor Porton from comment #5)
> If it requires compiler changes specifically for Scoped implementation, it
> should be done, Scoped is a very important construct and deserves special
> compiler support.
Sounds like lint. There's https://github.com/dlang-community/D-Scanner
I think it can be taught to detect such Scoped unwrapping.
Comment #10 by dfj1esp02 — 2019-08-26T09:30:12Z
Should be detectable by dscanner on syntax level: if rhs is a template named Scoped and lhs is not an `auto` declared variable.
Comment #11 by dfj1esp02 — 2019-08-26T09:33:16Z
(In reply to Victor Porton from comment #7)
> By the way, we should raise the task to completely replace Ada. There should
> be no unpredictable crashes.
That's more or less the plan for Safe D.