Skip to content

Commit

Permalink
Merge pull request ethereum#3790 from ethereum/empty-structs
Browse files Browse the repository at this point in the history
Disallow empty structs
  • Loading branch information
chriseth authored Mar 30, 2018
2 parents 58c57e4 + ebb1275 commit 326d656
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Features:
* Commandline interface: Error when missing or inaccessible file detected. Suppress it with the ``--ignore-missing`` flag.
* General: Support accessing dynamic return data in post-byzantium EVMs.
* Interfaces: Allow overriding external functions in interfaces with public in an implementing contract.
* Syntax Checker: Issue warning for empty structs (or error as experimental 0.5.0 feature).

Bugfixes:
* Code Generator: Allow ``block.blockhash`` without being called.
Expand Down
2 changes: 1 addition & 1 deletion docs/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ InheritanceSpecifier = UserDefinedTypeName ( '(' Expression ( ',' Expression )*
StateVariableDeclaration = TypeName ( 'public' | 'internal' | 'private' | 'constant' )? Identifier ('=' Expression)? ';'
UsingForDeclaration = 'using' Identifier 'for' ('*' | TypeName) ';'
StructDefinition = 'struct' Identifier '{'
( VariableDeclaration ';' (VariableDeclaration ';')* )? '}'
( VariableDeclaration ';' (VariableDeclaration ';')* ) '}'

ModifierDefinition = 'modifier' Identifier ParameterList? Block
ModifierInvocation = Identifier ( '(' ExpressionList? ')' )?
Expand Down
14 changes: 14 additions & 0 deletions libsolidity/analysis/SyntaxChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,17 @@ bool SyntaxChecker::visit(VariableDeclaration const& _declaration)
}
return true;
}

bool SyntaxChecker::visit(StructDefinition const& _struct)
{
bool const v050 = m_sourceUnit->annotation().experimentalFeatures.count(ExperimentalFeature::V050);

if (_struct.members().empty())
{
if (v050)
m_errorReporter.syntaxError(_struct.location(), "Defining empty structs is disallowed.");
else
m_errorReporter.warning(_struct.location(), "Defining empty structs is deprecated.");
}
return true;
}
2 changes: 2 additions & 0 deletions libsolidity/analysis/SyntaxChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class SyntaxChecker: private ASTConstVisitor

virtual bool visit(VariableDeclaration const& _declaration) override;

virtual bool visit(StructDefinition const& _struct) override;

ErrorReporter& m_errorReporter;

/// Flag that indicates whether a function modifier actually contains '_'.
Expand Down
2 changes: 1 addition & 1 deletion test/libsolidity/SolidityNameAndTypeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7289,7 +7289,7 @@ BOOST_AUTO_TEST_CASE(modifiers_access_storage_pointer)
{
char const* text = R"(
contract C {
struct S { }
struct S { uint a; }
modifier m(S storage x) {
x;
_;
Expand Down
5 changes: 5 additions & 0 deletions test/libsolidity/syntaxTests/empty_struct.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
contract test {
struct A {}
}
// ----
// Warning: Defining empty structs is deprecated.
6 changes: 6 additions & 0 deletions test/libsolidity/syntaxTests/empty_struct_050.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pragma experimental "v0.5.0";
contract test {
struct A {}
}
// ----
// SyntaxError: Defining empty structs is disallowed.

0 comments on commit 326d656

Please sign in to comment.