www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24372] New: ArraySliceError when calling a function with an

https://issues.dlang.org/show_bug.cgi?id=24372

          Issue ID: 24372
           Summary: ArraySliceError when calling a function with an
                    optional parameter and typesafe variadic
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: artha samerion.com

Tested and reproduced this issue using 2.106.1 and 2.107.0. The following
sample will crash the compiler:

```
import std;

struct A { }
struct B { }
struct C { }

void test(A a, B b = B.init, scope C[] c...) {
    writeln(a, b, c);
}

void main() {
    testWrap(A());
}
```

The above sample works if `b` or `c` is removed from the signature, or if `c`
is defined as `scope C[] c = null...`. While iterating on the arguments, the
compiler appears to assume the slice has a corresponding argument passed, but
it does not. 

Related: https://issues.dlang.org/show_bug.cgi?id=14651

Call stack:

```
core.exception.ArraySliceError src/dmd/mtype.d(4794): slice [2 .. 1] has a
larger lower index than 
upper index
----------------
??:? onArraySliceError [0x5edb604133da]
??:? _d_arraybounds_slicep [0x5edb60404c32]
src/dmd/mtype.d:4794 int dmd.mtype.TypeFunction.callMatch(dmd.mtype.Type,
dmd.expression.ArgumentLi
st, int, const(char)**, dmd.dscope.Scope*).__foreachbody14(ulong,
dmd.mtype.Parameter) [0x5edb60216
f27]
src/dmd/mtype.d:6844 int
dmd.mtype.Parameter._foreach(dmd.root.array.Array!(dmd.mtype.Parameter).Ar
ray*, int delegate(ulong, dmd.mtype.Parameter)).__lambda3!(ulong,
dmd.mtype.Parameter, ulong, dmd.m
type.Parameter).__lambda3(ulong, dmd.mtype.Parameter, ulong,
dmd.mtype.Parameter) [0x5edb6021b030]
src/dmd/mtype.d:6889 int dmd.mtype.Parameter._foreachImpl(scope int
delegate(ulong, dmd.mtype.Param
eter, ulong, dmd.mtype.Parameter), ulong, dmd.mtype.Parameter, ref ulong,
dmd.mtype.Parameter) [0x5
edb6021b217]
src/dmd/mtype.d:6859 int
dmd.mtype.Parameter._foreach(dmd.root.array.Array!(dmd.mtype.Parameter).Ar
ray*, int delegate(ulong, dmd.mtype.Parameter, ulong, dmd.mtype.Parameter))
[0x5edb6021b0d8]
src/dmd/mtype.d:6844 int
dmd.mtype.Parameter._foreach(dmd.root.array.Array!(dmd.mtype.Parameter).Ar
ray*, int delegate(ulong, dmd.mtype.Parameter)) [0x5edb6021aff4]
src/dmd/mtype.d:6615 int dmd.mtype.ParameterList.opApply(scope int
delegate(ulong, dmd.mtype.Parame
ter)) [0x5edb6021a8ba]
src/dmd/mtype.d:4765 dmd.astenums.MATCH
dmd.mtype.TypeFunction.callMatch(dmd.mtype.Type, dmd.expres
sion.ArgumentList, int, const(char)**, dmd.dscope.Scope*) [0x5edb60216c10]

[...]
```

--
Feb 07