digitalmars.D.learn - Detect uninitialized class var access
- rassoc (13/14) Sep 24 2022 Recently I refactored some old code of mine, now utilizing classes inste...
- Adam D Ruppe (7/9) Sep 24 2022 gdb --args ./your_program
- rassoc (10/14) Sep 24 2022 Thank you for your input, Adam. Real shame that there's no built-in com...
- Tejas (4/21) Sep 24 2022 AFAIK diagnostics like that are not possible because Walter
- Paul Backus (7/22) Sep 25 2022 Implementing such a diagnostic is equivalent, in the general
- Salih Dincer (5/8) Sep 24 2022 I think changing the structure will make things harder (I mean in
- wjoe (24/33) Sep 26 2022 How would you do this without classes:
- rikki cattermole (16/16) Sep 26 2022 Currently in D you would be forced to create a vtable struct manually.
- wjoe (6/22) Sep 26 2022 Or I could just use classes.
Recently I refactored some old code of mine, now utilizing classes instead structs, and I got hit by an uninitialized variable access pattern similar to the simplified example below. How can we easily spot this? What other switches and safeguards are there?dmd -dip1000 -debug -g -wjust crashes with `Error: program killed by signal 11` on linux and nothing else. ```d import std; safe: class Foo { int x; } void main() { Foo f; // = new Foo; writeln(f.x); } ```
Sep 24 2022
On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:just crashes with `Error: program killed by signal 11` on linux and nothing else.gdb --args ./your_program and then it will tell you all the details you want to know about when this happens. strangly sometimes adding the -O option will cause dmd to catch simple cases at compile time but gdb is the general solution to track these down.
Sep 24 2022
On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:gdb --args ./your_program and then it will tell you all the details you want to know about when this happens.Thank you for your input, Adam. Real shame that there's no built-in compiler solution and probably never will be according to [1]. :( D's competition got stuff like this right: ```crystal build foo.crIn foo.cr:6:6 6 | puts f.x ^ Error: read before assignment to local variable 'f' ``` [1] https://issues.dlang.org/show_bug.cgi?id=4595
Sep 24 2022
On Saturday, 24 September 2022 at 23:04:00 UTC, rassoc wrote:On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:AFAIK diagnostics like that are not possible because Walter doesn't want to implement full blown dataflow analysis in the compiler since it will slow down the compilation speed of dmdgdb --args ./your_program and then it will tell you all the details you want to know about when this happens.Thank you for your input, Adam. Real shame that there's no built-in compiler solution and probably never will be according to [1]. :( D's competition got stuff like this right: ```crystal build foo.crIn foo.cr:6:6 6 | puts f.x ^ Error: read before assignment to local variable 'f' ``` [1] https://issues.dlang.org/show_bug.cgi?id=4595
Sep 24 2022
On Sunday, 25 September 2022 at 03:04:45 UTC, Tejas wrote:On Saturday, 24 September 2022 at 23:04:00 UTC, rassoc wrote:Implementing such a diagnostic is equivalent, in the general case, to solving the halting problem. So even with dataflow analysis, there would always be cases where the compiler either fails to detect a potential null dereference (false negative), or detects one that can't actually happen at runtime (false positive).On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:AFAIK diagnostics like that are not possible because Walter doesn't want to implement full blown dataflow analysis in the compiler since it will slow down the compilation speed of dmdgdb --args ./your_program and then it will tell you all the details you want to know about when this happens.Thank you for your input, Adam. Real shame that there's no built-in compiler solution and probably never will be according to [1]. :( [1] https://issues.dlang.org/show_bug.cgi?id=4595
Sep 25 2022
On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:Recently I refactored some old code of mine, now utilizing classes instead structs, and I got hit by an uninitialized variable access pattern similar to the simplified example below.I think changing the structure will make things harder (I mean in general). I know most people will object; but classes are useless. Structs are enough for you for the rest of your life 😀 SDB 79
Sep 24 2022
On Sunday, 25 September 2022 at 02:10:00 UTC, Salih Dincer wrote:On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:How would you do this without classes: ``` D interface ForwardRange { // ... } class Foo: ForwardRange { // ... } class Bar: ForwardRange { // ... } struct Baz { ForwardRange r; this(ForwardRange r_) { r = r_; } } ```Recently I refactored some old code of mine, now utilizing classes instead structs, and I got hit by an uninitialized variable access pattern similar to the simplified example below.I think changing the structure will make things harder (I mean in general). I know most people will object; but classes are useless. Structs are enough for you for the rest of your life 😀 SDB 79
Sep 26 2022
Currently in D you would be forced to create a vtable struct manually. But if we had something like signatures you could do this: ```d struct Foo { //... } struct Bar { InputRange input; } void doIt() { Bar bar; Foo* foo = new Foo; bar.input = foo; } ``` Classes are not the only way to do OOP :)
Sep 26 2022
On Monday, 26 September 2022 at 16:51:11 UTC, rikki cattermole wrote:Currently in D you would be forced to create a vtable struct manually.Or I could just use classes. The cure shouldn't be worse than the disease.But if we had something like signatures you could do this: ```d struct Foo { //... } struct Bar { InputRange input; } void doIt() { Bar bar; Foo* foo = new Foo; bar.input = foo; } ```This would be nice but since we don't it doesn't really matter. unfortunately.
Sep 26 2022