Skip to content

Commit b2ac0da

Browse files
authored
Merge pull request #13210 from timweri/display_human_readable_type_name_in_conversion
Display human readable type name in error messages
2 parents b02f648 + e0ba7ef commit b2ac0da

File tree

72 files changed

+654
-137
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+654
-137
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Compiler Features:
99
* Yul Optimizer: Add rule to convert `mod(mul(X, Y), A)` into `mulmod(X, Y, A)`, if `A` is a power of two.
1010
* Yul Optimizer: Add rule to convert `mod(add(X, Y), A)` into `addmod(X, Y, A)`, if `A` is a power of two.
1111
* Code Generator: More efficient code for checked addition and subtraction.
12+
* Error Reporter: More readable and informative error/warning messages.
1213

1314
Bugfixes:
1415
* Commandline Interface: Disallow the following options outside of the compiler mode: ``--via-ir``,``--metadata-literal``, ``--metadata-hash``, ``--model-checker-show-unproved``, ``--model-checker-div-mod-no-slacks``, ``--model-checker-engine``, ``--model-checker-invariants``, ``--model-checker-solvers``, ``--model-checker-timeout``, ``--model-checker-contracts``, ``--model-checker-targets``.

libsolidity/analysis/TypeChecker.cpp

Lines changed: 51 additions & 51 deletions
Large diffs are not rendered by default.

libsolidity/ast/ASTJsonExporter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ string ASTJsonExporter::namePathToString(std::vector<ASTString> const& _namePath
143143
return boost::algorithm::join(_namePath, ".");
144144
}
145145

146-
Json::Value ASTJsonExporter::typePointerToJson(Type const* _tp, bool _short)
146+
Json::Value ASTJsonExporter::typePointerToJson(Type const* _tp, bool _withoutDataLocation)
147147
{
148148
Json::Value typeDescriptions(Json::objectValue);
149-
typeDescriptions["typeString"] = _tp ? Json::Value(_tp->toString(_short)) : Json::nullValue;
149+
typeDescriptions["typeString"] = _tp ? Json::Value(_tp->toString(_withoutDataLocation)) : Json::nullValue;
150150
typeDescriptions["typeIdentifier"] = _tp ? Json::Value(_tp->identifier()) : Json::nullValue;
151151
return typeDescriptions;
152152

libsolidity/ast/ASTJsonExporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class ASTJsonExporter: public ASTConstVisitor
184184

185185
return json;
186186
}
187-
static Json::Value typePointerToJson(Type const* _tp, bool _short = false);
187+
static Json::Value typePointerToJson(Type const* _tp, bool _withoutDataLocation = false);
188188
static Json::Value typePointerToJson(std::optional<FuncCallArguments> const& _tps);
189189
void appendExpressionAttributes(
190190
std::vector<std::pair<std::string, Json::Value>> &_attributes,

libsolidity/ast/Types.cpp

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ util::Result<TypePointers> transformParametersToExternal(TypePointers const& _pa
110110
return transformed;
111111
}
112112

113-
string toStringInParentheses(TypePointers const& _types, bool _short)
113+
string toStringInParentheses(TypePointers const& _types, bool _withoutDataLocation)
114114
{
115115
return '(' + util::joinHumanReadable(
116-
_types | ranges::views::transform([&](auto const* _type) { return _type->toString(_short); }),
116+
_types | ranges::views::transform([&](auto const* _type) { return _type->toString(_withoutDataLocation); }),
117117
","
118118
) + ')';
119119
}
@@ -1789,7 +1789,7 @@ vector<tuple<string, Type const*>> ArrayType::makeStackItems() const
17891789
solAssert(false, "");
17901790
}
17911791

1792-
string ArrayType::toString(bool _short) const
1792+
string ArrayType::toString(bool _withoutDataLocation) const
17931793
{
17941794
string ret;
17951795
if (isString())
@@ -1798,16 +1798,34 @@ string ArrayType::toString(bool _short) const
17981798
ret = "bytes";
17991799
else
18001800
{
1801-
ret = baseType()->toString(_short) + "[";
1801+
ret = baseType()->toString(_withoutDataLocation) + "[";
18021802
if (!isDynamicallySized())
18031803
ret += length().str();
18041804
ret += "]";
18051805
}
1806-
if (!_short)
1806+
if (!_withoutDataLocation)
18071807
ret += " " + stringForReferencePart();
18081808
return ret;
18091809
}
18101810

1811+
string ArrayType::humanReadableName() const
1812+
{
1813+
string ret;
1814+
if (isString())
1815+
ret = "string";
1816+
else if (isByteArrayOrString())
1817+
ret = "bytes";
1818+
else
1819+
{
1820+
ret = baseType()->toString(true) + "[";
1821+
if (!isDynamicallySized())
1822+
ret += length().str();
1823+
ret += "]";
1824+
}
1825+
ret += " " + stringForReferencePart();
1826+
return ret;
1827+
}
1828+
18111829
string ArrayType::canonicalName() const
18121830
{
18131831
string ret;
@@ -1990,9 +2008,14 @@ bool ArraySliceType::operator==(Type const& _other) const
19902008
return false;
19912009
}
19922010

1993-
string ArraySliceType::toString(bool _short) const
2011+
string ArraySliceType::toString(bool _withoutDataLocation) const
2012+
{
2013+
return m_arrayType.toString(_withoutDataLocation) + " slice";
2014+
}
2015+
2016+
string ArraySliceType::humanReadableName() const
19942017
{
1995-
return m_arrayType.toString(_short) + " slice";
2018+
return m_arrayType.humanReadableName() + " slice";
19962019
}
19972020

19982021
Type const* ArraySliceType::mobileType() const
@@ -2256,10 +2279,10 @@ bool StructType::containsNestedMapping() const
22562279
return m_struct.annotation().containsNestedMapping.value();
22572280
}
22582281

2259-
string StructType::toString(bool _short) const
2282+
string StructType::toString(bool _withoutDataLocation) const
22602283
{
22612284
string ret = "struct " + *m_struct.annotation().canonicalName;
2262-
if (!_short)
2285+
if (!_withoutDataLocation)
22632286
ret += " " + stringForReferencePart();
22642287
return ret;
22652288
}
@@ -2611,7 +2634,7 @@ bool UserDefinedValueType::operator==(Type const& _other) const
26112634
return other.definition() == definition();
26122635
}
26132636

2614-
string UserDefinedValueType::toString(bool /* _short */) const
2637+
string UserDefinedValueType::toString(bool /* _withoutDataLocation */) const
26152638
{
26162639
return *definition().annotation().canonicalName;
26172640
}
@@ -2659,13 +2682,24 @@ bool TupleType::operator==(Type const& _other) const
26592682
return false;
26602683
}
26612684

2662-
string TupleType::toString(bool _short) const
2685+
string TupleType::toString(bool _withoutDataLocation) const
2686+
{
2687+
if (components().empty())
2688+
return "tuple()";
2689+
string str = "tuple(";
2690+
for (auto const& t: components())
2691+
str += (t ? t->toString(_withoutDataLocation) : "") + ",";
2692+
str.pop_back();
2693+
return str + ")";
2694+
}
2695+
2696+
string TupleType::humanReadableName() const
26632697
{
26642698
if (components().empty())
26652699
return "tuple()";
26662700
string str = "tuple(";
26672701
for (auto const& t: components())
2668-
str += (t ? t->toString(_short) : "") + ",";
2702+
str += (t ? t->humanReadableName() : "") + ",";
26692703
str.pop_back();
26702704
return str + ")";
26712705
}
@@ -3103,15 +3137,15 @@ string FunctionType::humanReadableName() const
31033137
switch (m_kind)
31043138
{
31053139
case Kind::Error:
3106-
return "error " + m_declaration->name() + toStringInParentheses(m_parameterTypes, /* _short */ true);
3140+
return "error " + m_declaration->name() + toStringInParentheses(m_parameterTypes, /* _withoutDataLocation */ true);
31073141
case Kind::Event:
3108-
return "event " + m_declaration->name() + toStringInParentheses(m_parameterTypes, /* _short */ true);
3142+
return "event " + m_declaration->name() + toStringInParentheses(m_parameterTypes, /* _withoutDataLocation */ true);
31093143
default:
3110-
return toString(/* _short */ false);
3144+
return toString(/* _withoutDataLocation */ false);
31113145
}
31123146
}
31133147

3114-
string FunctionType::toString(bool _short) const
3148+
string FunctionType::toString(bool _withoutDataLocation) const
31153149
{
31163150
string name = "function ";
31173151
if (m_kind == Kind::Declaration)
@@ -3122,15 +3156,15 @@ string FunctionType::toString(bool _short) const
31223156
name += *contract->annotation().canonicalName + ".";
31233157
name += functionDefinition->name();
31243158
}
3125-
name += toStringInParentheses(m_parameterTypes, _short);
3159+
name += toStringInParentheses(m_parameterTypes, _withoutDataLocation);
31263160
if (m_stateMutability != StateMutability::NonPayable)
31273161
name += " " + stateMutabilityToString(m_stateMutability);
31283162
if (m_kind == Kind::External)
31293163
name += " external";
31303164
if (!m_returnParameterTypes.empty())
31313165
{
31323166
name += " returns ";
3133-
name += toStringInParentheses(m_returnParameterTypes, _short);
3167+
name += toStringInParentheses(m_returnParameterTypes, _withoutDataLocation);
31343168
}
31353169
return name;
31363170
}
@@ -3722,9 +3756,9 @@ bool MappingType::operator==(Type const& _other) const
37223756
return *other.m_keyType == *m_keyType && *other.m_valueType == *m_valueType;
37233757
}
37243758

3725-
string MappingType::toString(bool _short) const
3759+
string MappingType::toString(bool _withoutDataLocation) const
37263760
{
3727-
return "mapping(" + keyType()->toString(_short) + " => " + valueType()->toString(_short) + ")";
3761+
return "mapping(" + keyType()->toString(_withoutDataLocation) + " => " + valueType()->toString(_withoutDataLocation) + ")";
37283762
}
37293763

37303764
string MappingType::canonicalName() const
@@ -3949,11 +3983,11 @@ bool ModifierType::operator==(Type const& _other) const
39493983
return true;
39503984
}
39513985

3952-
string ModifierType::toString(bool _short) const
3986+
string ModifierType::toString(bool _withoutDataLocation) const
39533987
{
39543988
string name = "modifier (";
39553989
for (auto it = m_parameterTypes.begin(); it != m_parameterTypes.end(); ++it)
3956-
name += (*it)->toString(_short) + (it + 1 == m_parameterTypes.end() ? "" : ",");
3990+
name += (*it)->toString(_withoutDataLocation) + (it + 1 == m_parameterTypes.end() ? "" : ",");
39573991
return name + ")";
39583992
}
39593993

@@ -4149,7 +4183,7 @@ MemberList::MemberMap MagicType::nativeMembers(ASTNode const*) const
41494183
return {};
41504184
}
41514185

4152-
string MagicType::toString(bool _short) const
4186+
string MagicType::toString(bool _withoutDataLocation) const
41534187
{
41544188
switch (m_kind)
41554189
{
@@ -4163,7 +4197,7 @@ string MagicType::toString(bool _short) const
41634197
return "abi";
41644198
case Kind::MetaType:
41654199
solAssert(m_typeArgument, "");
4166-
return "type(" + m_typeArgument->toString(_short) + ")";
4200+
return "type(" + m_typeArgument->toString(_withoutDataLocation) + ")";
41674201
}
41684202
solAssert(false, "Unknown kind of magic.");
41694203
return {};

0 commit comments

Comments
 (0)