Skip to content

Commit ad3bc71

Browse files
authored
Merge pull request #11455 from ethereum/issue-11381
Fix: Allow multiple @return tags on public state variables
2 parents 1f8f1a3 + 354f9d1 commit ad3bc71

File tree

6 files changed

+148
-47
lines changed

6 files changed

+148
-47
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Bugfixes:
2020
* Code Generator: Fix internal error when super would have to skip an unimplemented function in the virtual resolution order.
2121
* Control Flow Graph: Take internal calls to functions that always revert into account for reporting unused or unassigned variables.
2222
* Control Flow Graph: Assume unimplemented modifiers use a placeholder.
23+
* Natspec: Allow multiple ``@return`` tags on public state variable documentation.
2324
* SMTChecker: Fix internal error on struct constructor with fixed bytes member initialized with string literal.
2425
* SMTChecker: Fix internal error on external calls from the constructor.
2526
* SMTChecker: Fix internal error on conversion from ``bytes`` to ``fixed bytes``.

libsolidity/analysis/DocStringTagParser.cpp

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,68 @@ bool DocStringTagParser::parseDocStrings(SourceUnit const& _sourceUnit)
4747
return errorWatcher.ok();
4848
}
4949

50+
bool DocStringTagParser::validateDocStringsUsingTypes(SourceUnit const& _sourceUnit)
51+
{
52+
ErrorReporter::ErrorWatcher errorWatcher = m_errorReporter.errorWatcher();
53+
54+
SimpleASTVisitor visitReturns(
55+
[](ASTNode const&) { return true; },
56+
[&](ASTNode const& _node)
57+
{
58+
if (auto const* annotation = dynamic_cast<StructurallyDocumentedAnnotation const*>(&_node.annotation()))
59+
{
60+
auto const& documentationNode = dynamic_cast<StructurallyDocumented const&>(_node);
61+
62+
size_t returnTagsVisited = 0;
63+
64+
for (auto const& [tagName, tagValue]: annotation->docTags)
65+
if (tagName == "return")
66+
{
67+
returnTagsVisited++;
68+
vector<string> returnParameterNames;
69+
70+
if (auto const* varDecl = dynamic_cast<VariableDeclaration const*>(&_node))
71+
{
72+
if (!varDecl->isPublic())
73+
continue;
74+
75+
// FunctionType() requires the DeclarationTypeChecker to have run.
76+
returnParameterNames = FunctionType(*varDecl).returnParameterNames();
77+
}
78+
else if (auto const* function = dynamic_cast<FunctionDefinition const*>(&_node))
79+
returnParameterNames = FunctionType(*function).returnParameterNames();
80+
else
81+
continue;
82+
83+
string content = tagValue.content;
84+
string firstWord = content.substr(0, content.find_first_of(" \t"));
85+
86+
if (returnTagsVisited > returnParameterNames.size())
87+
m_errorReporter.docstringParsingError(
88+
2604_error,
89+
documentationNode.documentation()->location(),
90+
"Documentation tag \"@" + tagName + " " + content + "\"" +
91+
" exceeds the number of return parameters."
92+
);
93+
else
94+
{
95+
string const& parameter = returnParameterNames.at(returnTagsVisited - 1);
96+
if (!parameter.empty() && parameter != firstWord)
97+
m_errorReporter.docstringParsingError(
98+
5856_error,
99+
documentationNode.documentation()->location(),
100+
"Documentation tag \"@" + tagName + " " + content + "\"" +
101+
" does not contain the name of its return parameter."
102+
);
103+
}
104+
}
105+
}
106+
});
107+
108+
_sourceUnit.accept(visitReturns);
109+
return errorWatcher.ok();
110+
}
111+
50112
bool DocStringTagParser::visit(ContractDefinition const& _contract)
51113
{
52114
static set<string> const validTags = set<string>{"author", "title", "dev", "notice"};
@@ -169,7 +231,6 @@ void DocStringTagParser::parseDocStrings(
169231

170232
_annotation.docTags = DocStringParser{*_node.documentation(), m_errorReporter}.parse();
171233

172-
size_t returnTagsVisited = 0;
173234
for (auto const& [tagName, tagValue]: _annotation.docTags)
174235
{
175236
string static const customPrefix("custom:");
@@ -196,43 +257,6 @@ void DocStringTagParser::parseDocStrings(
196257
_node.documentation()->location(),
197258
"Documentation tag @" + tagName + " not valid for " + _nodeName + "."
198259
);
199-
else if (tagName == "return")
200-
{
201-
returnTagsVisited++;
202-
if (auto const* varDecl = dynamic_cast<VariableDeclaration const*>(&_node))
203-
{
204-
solAssert(varDecl->isPublic(), "@return is only allowed on public state-variables.");
205-
if (returnTagsVisited > 1)
206-
m_errorReporter.docstringParsingError(
207-
5256_error,
208-
_node.documentation()->location(),
209-
"Documentation tag \"@" + tagName + "\" is only allowed once on state-variables."
210-
);
211-
}
212-
else if (auto const* function = dynamic_cast<FunctionDefinition const*>(&_node))
213-
{
214-
string content = tagValue.content;
215-
string firstWord = content.substr(0, content.find_first_of(" \t"));
216-
217-
if (returnTagsVisited > function->returnParameters().size())
218-
m_errorReporter.docstringParsingError(
219-
2604_error,
220-
_node.documentation()->location(),
221-
"Documentation tag \"@" + tagName + " " + tagValue.content + "\"" +
222-
" exceeds the number of return parameters."
223-
);
224-
else
225-
{
226-
auto parameter = function->returnParameters().at(returnTagsVisited - 1);
227-
if (!parameter->name().empty() && parameter->name() != firstWord)
228-
m_errorReporter.docstringParsingError(
229-
5856_error,
230-
_node.documentation()->location(),
231-
"Documentation tag \"@" + tagName + " " + tagValue.content + "\"" +
232-
" does not contain the name of its return parameter."
233-
);
234-
}
235-
}
236-
}
237260
}
238261
}
262+

libsolidity/analysis/DocStringTagParser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class DocStringTagParser: private ASTConstVisitor
3737
public:
3838
explicit DocStringTagParser(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter) {}
3939
bool parseDocStrings(SourceUnit const& _sourceUnit);
40+
/// Validate the parsed doc strings, requires parseDocStrings() and the
41+
/// DeclarationTypeChecker to have run.
42+
bool validateDocStringsUsingTypes(SourceUnit const& _sourceUnit);
4043

4144
private:
4245
bool visit(ContractDefinition const& _contract) override;

libsolidity/interface/CompilerStack.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,6 @@ bool CompilerStack::analyze()
416416
if (source->ast && !syntaxChecker.checkSyntax(*source->ast))
417417
noErrors = false;
418418

419-
DocStringTagParser docStringTagParser(m_errorReporter);
420-
for (Source const* source: m_sourceOrder)
421-
if (source->ast && !docStringTagParser.parseDocStrings(*source->ast))
422-
noErrors = false;
423-
424419
m_globalContext = make_shared<GlobalContext>();
425420
// We need to keep the same resolver during the whole process.
426421
NameAndTypeResolver resolver(*m_globalContext, m_evmVersion, m_errorReporter);
@@ -437,6 +432,12 @@ bool CompilerStack::analyze()
437432

438433
resolver.warnHomonymDeclarations();
439434

435+
DocStringTagParser docStringTagParser(m_errorReporter);
436+
for (Source const* source: m_sourceOrder)
437+
if (source->ast && !docStringTagParser.parseDocStrings(*source->ast))
438+
noErrors = false;
439+
440+
// Requires DocStringTagParser
440441
for (Source const* source: m_sourceOrder)
441442
if (source->ast && !resolver.resolveNamesAndTypes(*source->ast))
442443
return false;
@@ -446,6 +447,11 @@ bool CompilerStack::analyze()
446447
if (source->ast && !declarationTypeChecker.check(*source->ast))
447448
return false;
448449

450+
// Requires DeclarationTypeChecker to have run
451+
for (Source const* source: m_sourceOrder)
452+
if (source->ast && !docStringTagParser.validateDocStringsUsingTypes(*source->ast))
453+
noErrors = false;
454+
449455
// Next, we check inheritance, overrides, function collisions and other things at
450456
// contract or function level.
451457
// This also calculates whether a contract is abstract, which is needed by the

test/libsolidity/SolidityNatspecJSON.cpp

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,73 @@ BOOST_AUTO_TEST_CASE(public_state_variable)
328328
checkNatspec(sourceCode, "test", userDoc, true);
329329
}
330330

331+
BOOST_AUTO_TEST_CASE(public_state_variable_struct)
332+
{
333+
char const* sourceCode = R"(
334+
contract Bank {
335+
struct Coin {
336+
string observeGraphicURL;
337+
string reverseGraphicURL;
338+
}
339+
340+
/// @notice Get the n-th coin I own
341+
/// @return observeGraphicURL Front pic
342+
/// @return reverseGraphicURL Back pic
343+
Coin[] public coinStack;
344+
}
345+
)";
346+
347+
char const* devDoc = R"R(
348+
{
349+
"methods" : {},
350+
"stateVariables" :
351+
{
352+
"coinStack" :
353+
{
354+
"returns" :
355+
{
356+
"observeGraphicURL" : "Front pic",
357+
"reverseGraphicURL" : "Back pic"
358+
}
359+
}
360+
}
361+
}
362+
)R";
363+
checkNatspec(sourceCode, "Bank", devDoc, false);
364+
365+
char const* userDoc = R"R(
366+
{
367+
"methods" :
368+
{
369+
"coinStack(uint256)" :
370+
{
371+
"notice": "Get the n-th coin I own"
372+
}
373+
}
374+
}
375+
)R";
376+
checkNatspec(sourceCode, "Bank", userDoc, true);
377+
}
378+
379+
BOOST_AUTO_TEST_CASE(public_state_variable_struct_repeated)
380+
{
381+
char const* sourceCode = R"(
382+
contract Bank {
383+
struct Coin {
384+
string obverseGraphicURL;
385+
string reverseGraphicURL;
386+
}
387+
388+
/// @notice Get the n-th coin I own
389+
/// @return obverseGraphicURL Front pic
390+
/// @return obverseGraphicURL Front pic
391+
Coin[] public coinStack;
392+
}
393+
)";
394+
395+
expectNatspecError(sourceCode);
396+
}
397+
331398
BOOST_AUTO_TEST_CASE(private_state_variable)
332399
{
333400
char const* sourceCode = R"(
@@ -1274,7 +1341,7 @@ BOOST_AUTO_TEST_CASE(dev_default_inherit_variable)
12741341

12751342
char const *natspec1 = R"ABCDEF({
12761343
"methods" : {},
1277-
"stateVariables" :
1344+
"stateVariables" :
12781345
{
12791346
"x" :
12801347
{
@@ -1340,7 +1407,7 @@ BOOST_AUTO_TEST_CASE(dev_explicit_inherit_variable)
13401407

13411408
char const *natspec1 = R"ABCDEF({
13421409
"methods" : {},
1343-
"stateVariables" :
1410+
"stateVariables" :
13441411
{
13451412
"x" :
13461413
{

test/libsolidity/syntaxTests/natspec/docstring_state_variable_too_many_return_tags.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ contract test {
66
uint public state;
77
}
88
// ----
9-
// DocstringParsingError 5256: (18-137): Documentation tag "@return" is only allowed once on state-variables.
9+
// DocstringParsingError 2604: (18-137): Documentation tag "@return returns something" exceeds the number of return parameters.

0 commit comments

Comments
 (0)