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 assignment to ref ternary silently falls back to non-ref assignment #75023

Closed
jjonescz opened this issue Sep 9, 2024 · 2 comments · Fixed by #75076
Closed

Ref assignment to ref ternary silently falls back to non-ref assignment #75023

jjonescz opened this issue Sep 9, 2024 · 2 comments · Fixed by #75076
Assignees
Labels
Area-Compilers Bug New Language Feature - Ref Locals and Returns Ref Locals and Returns untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@jjonescz
Copy link
Member

jjonescz commented Sep 9, 2024

Version Used: cbe141c

Steps to Reproduce:

static void M(bool b, ref string x, ref string y, ref string z)
{
    (b ? ref x : ref y) = ref z;
}

SharpLab

Expected Behavior:

Emitted equivalently to

if (b) { x = ref z; } else { y = ref z; }

Or perhaps an error like

error CS8373: The left-hand side of a ref assignment must be a ref variable.

Actual Behavior:

Emitted like non-ref assignment (b ? ref x : ref y) = z.

Notes:

When emitting with a Debug roslyn build, this assert fails:

Search keywords: ref conditional operator.

This issue is referenced in code (via #74498).

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Sep 9, 2024
@jjonescz
Copy link
Member Author

Similar thing happens also when the LHS is a ref assignment - these two currently emit the same IL:

static void M1(ref int x, ref int y, ref int z)
{
    (x = ref y) = ref z;
}
static void M2(ref int x, ref int y, ref int z)
{
    (x = ref y) = z;
}

@jjonescz
Copy link
Member Author

Looks like both cases should be errors, only locals and parameters are allowed on the LHS per the spec:

The left operand shall be an expression that binds to a reference variable (§9.7), a reference parameter (other than this), an output parameter, or an input parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug New Language Feature - Ref Locals and Returns Ref Locals and Returns untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant