From 7a51acc5fe3458d6c76c81ae409761dfd4be9be5 Mon Sep 17 00:00:00 2001 From: Marenz Date: Tue, 21 Sep 2021 18:02:20 +0200 Subject: [PATCH] Correct wrong error message referencing `.slot` and `.offset` when `.length` was used --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 4 ++-- .../inlineAssembly/invalid/constant_length_access.sol | 10 ++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 test/libsolidity/syntaxTests/inlineAssembly/invalid/constant_length_access.sol diff --git a/Changelog.md b/Changelog.md index 2d0a1e484..eacd9777b 100644 --- a/Changelog.md +++ b/Changelog.md @@ -29,6 +29,7 @@ Bugfixes: * SMTChecker: Fix false positive in external calls from constructors. * SMTChecker: Fix internal error on some multi-source uses of ``abi.*``, cryptographic functions and constants. * Standard JSON: Fix non-fatal errors in Yul mode being discarded if followed by a fatal error. + * Type Checker: Correct wrong error message in inline assembly complaining about ``.slot`` or ``.offset` not valid when actually ``.length`` was used. * Type Checker: Disallow modifier declarations and definitions in interfaces. * Yul Optimizer: Fix a crash in LoadResolver, when ``keccak256`` has particular non-identifier arguments. diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index afcf92525..c9772f1c8 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -799,7 +799,7 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly) m_errorReporter.typeError(6252_error, _identifier.debugData->location, "Constant variables cannot be assigned to."); return false; } - else if (!identifierInfo.suffix.empty()) + else if (identifierInfo.suffix == "slot" || identifierInfo.suffix == "offset") { m_errorReporter.typeError(6617_error, _identifier.debugData->location, "The suffixes .offset and .slot can only be used on non-constant storage variables."); return false; @@ -829,7 +829,7 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly) { string const& suffix = identifierInfo.suffix; solAssert((set{"offset", "slot", "length"}).count(suffix), ""); - if (var->isStateVariable() || var->type()->dataStoredIn(DataLocation::Storage)) + if (!var->isConstant() && (var->isStateVariable() || var->type()->dataStoredIn(DataLocation::Storage))) { if (suffix != "slot" && suffix != "offset") { diff --git a/test/libsolidity/syntaxTests/inlineAssembly/invalid/constant_length_access.sol b/test/libsolidity/syntaxTests/inlineAssembly/invalid/constant_length_access.sol new file mode 100644 index 000000000..eda71f955 --- /dev/null +++ b/test/libsolidity/syntaxTests/inlineAssembly/invalid/constant_length_access.sol @@ -0,0 +1,10 @@ +contract Test { + uint constant x = 2; + function f() public pure { + assembly { + let y := x.length + } + } +} +// ---- +// TypeError 3622: (91-99): The suffix ".length" is not supported by this variable or type.