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

Handle nullable postcondition for non-static local function inside field initializer #76716

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

RikkiGibson
Copy link
Contributor

Closes #76528

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 10, 2025
@RikkiGibson RikkiGibson marked this pull request as ready for review January 11, 2025 16:38
@RikkiGibson RikkiGibson requested a review from a team as a code owner January 11, 2025 16:38
{
public static string? field;

public Action Prop1 { get; } = () =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider testing also when the lambdas are marked static.

@RikkiGibson RikkiGibson requested a review from a team January 14, 2025 23:43
@RikkiGibson
Copy link
Contributor Author

@dotnet/roslyn-compiler for second review

@jcouv jcouv self-assigned this Jan 17, 2025
@@ -7188,6 +7188,13 @@ private int GetReceiverSlotForMemberPostConditions(MethodSymbol? method)
}
}

if (current.ContainingSymbol is FieldSymbol)
{
// Functions inside a field initializer are effectively static.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Functions inside a field initializer are effectively static.

Is this accurate? For example, it looks like in the following scenario both the lambda and the local function are implemented by an instance method.

#nullable enable

using System;
using System.Diagnostics.CodeAnalysis;

public class C(string p)
{
    public static string? field;

    public Action field2 = () =>
    {
        init();
        Console.WriteLine(field.Length);

        [MemberNotNull(nameof(field))]
        void init() => field ??= p;
    };
    
    string M() => p;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, when marked static the functions cannot use p. I'll adjust the comment and add the test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the comment got removed, but it feels like we still need some rational to explain why returning 0 is probably not 100% accurate, but OK thing to do.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
4 participants