Skip to content

Calling static array based vararg function with a value causes extra destructor call #22836

@s-ludwig

Description

@s-ludwig

The following simplified automatic reference counting implementation erroneously counts below zero. Passing a value of type S[1] to bar instead works correctly.

int counter = 0;

struct S {
    bool constructed;
    this(bool) { constructed = true; counter++; }
    this(this) { if (constructed) counter++; }
    ~this() {
        if (constructed) {
            assert(counter > 0);
            counter--;
        }
    }
}

void bar(S[1] s...) { S s2 = s[0]; }
void foo(S s) { bar(s); }
void main() { S s = S(true); foo(s); }

Output:

core.exception.AssertError@onlineapp.d(10): Assertion failure

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions