diff options
author | chriseth <chris@ethereum.org> | 2018-03-30 19:58:35 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-30 19:58:35 +0800 |
commit | 326d656a552ca247b5c96f1921c9c4c828ce93ad (patch) | |
tree | 0d03f9f5a347a051757e1d35f182eff782228e05 | |
parent | 58c57e446b65b31d8c4573d506c879fe385a51d7 (diff) | |
parent | ebb12756adfe95ae694ed7e141890c6d47e8b7b4 (diff) | |
download | dexon-solidity-326d656a552ca247b5c96f1921c9c4c828ce93ad.tar dexon-solidity-326d656a552ca247b5c96f1921c9c4c828ce93ad.tar.gz dexon-solidity-326d656a552ca247b5c96f1921c9c4c828ce93ad.tar.bz2 dexon-solidity-326d656a552ca247b5c96f1921c9c4c828ce93ad.tar.lz dexon-solidity-326d656a552ca247b5c96f1921c9c4c828ce93ad.tar.xz dexon-solidity-326d656a552ca247b5c96f1921c9c4c828ce93ad.tar.zst dexon-solidity-326d656a552ca247b5c96f1921c9c4c828ce93ad.zip |
Merge pull request #3790 from ethereum/empty-structs
Disallow empty structs
-rw-r--r-- | Changelog.md | 1 | ||||
-rw-r--r-- | docs/grammar.txt | 2 | ||||
-rw-r--r-- | libsolidity/analysis/SyntaxChecker.cpp | 14 | ||||
-rw-r--r-- | libsolidity/analysis/SyntaxChecker.h | 2 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 2 | ||||
-rw-r--r-- | test/libsolidity/syntaxTests/empty_struct.sol | 5 | ||||
-rw-r--r-- | test/libsolidity/syntaxTests/empty_struct_050.sol | 6 |
7 files changed, 30 insertions, 2 deletions
diff --git a/Changelog.md b/Changelog.md index af09f804..d35a2b45 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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. diff --git a/docs/grammar.txt b/docs/grammar.txt index a5c2acf3..b4ca5ca9 100644 --- a/docs/grammar.txt +++ b/docs/grammar.txt @@ -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? ')' )? diff --git a/libsolidity/analysis/SyntaxChecker.cpp b/libsolidity/analysis/SyntaxChecker.cpp index ddac194b..3a32810b 100644 --- a/libsolidity/analysis/SyntaxChecker.cpp +++ b/libsolidity/analysis/SyntaxChecker.cpp @@ -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; +} diff --git a/libsolidity/analysis/SyntaxChecker.h b/libsolidity/analysis/SyntaxChecker.h index 871bf0a9..1579df57 100644 --- a/libsolidity/analysis/SyntaxChecker.h +++ b/libsolidity/analysis/SyntaxChecker.h @@ -71,6 +71,8 @@ private: 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 '_'. diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index e5269bc5..dd29bf48 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -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; _; diff --git a/test/libsolidity/syntaxTests/empty_struct.sol b/test/libsolidity/syntaxTests/empty_struct.sol new file mode 100644 index 00000000..dcced618 --- /dev/null +++ b/test/libsolidity/syntaxTests/empty_struct.sol @@ -0,0 +1,5 @@ +contract test { + struct A {} +} +// ---- +// Warning: Defining empty structs is deprecated. diff --git a/test/libsolidity/syntaxTests/empty_struct_050.sol b/test/libsolidity/syntaxTests/empty_struct_050.sol new file mode 100644 index 00000000..dbec93c4 --- /dev/null +++ b/test/libsolidity/syntaxTests/empty_struct_050.sol @@ -0,0 +1,6 @@ +pragma experimental "v0.5.0"; +contract test { + struct A {} +} +// ---- +// SyntaxError: Defining empty structs is disallowed. |