Weird DIP1000 issue

0xEAB desisma at heidel.beer
Tue Feb 7 23:42:38 UTC 2023


Hi,

I’ve recently run into an issue with DIP1000.

After a lot of confusion by myself and suggestions from fellow 
coders on Discord (Thanks everyone!) I went on with implementing 
a potential workaround. Well… until my issue was suddenly gone 
although there was no actual workaround in place yet.

Yesterday, it came to my mind that there must be something off. 
And that our/my original conclusions might not be exactly sound.

So I went back to the reduced sample code provided by Mayonix 
(special thanks for throwing it into dustmite!) and compared it 
to the working version. I found something interesting and 
prepared this post. Then double checked my code samples… Just to 
notice the error is gone (again!). “What’s going on?”

The issue comes down to:
```
noscope360.d(5): Error: scope variable `c` assigned to non-scope 
parameter `_param_0` calling `write`
noscope360.d(13):        which is not `scope` because of `buffer 
= _param_0`
```

Here’s the code snippet:
```d
void main() @safe {
     auto mb = MBuf();
     auto vr = VRes!X();
     foreach(c; vr.errors)
         mb.write(c);
}

struct X { }

struct MBuf {
     const(ubyte)[] _bufferList;
     void write(Buffers...)(Buffers buffers) {
         foreach (buffer; buffers) { }
     }
}

struct VErr { string m; }

struct VRes(Data) {
     VErr[Data.tupleof.length] _errors;
     auto errors() {
         import std.algorithm;
         return _errors[].filter!(e => e.m);
     }
}
```

And two potential fixes:

```diff
--- noscope360.d
+++ yoscope360.d
@@ -8,7 +8,6 @@
  struct X { }

  struct MBuf {
-    const(ubyte)[] _bufferList;
      void write(Buffers...)(Buffers buffers) {
          foreach (buffer; buffers) { }
      }
```


```diff
--- noscope360.d
+++ throwscope360.d
@@ -10,7 +10,7 @@
  struct MBuf {
      const(ubyte)[] _bufferList;
      void write(Buffers...)(Buffers buffers) {
-        foreach (buffer; buffers) { }
+        static foreach (buffer; buffers) { }
      }
  }
```

The changes from *throwscope360* are obviously not doable in 
practice, as they remove code that would have been used 
eventually.

Does it make sense to have `*static* foreach` mandatory here?

I don’t feel like those changes should matter.
While I’m by no means sure that this is a bug, I would really 
hope for it to be one.


More information about the Digitalmars-d mailing list