Skip to content
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

[semi-auto-props]: Avoid using IsAutoProperty when possible #59109

Merged

Conversation

Youssef1313
Copy link
Member

Test plan: #57012

@Youssef1313 Youssef1313 requested a review from a team as a code owner January 27, 2022 11:30
@ghost ghost added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Jan 27, 2022
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers and removed Community The pull request was submitted by a contributor who is not a Microsoft employee. labels Jan 27, 2022
Comment on lines 798 to 799
// PROTOTYPE(semi-auto-props): Implement this.
return IsAutoProperty;
Copy link
Member Author

Choose a reason for hiding this comment

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

Will this require extra binding? 😕

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 require some amount of binding, certainly, but I think it won't require fully binding the body. We'll just need to check to see whether any identifiers named field are used without a field being in scope.

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'll just need to check to see whether any identifiers named field are used without a field being in scope.

Any pointer on how can I see if there are identifiers named field without needing to fully bind the bind? Introducing a new BinderFlag for example? (I know it's unlikely we want to introduce a binder flag for this, but can't think of the correct way to do it)

Copy link
Member

Choose a reason for hiding this comment

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

My thought is that we can walk the binder chain, looking for anything in scope named field. IE, locals or fields. I do think we should ask the LDM what this code means though:

int Prop
{
    set
    {
        field = value;
        {
            int field = 0; // Does this shadow the backing field? Or is there an error reported somewhere?
        }
    }
}

Copy link
Member Author

@Youssef1313 Youssef1313 Jan 28, 2022

Choose a reason for hiding this comment

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

@333fred Do I need to open an issue for that in dotnet/csharplang?

cc @CyrusNajmabadi

Copy link
Contributor

@AlekseyTs AlekseyTs Jan 28, 2022

Choose a reason for hiding this comment

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

Do I need to open an issue for that in dotnet/csharplang?

I think there is no design question here. The specification is clear: "The field identifier is only considered the field keyword when there is no existing symbol named field in scope at that location." We can certainly have different implementation strategies, but for now I would stick with the simplistic approach of simply binding the body. Anything else is more complexity and more risk.

Copy link
Member

Choose a reason for hiding this comment

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

@333fred Do I need to open an issue for that in dotnet/csharplang?

Yes, let's get that in an issue, or in the spec.

@Youssef1313
Copy link
Member Author

@AlekseyTs For review.

Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

LGTM (commit 4)

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 4)

@AlekseyTs
Copy link
Contributor

@Youssef1313 Please use meaningful names for PRs. Each PR has a goal and that goal should be reflected in the title.

@Youssef1313 Youssef1313 changed the title [semi-auto-props]: Minor refactoring [semi-auto-props]: Avoid using IsAutoProperty when possible Jan 28, 2022
@Youssef1313 Youssef1313 requested a review from AlekseyTs February 4, 2022 08:20
{
// PROTOTYPE(semi-auto-props): Fix implementation for semi auto properties.
return (_setMethod is null && _getMethod?.BodyShouldBeSynthesized == true) ||
_setMethod?.BodyShouldBeSynthesized == true;
Copy link
Contributor

@AlekseyTs AlekseyTs Feb 4, 2022

Choose a reason for hiding this comment

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

I am not sure if using the same implementation as for AllowInitializer is the right thing to do. Earlier I was suggesting the following: "Could this be as simple as checking if getter exists and is auto-implemented instead?" It feels like that logic will exactly match the name of the property that we are replacing, IsAutoPropertyWithGetAccessor. I could imagine even more relaxed implementation. Something like

_getMethod?.BodyShouldBeSynthesized == true ||
                    _setMethod?.BodyShouldBeSynthesized == true

For an attribute, we just need to have a field. Requirements for an initializer are more strict. Especially taking https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-01-12.md#conclusion into consideration. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

_getMethod?.BodyShouldBeSynthesized == true ||
                    _setMethod?.BodyShouldBeSynthesized == true

@AlekseyTs Both of the logics don't seem to take semi auto properties into account, unless I'm missing something. For example, public string S { get => field; } should be allowed to have a field attribute target. But I agree your logic is a bit more accurate.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 6)

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 7)

@AlekseyTs AlekseyTs merged commit 7b4739b into dotnet:features/semi-auto-props Feb 8, 2022
@AlekseyTs
Copy link
Contributor

@Youssef1313 Thanks for the contribution.

@Youssef1313 Youssef1313 deleted the semi-refactor2 branch February 8, 2022 23:40
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.

3 participants