Skip to content

Fix extra warning for unused local function#47473

Merged
333fred merged 3 commits intodotnet:masterfrom
Youssef1313:47463
Sep 9, 2020
Merged

Fix extra warning for unused local function#47473
333fred merged 3 commits intodotnet:masterfrom
Youssef1313:47463

Conversation

@Youssef1313
Copy link
Member

Fixes #47463

@Youssef1313 Youssef1313 requested a review from a team as a code owner September 4, 2020 23:23
VisitArgumentsBeforeCall(node.Arguments, node.ArgumentRefKindsOpt);

if (!callsAreOmitted && node.Method?.OriginalDefinition is LocalFunctionSymbol localFunc)
if (node.Method?.OriginalDefinition is LocalFunctionSymbol localFunc)
Copy link
Member Author

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.

Copy link
Member

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;   
        }
    }
}

Copy link
Member

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.

Copy link
Member

@RikkiGibson RikkiGibson Sep 7, 2020

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.

Copy link
Member Author

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 😄

Copy link
Member

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.

Copy link
Member

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.

@Youssef1313
Copy link
Member Author

Youssef1313 commented Sep 5, 2020

CI is failing due to the following test

[Fact]
public void StaticLocalFunction_ConditionalAttribute_Unreferenced()
{
var source = @"
using System.Diagnostics;
using System;
class C
{
static void Main()
{
local1();
[Conditional(""DEBUG"")]
static void local1()
{
Console.Write(""hello"");
}
}
}
";
CreateCompilation(source, parseOptions: TestOptions.Regular9).VerifyDiagnostics(
// (12,21): warning CS8321: The local function 'local1' is declared but never used
// static void local1()
Diagnostic(ErrorCode.WRN_UnreferencedLocalFunction, "local1").WithArguments("local1").WithLocation(12, 21));
CreateCompilation(source, parseOptions: TestOptions.Regular9.WithPreprocessorSymbols("DEBUG")).VerifyDiagnostics();
}

Should I delete my test and update the expected for the failing test?

@RikkiGibson RikkiGibson added Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee. labels Sep 5, 2020
@RikkiGibson
Copy link
Member

RikkiGibson commented Sep 5, 2020

@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) { }

@RikkiGibson RikkiGibson requested a review from a team September 7, 2020 19:58
@RikkiGibson
Copy link
Member

@dotnet/roslyn-compiler Please review this very straightforward bug fix

@jcouv jcouv self-assigned this Sep 9, 2020
Copy link
Member

@jcouv jcouv left a 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.

@Youssef1313
Copy link
Member Author

@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.

@333fred 333fred merged commit ebdda46 into dotnet:master Sep 9, 2020
@ghost ghost added this to the Next milestone Sep 9, 2020
@333fred
Copy link
Member

333fred commented Sep 9, 2020

Thanks @Youssef1313!

@Youssef1313 Youssef1313 deleted the 47463 branch September 9, 2020 21:52
@dibarbet dibarbet modified the milestones: Next, 16.8.P4 Sep 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conditional Local Functions are considered unused

5 participants