Fix extra warning for unused local function#47473
Conversation
| 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.
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.
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.
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.
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.
we disallow non-static local functions from having ConditionalAttribute at all.
That saved some extra debugging 😄
There was a problem hiding this comment.
@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.
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 |
jcouv
left a comment
There was a problem hiding this comment.
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