Skip to content

Analyze flow in receiver of extension method group in delegate creation#59874

Merged
RikkiGibson merged 3 commits intodotnet:mainfrom
jcouv:bad-region
Mar 8, 2022
Merged

Analyze flow in receiver of extension method group in delegate creation#59874
RikkiGibson merged 3 commits intodotnet:mainfrom
jcouv:bad-region

Conversation

@jcouv
Copy link
Copy Markdown
Member

@jcouv jcouv commented Mar 2, 2022

Fixes #59738

Below is the current behavior of a test that illustrates the problem. The problem occurs when creating a delegate with a method group, using an extension method. In such case, we fail to visit the receiver of the extension method.

Note that local function invocations can have a receiver (implicit this) so the local function case was moved to be handled first.

The behavior in the reported issue also results from our not visiting the receiver. The analysis keeps track of when we enter/exit the region marked with /*<bind>*/ and since we didn't visit the expression, the state of the visitor was stuck on "before" which results in a failed analysis.

        [Fact, WorkItem(59738, "https://github.com/dotnet/roslyn/issues/59738")]
        public void DefiniteAssignmentInReceiverOfExtensionMethodInDelegateCreation()
        {
            var comp = CreateCompilation(@"
using System;
bool b = true;
_ = new Func<string>((b ? M(out var i) : i.ToString()).ExtensionMethod);

string M(out int i)
{
    throw null;
}

static class Extension
{
    public static string ExtensionMethod(this string s) => throw null;
}
	");
            comp.VerifyDiagnostics(
                // (3,6): warning CS0219: The variable 'b' is assigned but its value is never used
                // bool b = true;
                Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "b").WithArguments("b").WithLocation(3, 6),
                // (4,37): warning CS0168: The variable 'i' is declared but never used
                // _ = new Func<string>((b ? M(out var i) : i.ToString()).ExtensionMethod);
                Diagnostic(ErrorCode.WRN_UnreferencedVar, "i").WithArguments("i").WithLocation(4, 37),
                // (6,8): warning CS8321: The local function 'M' is declared but never used
                // string M(out int i)
                Diagnostic(ErrorCode.WRN_UnreferencedLocalFunction, "M").WithArguments("M").WithLocation(6, 8)
                );
        }

Co-Authored-By: Bernd Baumanns baumannsbernd@gmail.com

@ghost ghost added the Area-Compilers label Mar 2, 2022
@jcouv jcouv self-assigned this Mar 2, 2022
@jcouv jcouv marked this pull request as ready for review March 2, 2022 06:31
@jcouv jcouv requested a review from a team as a code owner March 2, 2022 06:31
@jcouv jcouv requested a review from RikkiGibson March 2, 2022 19:21
@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Mar 2, 2022

@dotnet/roslyn-compiler for second review. Thanks

1 similar comment
@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Mar 4, 2022

@dotnet/roslyn-compiler for second review. Thanks

@jcouv jcouv enabled auto-merge (squash) March 8, 2022 00:41
@RikkiGibson RikkiGibson disabled auto-merge March 8, 2022 17:51
@RikkiGibson RikkiGibson merged commit d5c084a into dotnet:main Mar 8, 2022
@ghost ghost added this to the Next milestone Mar 8, 2022
333fred added a commit that referenced this pull request Mar 10, 2022
333fred added a commit that referenced this pull request Mar 10, 2022
@allisonchou allisonchou modified the milestones: Next, 17.2.P3 Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AnalyzeDataFlow fails

4 participants