Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref and auto ref variable breaks escape analysis within typeof #20903

Open
Bolpat opened this issue Feb 20, 2025 · 0 comments
Open

ref and auto ref variable breaks escape analysis within typeof #20903

Bolpat opened this issue Feb 20, 2025 · 0 comments

Comments

@Bolpat
Copy link
Contributor

Bolpat commented Feb 20, 2025

void main()
{
    int x;

    ref typeof(x) r0 = x; // good
    auto ref typeof(x) r1 = 0; // good

    typeof(function{ return 0; }()) v0 = x; // good
    typeof(function{ return 0; }()) v1 = 0; // good

    ref typeof(function{ return 0; }()) r2 = x;
    // Error: cannot `ref` return constant `0`
    // Error: type `int` cannot be assigned to `ref _error_ r2`
    
    auto ref typeof(function{ return 0; }()) r3 = x;
    // Error: cannot `ref` return constant `0`

    auto ref typeof(function{ return 0; }()) r4 = 0;
    // Error: cannot `ref` return constant `0`


    typeof(function ref{ return x; }) fp0; // infers `ref int function() pure nothrow @nogc @safe` (`ref` needed)
    /*auto*/ ref typeof(function{ return x; }) fp = fp0; // Note: no `ref` after `function`
    static assert(__traits(isRef, fp)); // `fp` is a reference variable: as intended
    static assert(is(typeof(&fp()))); // `fp` returns by reference: makes no sense
    // With`auto ref` instead of `ref`: No change in behavior
}

It seems the presence of ref or auto ref on the variable makes the function literal within typeof have a ref return. If within the literal, return 0; is replaced by returning a local variable,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant