Skip to content

Allow NatSpec comments for state variables#8532

Merged
chriseth merged 3 commits intoargotorg:developfrom
aarlt:structured-docs-variables-aarlt
May 19, 2020
Merged

Allow NatSpec comments for state variables#8532
chriseth merged 3 commits intoargotorg:developfrom
aarlt:structured-docs-variables-aarlt

Conversation

@aarlt
Copy link
Contributor

@aarlt aarlt commented Mar 24, 2020

Fixes #3418.
replaces #8399

@aarlt aarlt force-pushed the structured-docs-variables-aarlt branch 4 times, most recently from 9e6d5c4 to 84f27c4 Compare March 25, 2020 00:04
@chriseth
Copy link
Contributor

Please squash.

@aarlt aarlt force-pushed the structured-docs-variables-aarlt branch from 0302dd8 to 7c0ee1a Compare March 25, 2020 21:55
@aarlt
Copy link
Contributor Author

aarlt commented Mar 27, 2020

@chriseth i'm not sure whether it is good to forbid notice on non-public state variables. it is used a lot by contract developers. Some examples:

  • colonyNetwork @ contracts/ReputationMiningCycle.sol
contract ReputationMiningCycle is ReputationMiningCycleStorage, PatriciaTreeProofs, DSMath {
  /// @notice Minimum reputation mining stake in CLNY
  uint256 constant MIN_STAKE = 2000 * WAD;

  /// @notice Size of mining window in seconds
  uint256 constant MINING_WINDOW_SIZE = 60 * 60 * 24; // 24 hours
  ...
}
  • OpenZeppelin @ contracts/drafts/ERC20Migrator.sol:
contract ERC20Migrator {
    using SafeERC20 for IERC20;

    /// Address of the old token contract
    IERC20 private _legacyToken;

    /// Address of the new token contract
    ERC20Mintable private _newToken;
    ...
}
  • Solidity Documentation
    contract Fund {
        /// Mapping of ether shares of the contract.
        mapping(address => uint) shares;
        /// Withdraw your share.
        function withdraw() public {
            (bool success,) = msg.sender.call{value: shares[msg.sender]}("");
            if (success)
                shares[msg.sender] = 0;
        }
    }

@aarlt aarlt force-pushed the structured-docs-variables-aarlt branch 12 times, most recently from 8d0f2ec to 7fa485e Compare March 31, 2020 02:29
@chriseth
Copy link
Contributor

chriseth commented Apr 1, 2020

@aarlt @notice is useless on non-public functions. We can add a warning now and disallow it in 0.7.0.

{
if (_variable.isStateVariable())
{
static set<string> const validPublicTags = set<string>{"dev", "notice", "title", "author"};
Copy link
Contributor

Choose a reason for hiding this comment

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

Is author not only for contract?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also title?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, they are normally only allowed on contract. The thing is that some contract authors in the past put this "accidentally" in front of a state variable. It was ok in the past, because there was no check on state variables implemented. Looks like that they never noticed that title and author was never set. However, I added the checks here to inform the authors that this will not be allowed anymore.

else
{
parseDocStrings(_variable, _variable.annotation(), validPublicTags, "non-public state variables");
if (_variable.annotation().docTags.find("notice") != _variable.annotation().docTags.end())
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use contains or count

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oki

if (_variable.annotation().docTags.find("notice") != _variable.annotation().docTags.end())
m_errorReporter.warning(9098_error, _variable.documentation()->location(), "Documentation tag on non-public state variables will be disallowed in 0.7.0. You will need to use the @dev tag explicitly.");
}
if (_variable.annotation().docTags.find("title") != _variable.annotation().docTags.end() ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

}

if (!_options.isStateVariable && documentation != nullptr)
parserWarning(2837_error, "Only state variables can retrieve a docstring. This will be disallowed in 0.7.0.");
Copy link
Contributor

@chriseth chriseth May 12, 2020

Choose a reason for hiding this comment

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

Suggested change
parserWarning(2837_error, "Only state variables can retrieve a docstring. This will be disallowed in 0.7.0.");
parserWarning(2837_error, "Only state variables can have a docstring. This will be disallowed in 0.7.0.");

char const* sourceCode = R"(
contract test {
/// @notice example of notice
/// @dev example of dev
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you check if @returns works here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right now only @dev and @notice is allowed, but it could also make sense to allow @return here too.

Copy link
Contributor Author

@aarlt aarlt May 13, 2020

Choose a reason for hiding this comment

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

For contract

contract test {
  /// @notice example of notice
  /// @dev example of dev
  /// @return returns something
  uint public state;
}

devdoc will now look like this:

{
  "methods": {},
  "stateVariables":
  {
    "state":
    {
      "details": "example of dev",
      "return": "returns something"
    }
  }
}
  • multiple return tags on a state-variable will result in a parsing error.

@aarlt aarlt force-pushed the structured-docs-variables-aarlt branch 5 times, most recently from fb7f327 to c248c78 Compare May 13, 2020 19:34
@chriseth
Copy link
Contributor

Needs rebase.

@aarlt aarlt force-pushed the structured-docs-variables-aarlt branch 2 times, most recently from cab1760 to ce68386 Compare May 14, 2020 17:22
@leonardoalt
Copy link

Needs rebase again. solc-js tests might be fixed then

@aarlt aarlt force-pushed the structured-docs-variables-aarlt branch from ce68386 to 0a38ec8 Compare May 18, 2020 11:05
@chriseth
Copy link
Contributor

Needs rebase.

``@author`` The name of the author contract, interface, function
``@notice`` Explain to an end user what this does contract, interface, function
``@dev`` Explain to a developer any extra details contract, interface, function
``@notice`` Explain to an end user what this does contract, interface, function, state variable
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
``@notice`` Explain to an end user what this does contract, interface, function, state variable
``@notice`` Explain to an end user what this does contract, interface, function, public state variable

``@dev`` Explain to a developer any extra details contract, interface, function, state variable
``@param`` Documents a parameter just like in doxygen (must be followed by parameter name) function
``@return`` Documents the return variables of a contract's function function
``@return`` Documents the return variables of a contract's function function, state variable
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
``@return`` Documents the return variables of a contract's function function, state variable
``@return`` Documents the return variables of a contract's function function, public state variable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thats true, @return should only be allowed on public state-variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added now a check: @return is now only allowed on public state-variables. I added also a test for this.

if (returnTagsVisited > 1)
appendError(
_node.documentation()->location(),
"Documentation tag \"@" + docTag.first + "\" is only allowed once on state-variables.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"Documentation tag \"@" + docTag.first + "\" is only allowed once on state-variables.");
"Documentation tag \"@" + docTag.first + "\" is only allowed once on state-variables."
);

{
parseDocStrings(_variable, _variable.annotation(), validPublicTags, "non-public state variables");
if (_variable.annotation().docTags.count("notice") > 0)
m_errorReporter.warning(9098_error, _variable.documentation()->location(), "Documentation tag on non-public state variables will be disallowed in 0.7.0. You will need to use the @dev tag explicitly.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Please wrap this line.

m_errorReporter.warning(9098_error, _variable.documentation()->location(), "Documentation tag on non-public state variables will be disallowed in 0.7.0. You will need to use the @dev tag explicitly.");
}
if (_variable.annotation().docTags.count("title") > 0 || _variable.annotation().docTags.count("author") > 0)
m_errorReporter.warning(4822_error, _variable.documentation()->location(), "Documentation tag @title and @author is only allowed on contract definitions. It will be disallowed in 0.7.0.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Please wrap this line.

@aarlt aarlt force-pushed the structured-docs-variables-aarlt branch from 0a38ec8 to fc8b934 Compare May 19, 2020 15:57
fulldecent and others added 3 commits May 19, 2020 11:01
- adds natspec generation for state variables.
- exports structured docs for state variables to JSON.
@aarlt aarlt force-pushed the structured-docs-variables-aarlt branch from fc8b934 to af8bb5f Compare May 19, 2020 16:02
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.

Allow NatSpec comments for variables

7 participants