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

Editions uses identifiers instead of string literals for reserved names #176

Merged
merged 2 commits into from
Aug 23, 2023

Conversation

jhump
Copy link
Member

@jhump jhump commented Aug 23, 2023

This implements the use of identifiers, instead of string literals, in reserved statements. This new syntax is for editions; proto2 and proto3 continue to use string literals for backwards compatibility.

This effectively ports protocolbuffers/protobuf#13471 to protocompile.

@jhump jhump requested a review from pkwarren August 23, 2023 16:35
parser/result.go Outdated
ed := &descriptorpb.EnumDescriptorProto{Name: proto.String(en.Name.Val)}
r.putEnumNode(ed, en)
rsvdNames := map[string]int{}
Copy link
Member

Choose a reason for hiding this comment

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

Small nit - looks like we are only using the count to determine if it is >1. We could tweak to use struct{} value instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

This was intentional to de-duplicate the errors. We only report the first duplicate. But perhaps it would be okay (or even better?) to report them all. They are currently de-duplicated via use of int because we only report when count == 1, not when it's >= 1.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, I changed this so it actually stores the position of the first declaration and can report that in the error message.

nameNodeInfo := r.file.NodeInfo(node.Identifiers[0])
_ = handler.HandleErrorf(nameNodeInfo.Start(), `must use string literals, not identifiers, to reserved names with proto2 and proto3`)
}
for _, n := range node.Names {
Copy link
Member

Choose a reason for hiding this comment

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

Should we put this in an if statement for non editions syntax? Feels like we should ignore it entirely in error messages if string literals aren't supported at all.

Copy link
Member Author

Choose a reason for hiding this comment

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

I re-structured this method to do just this. The branch for editions checks that Names is empty and processes Identifiers. The other branch, does the opposite, checking that Identifiers is empty and processes Names.

@jhump jhump requested a review from pkwarren August 23, 2023 18:11
@jhump jhump merged commit dbf43b6 into main Aug 23, 2023
7 checks passed
@jhump jhump deleted the jh/reserved-names-as-identifiers branch August 23, 2023 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants