September 27, 2023
https://issues.dlang.org/show_bug.cgi?id=24166

          Issue ID: 24166
           Summary: strange errors returning references to void
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: john.loughran.colvin@gmail.com

Starting with a correct program as a baseline:


auto ref foo(T)(T[] a, size_t i) {
    return a[i];
}

void main() {
    int[] a = [1,2,3];
    blah(&foo(a, 1));
}

void blah(void* a) {
    import std.stdio : writeln;
    writeln(a); // prints some pointer value
}


now moving on to some odd situations. Changing `a` to be `void[]`:


auto ref foo(T)(T[] a, size_t i) {
    return a[i];
}

void main() {
    void[] a = [1,2,3];
    blah(&foo(a, 1));
}

void blah(void* a) {
    import std.stdio : writeln;
    writeln(a);
}

onlineapp.d(2): Deprecation: `a[i]` has no effect
onlineapp.d(7): Error: `foo(a, 1LU)` is not an lvalue and cannot be modified


Strange, `foo` is not inferred as `ref`. Ok, let's force it by removing that `auto`


ref foo(T)(T[] a, size_t i) {
    return a[i];
}

void main() {
    void[] a = [1,2,3];
    blah(&foo(a, 1));
}

void blah(void* a) {
    import std.stdio : writeln;
    writeln(a);
}

onlineapp.d(2): Deprecation: `a[i]` has no effect
C


well that's odd! That statement should definitely have an effect. But what's also strange is that `C` is an unlikely pointer value (it prints `3` on another machine I tried).

Let's try with ldc:


onlineapp.d(2): Deprecation: `a[i]` has no effect
Invalid bitcast
  %16 = bitcast void <badref> to i8*, !dbg !3044
LLVM ERROR: Broken module found, compilation aborted!
#0 0x0000564ffa018667 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int)
(/dlang/ldc-1.34.0/bin/ldc2+0x6632667)
Error: Error executing /dlang/ldc-1.34.0/bin/ldc2: Aborted (core dumped)

oh dear...

--