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

Can't suppress NRT warnings on ternary with refs #73928

Closed
MihaZupan opened this issue Jun 11, 2024 · 2 comments · Fixed by #74498
Closed

Can't suppress NRT warnings on ternary with refs #73928

MihaZupan opened this issue Jun 11, 2024 · 2 comments · Fixed by #74498

Comments

@MihaZupan
Copy link
Member

Version Used:

SDK 9.0.100-preview.6.24303.15

Steps to Reproduce:

While you can suppress NRT warnings on simple assignments like so

ref object myRef = ref GetNullableObjectRef()!;
ref object? myRef = ref GetObjectRef()!;

I've been unable to find a combination of !/?/() to suppress the warnings on this ternary:

void Problem(bool condition)
{
    ref object myRef = ref (condition
        ? ref GetObjectRef()!
        : ref GetNullableObjectRef()!)!;

    // Assuming these are methods whose shape I can't change
    static ref object GetObjectRef() => ref Unsafe.NullRef<object>();
    static ref object? GetNullableObjectRef() => ref Unsafe.NullRef<object?>();
}

SharpLab

Diagnostic Id:

warning CS8619: Nullability of reference types in value of type 'object' doesn't match target type 'object?'.

Expected Behavior:

I should be able to suppress warnings with some combination of !s.

Actual Behavior:

I can only avoid warnings via #nullable disable.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 11, 2024
@jaredpar jaredpar added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 11, 2024
@jaredpar jaredpar added this to the 17.12 milestone Jun 11, 2024
@TessenR
Copy link

TessenR commented Oct 7, 2024

@jjonescz Is it expected that now you can suppress nullability mismatch for foreach with ref variables but not for foreach with regular variables?

It looks like the following test is supposed to cover that scenario, but it's missing the actual nullability mismatch because the parameter of the test method is object[] instead of object?[]

image

9941c1f#diff-14fe026e6093e9522c01e6c377b26554dd6b663214884198dcc244f370548fa1R90883

The following code right now does have a warning and it cannot be suppressed:

void M2(object[] a) { foreach (object item in a!) { } }

Should I create a separate issue for this case?

@jjonescz
Copy link
Member

jjonescz commented Oct 8, 2024

@TessenR that indeed looks unexpected, I've converted your comment into an issue: #75430. Thanks for reporting this.

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

Successfully merging a pull request may close this issue.

4 participants