Skip to content

Fix generate event handler with invalid name (#59935)#60810

Merged
jasonmalinowski merged 3 commits intodotnet:mainfrom
maciek231:GenerateEventHandlerNameForEventsInGenericClass
Apr 27, 2022
Merged

Fix generate event handler with invalid name (#59935)#60810
jasonmalinowski merged 3 commits intodotnet:mainfrom
maciek231:GenerateEventHandlerNameForEventsInGenericClass

Conversation

@maciek231
Copy link
Copy Markdown
Contributor

Fix #59935

@maciek231 maciek231 requested a review from a team as a code owner April 17, 2022 17:29
@ghost ghost added Community The pull request was submitted by a contributor who is not a Microsoft employee. Area-IDE and removed Community The pull request was submitted by a contributor who is not a Microsoft employee. labels Apr 17, 2022
{
void M()
{
Generic<int>.MyEvent +$$
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for global::Generic<int>.MyEvent? I'm expecting it to fail.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For that, I suggest using this extension method

public static SimpleNameSyntax? GetRightmostName(this ExpressionSyntax node)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it failed both for generic and non-generic. I added more tests and fix that

@jinujoseph jinujoseph added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Apr 19, 2022
// Even easier -- the LHS of the dot is the name itself
return lhsNameSyntax.ToString();
}
return lhs.ToString();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetRightmostName could return null (at least it's annotated as such), does this need a null check? My gut is it seems like you'd need to have something malformed to the left of the period but still somehow have succeeded in binding the event, so that seems like it'd be a difficult thing to achieve.

Copy link
Copy Markdown
Member

@Youssef1313 Youssef1313 Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasonmalinowski Nice catch!

But will it really be something malformed? I think it can be null in a completely valid scenario like this?

using System;

class C
{
    public event EventHandler MyEvent;
    
    public static C CreateC()
    {
        return new C();
    }
    
    public void M2()
    {
        CreateC().MyEvent +=
    }
}

Here memberAccessExpression.Expression is InvocationExpressionSyntax where GetRightmostName returns null. We need to fall into syntaxFactsService below when it's null, I think.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Youssef1313 Ah right, of course. I was mentally reading through the GetRightmostName() then forgetting that we're passing in the LHS of the original dot, so yeah. If we don't have a test for that case, we should add one regardless,

}

if (lhs is NameSyntax lhsNameSyntax)
if (lhs != null)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lhs is not null maybe

Copy link
Copy Markdown
Member

@jasonmalinowski jasonmalinowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@jasonmalinowski jasonmalinowski merged commit 5a153e0 into dotnet:main Apr 27, 2022
@ghost ghost added this to the Next milestone Apr 27, 2022
@jasonmalinowski
Copy link
Copy Markdown
Member

Thanks @maciek231 for the fix!

@RikkiGibson RikkiGibson modified the milestones: Next, 17.3 P3 Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE 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.

The name of the generated event handler is invalid for generics classes.

6 participants