Skip to content

Add support for enum values in NatSpec #15956

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

Merged
merged 2 commits into from
Apr 4, 2025

Conversation

nikola-matic
Copy link
Collaborator

@nikola-matic nikola-matic commented Mar 19, 2025

References #12295.
Closes #14193.
Thanks @veniger for the original PR.

@nikola-matic nikola-matic added this to the 0.8.30 milestone Mar 19, 2025
@nikola-matic nikola-matic self-assigned this Mar 19, 2025
matheusaaguiar
matheusaaguiar previously approved these changes Mar 20, 2025
@@ -430,6 +430,7 @@ bool ASTJsonExporter::visit(EnumValue const& _node)
setJsonNode(_node, "EnumValue", {
std::make_pair("name", _node.name()),
std::make_pair("nameLocation", sourceLocationToString(_node.nameLocation())),
std::make_pair("documentation", _node.documentation() ? toJson(*_node.documentation()) : Json()),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could also use the overload toJson(ASTNode*) which already tests and returns the proper value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cameel unfortunately, this doesn't quite work in the case when a natspec doc is provided beyond the last value, as the natspec comment is not associated with any _node, which then segfaults here.

Going over the exporter in more detail now, and we perform a node.documentation() check for every single export, i.e. we don't use toJson(*_node.documentation()) by itself.

I'll therefore keep this as is.

Copy link
Collaborator

@matheusaaguiar matheusaaguiar Apr 2, 2025

Choose a reason for hiding this comment

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

No problem keeping it as is, but this works for me:

Suggested change
std::make_pair("documentation", _node.documentation() ? toJson(*_node.documentation()) : Json()),
std::make_pair("documentation", toJson(&*_node.documentation()))

or

Suggested change
std::make_pair("documentation", _node.documentation() ? toJson(*_node.documentation()) : Json()),
std::make_pair("documentation", toJson(_node.documentation().get()))

I guess you forgot to pass the pointer and it is resolving to the overload which takes a reference instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Aaah OK, guess I'm blind having not seen the second toJson() overload that was right below the one I was debugging.

Changelog.md Outdated
@@ -4,6 +4,7 @@ Language Features:


Compiler Features:
* NatSpec: Add support for NatSpec documentation in ``enum`` value definitions.
Copy link
Member

@cameel cameel Mar 21, 2025

Choose a reason for hiding this comment

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

I see that this is how we were describing such additions in the past, but "support" is quite ambiguous. This Natspec will not show up in --userdoc or --devdoc output. Using the tags also wasn't even disallowed on enum values before. The only thing that changes is that it's now captured in the AST. How about we say just that?

Suggested change
* NatSpec: Add support for NatSpec documentation in ``enum`` value definitions.
* NatSpec: Capture Natspec documentation of `enum` values in the AST.

Perhaps this should also be stated in the docs. Right now they're rather vague and even incorrect, depending on how you define "support". After reading them and checking this PR I'm really confused about what we actually support.

Copy link
Member

Choose a reason for hiding this comment

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

For completeness, we should also test an enum defined outside of a contract.

Copy link
Member

Choose a reason for hiding this comment

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

And I'd also test what happens with Natspec that's after the last value. Is it ignored or does it cause an error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For completeness, we should also test an enum defined outside of a contract.

Isn't this one technically defined outside of a contract?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And I'd also test what happens with Natspec that's after the last value. Is it ignored or does it cause an error?

Good call, it segfaults in the ASTJSON test, but does not in the natspecJSON one, so it's likely an issue in the JSON exporter. Will investigate and fix.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As it turns out, it wasn't segfaulting initially, but only after I applied Matheus' suggestion. Right now, it simply ignores any natspec doc comment that isn't associated with an actual AST node (in this case enum value), and this looks to be the case across the board for all such comments/docs.

@cameel
Copy link
Member

cameel commented Mar 21, 2025

@nikola-matic Please link to the original PR in the description (#14193). Since this PR completely replaces it, you can even do Closes #14193.

@nikola-matic nikola-matic force-pushed the make-enums-available-in-natspec branch 2 times, most recently from ef742a7 to 7da89a9 Compare April 2, 2025 11:17
matheusaaguiar
matheusaaguiar previously approved these changes Apr 2, 2025
@nikola-matic nikola-matic merged commit 949a52f into develop Apr 4, 2025
74 checks passed
@nikola-matic nikola-matic deleted the make-enums-available-in-natspec branch April 4, 2025 12:44
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.

3 participants