Skip to content

Commit d30aa71

Browse files
committed
Fix shadowing struct types by struct member names
1 parent 2fb2788 commit d30aa71

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

libsolidity/analysis/NameAndTypeResolver.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ Declaration const* NameAndTypeResolver::pathFromCurrentScope(vector<ASTString> c
191191
{
192192
solAssert(!_path.empty(), "");
193193
vector<Declaration const*> candidates = m_currentScope->resolveName(_path.front(), true);
194+
195+
if (!candidates.empty() && candidates.front()->isStructMember())
196+
{
197+
// A special case like
198+
// enum E {a}; struct S {E E;}
199+
// We are within a struct scope and resolved name is a struct member name.
200+
// We resolve again, starting from the enclosing scope now.
201+
solAssert(_path.size() == 1, "");
202+
solAssert(candidates.size() == 1, "");
203+
DeclarationContainer const* enclosingContainer = m_currentScope->enclosingContainer();
204+
if (!enclosingContainer)
205+
return nullptr;
206+
candidates = enclosingContainer->resolveName(_path.front(), true);
207+
}
208+
194209
for (size_t i = 1; i < _path.size() && candidates.size() == 1; i++)
195210
{
196211
if (!m_scopes.count(candidates.front()))

test/libsolidity/syntaxTests/structs/member_type_eq_name.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ contract C {
33
function f(function(S memory) external) public {}
44
}
55
// ----
6-
// TypeError 5172: (25-26): Name has to refer to a struct, enum or contract.
6+
// DeclarationError 7920: (25-26): Identifier not found or not unique.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
contract C {
2+
enum E {a, b, c}
3+
struct S {E X; uint E;}
4+
struct T {E T; uint E;}
5+
struct U {E E;}
6+
}

0 commit comments

Comments
 (0)