-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
base: main
Are you sure you want to change the base?
Conversation
{ | ||
public static string? field; | ||
|
||
public Action Prop1 { get; } = () => |
There was a problem hiding this comment.
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
.
@dotnet/roslyn-compiler for second review |
@@ -7188,6 +7188,13 @@ private int GetReceiverSlotForMemberPostConditions(MethodSymbol? method) | |||
} | |||
} | |||
|
|||
if (current.ContainingSymbol is FieldSymbol) | |||
{ | |||
// Functions inside a field initializer are effectively static. |
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Done with review pass (commit 2) |
Closes #76528