Skip to content

inout and auto ref causing a return value to be accepted by ref but not actually be mutated #21057

@jmdavis

Description

@jmdavis

This code

void main()
{
    S foo;
    const S bar;

    functionTakingRef(foo.test);

    assert(foo.test == 69); //fails
    assert(bar.test == 0);
}

void functionTakingRef(ref ubyte u)
{
    u = 69;
}

struct S
{
    private ubyte field;

    auto ref ubyte test() inout
    {
        return field;
    }
}

fails the first assertion. Somehow, test is being called, and its result is being passed to functionTakingRef, and yet that ref isn't being mutated. It's staying 0 like the ref doesn't actually point to field like it's supposed to.

The issue seems to somehow be tied up in auto ref and referring inout in that if test is changed to

    ref inout(ubyte) test() inout

then the code works properly. Curiously,

auto ref inout(ubyte) test() inout

also compiles and works even though both auto ref and ubyte are specified, which also seems like a bug to me, but if so, that's a separate issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions