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

Add support for enum values in NatSpec #15956

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

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
@@ -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.

// "kind": "user",
// "methods": {},
// "version": 1
// }
Copy link
Collaborator

Choose a reason for hiding this comment

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

The test is the same as the other one?

Copy link
Member

Choose a reason for hiding this comment

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

Looks like this is also the case for docstring_enum.sol/enum_no_docs.sol these seem to be based on.

Apparently both were added in #13626, but docstring_enum.sol was originally a syntax test. Since Natspec JSON tests are a superset of syntax tests (they do full analysis and IIRC can also display errors the way syntax tests do), when I created it, I moved some Natspec-related syntax tests there (dc68480). So I could have just removed it, because it is simply redundant.

We should do that now. Let's leave just one of each and call them enum.sol and enum_value.sol.

Comment on lines +4 to +8
/// @title example of title
/// @author example of author
/// @notice example of notice
/// @dev example of dev
Green
Copy link
Member

Choose a reason for hiding this comment

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

I see that we accept @param, @return and @inheritdoc as well. These do not make sense for enum values. We should disallow them. And have a test covering them.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, looks like they're also allowed on enums in general. Or structs. And they would compile on enum values before this PR as well, they were just not included in the AST. That would make disallowing them breaking...

And to complicate things even more, looks like Foundry is planning to actually interpret @param on enums as referring to the values: foundry-rs/foundry#10022 (review).

So I don't think we can really disallow them, but then we should document them. Natspec Format > Tags does not list @param, @return and @inheritdoc for enums, structs and probably other things that support them, even though these values are not only allowed but also captured as StructuredDocumentation in the AST.

Copy link
Member

Choose a reason for hiding this comment

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

The tags allowed for enum values should be documented in Natspec Format > Tags.

Unless it is assumed to be covered by enum? I guess struct fields allow Natspec as well and they are not explicitly listed there. On the other hand we don't capture Natspec in AST for struct fields but we now do for enum values so the situation is not the same.

Copy link
Member

Choose a reason for hiding this comment

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

Enums are also not mentioned at all in this bit:

Documentation is inserted above each contract, interface, library, function, and event using the Doxygen notation format

/// @title example of title
/// @author example of author
/// @notice example of notice
/// @dev example of dev
Copy link
Member

Choose a reason for hiding this comment

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

Please include a @custom tag as well.

@@ -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?

@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.

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