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
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Debug.Assert(method.MethodKind is MethodKind.LambdaMethod or MethodKind.LocalFunction);
return 0;
}

if (current.TryGetThisParameter(out var thisParameter) && thisParameter is not null)
{
return GetOrCreateSlot(thisParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10588,5 +10588,63 @@ void M()
// [A(p)] void F() { }
Diagnostic(ErrorCode.WRN_UnreferencedLocalFunction, "F").WithArguments("F").WithLocation(13, 21));
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/76528")]
[InlineData("")]
[InlineData("static ")]
public void Repro76528(string modifiers)
{
var source = $$"""
#nullable enable

using System;
using System.Diagnostics.CodeAnalysis;

public class C
{
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.

{
init();
Console.WriteLine(field.Length);

[MemberNotNull(nameof(field))]
{{modifiers}}void init() => field ??= "";
};
}
""";
var comp = CreateCompilation([source, MemberNotNullAttributeDefinition]);
comp.VerifyEmitDiagnostics();
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/76528")]
[InlineData("")]
[InlineData("static ")]
public void Repro76528_FieldInitializer(string modifiers)
{
var source = $$"""
#nullable enable

using System;
using System.Diagnostics.CodeAnalysis;

public class C
{
public static string? field;

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

[MemberNotNull(nameof(field))]
{{modifiers}}void init() => field ??= "";
};
}
""";
var comp = CreateCompilation([source, MemberNotNullAttributeDefinition]);
comp.VerifyEmitDiagnostics();
}
}
}