-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Fix extra warning for unused local function #47473
Conversation
@@ -1180,7 +1180,7 @@ public override BoundNode VisitCall(BoundCall node) | |||
VisitReceiverBeforeCall(node.ReceiverOpt, node.Method); | |||
VisitArgumentsBeforeCall(node.Arguments, node.ArgumentRefKindsOpt); | |||
|
|||
if (!callsAreOmitted && node.Method?.OriginalDefinition is LocalFunctionSymbol localFunc) | |||
if (node.Method?.OriginalDefinition is LocalFunctionSymbol localFunc) |
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.
Since node
is actually a BoundCall
, then we already know the local function is used. We want to visit it regardless it's conditionally omitted or not.
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.
I worry that this change may be unsound (allowing accessing an unassigned variable). Please add a test for:
public class C
{
public void M()
{
int i;
local();
i.ToString();
[Conditional("DEBUG")]
void local()
{
i = 1;
}
}
}
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 will be good to add this test. I believe it will produce the desired error on i.ToString()
because we reset the definite assignment state after visiting the call when callsAreOmitted
is true.
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.
Oh wait. I forgot that we disallow non-static
local functions from having ConditionalAttribute
at all. We have specified that a local function must be static
in order to be conditional.
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.
we disallow non-static local functions from having ConditionalAttribute at all.
That saved some extra debugging 😄
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.
@jcouv Please take another look at your convenience. I do not think there is a supported scenario involving capturing variables in a conditional local function.
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.
Sounds good. That's great.
Can we update the test (LocalFunction_ConditionalAttributeDisallowed
). We should be able to remove the #pragma warning
.
src/Compilers/CSharp/Test/Semantic/FlowAnalysis/LocalFunctions.cs
Outdated
Show resolved
Hide resolved
CI is failing due to the following test roslyn/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenLocalFunctionTests.cs Lines 5513 to 5540 in 1ed3ef5
Should I delete my test and update the expected for the failing test? |
@Youssef1313 I went in and made the edits to the tests. Thanks for tracking down the root cause of this 😄 I feel confident we want to make the behavior change given by this PR, since it brings us into alignment with how unused local variable warnings work: using System.Diagnostics;
int x = 42;
local(x); // comment out this call and warning CS0219 is introduced, regardless of build configuration
[Conditional("DEBUG")]
static void local(int x1) { } |
@dotnet/roslyn-compiler Please review this very straightforward bug fix |
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.
LGTM Thanks (iteration 3). Consider updating existing test for [Conditional] on non-static local function.
@jcouv The test you're referring to should fail if we removed the #pragma warning because the local function there is indeed unused. The PR removes the warning only if the method is actually used. |
Thanks @Youssef1313! |
Fixes #47463