Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error for failed lookup #3932

Merged
merged 2 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Features:
* Syntax Checker: Warn about functions named "constructor".

Bugfixes:
* Type Checker: Improve error message for failed function overload resolution.
* Type Checker: Do not complain about new-style constructor and fallback function to have the same name.
* Type Checker: Detect multiple constructor declarations in the new syntax and old syntax.

Expand Down
27 changes: 16 additions & 11 deletions libsolidity/analysis/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,8 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
// Retrieve the types of the arguments if this is used to call a function.
auto const& argumentTypes = _memberAccess.annotation().argumentTypes;
MemberList::MemberMap possibleMembers = exprType->members(m_scope).membersByName(memberName);
if (possibleMembers.size() > 1 && argumentTypes)
size_t const initialMemberCount = possibleMembers.size();
if (initialMemberCount > 1 && argumentTypes)
{
// do overload resolution
for (auto it = possibleMembers.begin(); it != possibleMembers.end();)
Expand All @@ -1918,17 +1919,21 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
}
if (possibleMembers.size() == 0)
{
auto storageType = ReferenceType::copyForLocationIfReference(
DataLocation::Storage,
exprType
);
if (!storageType->members(m_scope).membersByName(memberName).empty())
m_errorReporter.fatalTypeError(
_memberAccess.location(),
"Member \"" + memberName + "\" is not available in " +
exprType->toString() +
" outside of storage."
if (initialMemberCount == 0)
{
// Try to see if the member was removed because it is only available for storage types.
auto storageType = ReferenceType::copyForLocationIfReference(
DataLocation::Storage,
exprType
);
if (!storageType->members(m_scope).membersByName(memberName).empty())
m_errorReporter.fatalTypeError(
_memberAccess.location(),
"Member \"" + memberName + "\" is not available in " +
exprType->toString() +
" outside of storage."
);
}
m_errorReporter.fatalTypeError(
_memberAccess.location(),
"Member \"" + memberName + "\" not found or not visible "
Expand Down
15 changes: 0 additions & 15 deletions test/libsolidity/SolidityNameAndTypeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2993,21 +2993,6 @@ BOOST_AUTO_TEST_CASE(literal_strings)
CHECK_SUCCESS(text);
}

BOOST_AUTO_TEST_CASE(memory_structs_with_mappings)
{
char const* text = R"(
contract Test {
struct S { uint8 a; mapping(uint => uint) b; uint8 c; }
S s;
function f() public {
S memory x;
x.b[1];
}
}
)";
CHECK_ERROR(text, TypeError, "Member \"b\" is not available in struct Test.S memory outside of storage.");
}

BOOST_AUTO_TEST_CASE(string_bytes_conversion)
{
char const* text = R"(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract C {
function f(uint, uint) {}
function f(uint) {}
function g() { f(1, 2, 3); }
}
// ----
// TypeError: (80-81): No matching declaration found after argument-dependent lookup.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
library L {
function f(uint, uint) {}
function f(uint) {}
}
contract C {
function g() { L.f(1, 2, 3); }
}
// ----
// TypeError: (94-97): Member "f" not found or not visible after argument-dependent lookup in type(library L)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this test case from the original issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better question though: cannot we support overloads from libraries?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, it was too late.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
contract Test {
struct S { uint8 a; mapping(uint => uint) b; uint8 c; }
S s;
function f() public {
S memory x;
x.b[1];
}
}
// ----
// TypeError: (118-121): Member "b" is not available in struct Test.S memory outside of storage.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
contract Test {
function f() public pure {
uint[] memory x;
x.push(1);
}
}
// ----
// TypeError: (77-83): Member "push" is not available in uint256[] memory outside of storage.