mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
If a declaration shadows several others, group them together
This commit is contained in:
parent
f29ebc0847
commit
90c8c32d86
@ -191,6 +191,9 @@ void DeclarationContainer::populateHomonyms(back_insert_iterator<Homonyms> _it)
|
|||||||
innerContainer->populateHomonyms(_it);
|
innerContainer->populateHomonyms(_it);
|
||||||
|
|
||||||
for (auto [name, location]: m_homonymCandidates)
|
for (auto [name, location]: m_homonymCandidates)
|
||||||
for (auto const* declaration: m_enclosingContainer->resolveName(name, true, true))
|
{
|
||||||
_it = make_pair(location, declaration);
|
vector<Declaration const*> const& declarations = m_enclosingContainer->resolveName(name, true, true);
|
||||||
|
if (!declarations.empty())
|
||||||
|
_it = make_pair(location, declarations);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,8 +27,6 @@
|
|||||||
#include <liblangutil/Exceptions.h>
|
#include <liblangutil/Exceptions.h>
|
||||||
#include <liblangutil/SourceLocation.h>
|
#include <liblangutil/SourceLocation.h>
|
||||||
#include <boost/noncopyable.hpp>
|
#include <boost/noncopyable.hpp>
|
||||||
#include <map>
|
|
||||||
#include <set>
|
|
||||||
|
|
||||||
namespace solidity::frontend
|
namespace solidity::frontend
|
||||||
{
|
{
|
||||||
@ -40,7 +38,7 @@ namespace solidity::frontend
|
|||||||
class DeclarationContainer
|
class DeclarationContainer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using Homonyms = std::vector<std::pair<langutil::SourceLocation const*, Declaration const*>>;
|
using Homonyms = std::vector<std::pair<langutil::SourceLocation const*, std::vector<Declaration const*>>>;
|
||||||
|
|
||||||
explicit DeclarationContainer(
|
explicit DeclarationContainer(
|
||||||
ASTNode const* _enclosingNode = nullptr,
|
ASTNode const* _enclosingNode = nullptr,
|
||||||
|
@ -229,41 +229,44 @@ void NameAndTypeResolver::warnHomonymDeclarations() const
|
|||||||
DeclarationContainer::Homonyms homonyms;
|
DeclarationContainer::Homonyms homonyms;
|
||||||
m_scopes.at(nullptr)->populateHomonyms(back_inserter(homonyms));
|
m_scopes.at(nullptr)->populateHomonyms(back_inserter(homonyms));
|
||||||
|
|
||||||
unordered_set<SourceLocation const*> noMoreMagic;
|
for (auto [innerLocation, outerDeclarations]: homonyms)
|
||||||
for (auto [innerLocation, outerDeclaration]: homonyms)
|
|
||||||
{
|
{
|
||||||
solAssert(innerLocation && outerDeclaration, "");
|
solAssert(innerLocation && !outerDeclarations.empty(), "");
|
||||||
|
|
||||||
if (dynamic_cast<MagicVariableDeclaration const*>(outerDeclaration))
|
bool magicShadowed = false;
|
||||||
|
SecondarySourceLocation homonymousLocations;
|
||||||
|
SecondarySourceLocation shadowedLocations;
|
||||||
|
for (Declaration const* outerDeclaration: outerDeclarations)
|
||||||
{
|
{
|
||||||
// avoids duplecated warnings
|
solAssert(outerDeclaration, "");
|
||||||
// (some magic variables ("revert", "require") appear in different flavors under the same name)
|
if (dynamic_cast<MagicVariableDeclaration const*>(outerDeclaration))
|
||||||
if (noMoreMagic.insert(innerLocation).second)
|
magicShadowed = true;
|
||||||
m_errorReporter.warning(
|
else if (!outerDeclaration->isVisibleInContract())
|
||||||
2319_error,
|
homonymousLocations.append("The other declaration is here:", outerDeclaration->location());
|
||||||
*innerLocation,
|
|
||||||
"This declaration shadows a builtin symbol."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SourceLocation const& outerLocation = outerDeclaration->location();
|
|
||||||
|
|
||||||
if (!outerDeclaration->isVisibleInContract())
|
|
||||||
m_errorReporter.warning(
|
|
||||||
8760_error,
|
|
||||||
*innerLocation,
|
|
||||||
"This declaration has the same name as another declaration.",
|
|
||||||
SecondarySourceLocation().append("The other declaration is here:", outerLocation)
|
|
||||||
);
|
|
||||||
else
|
else
|
||||||
m_errorReporter.warning(
|
shadowedLocations.append("The shadowed declaration is here:", outerDeclaration->location());
|
||||||
2519_error,
|
|
||||||
*innerLocation,
|
|
||||||
"This declaration shadows an existing declaration.",
|
|
||||||
SecondarySourceLocation().append("The shadowed declaration is here:", outerLocation)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (magicShadowed)
|
||||||
|
m_errorReporter.warning(
|
||||||
|
2319_error,
|
||||||
|
*innerLocation,
|
||||||
|
"This declaration shadows a builtin symbol."
|
||||||
|
);
|
||||||
|
if (!homonymousLocations.infos.empty())
|
||||||
|
m_errorReporter.warning(
|
||||||
|
8760_error,
|
||||||
|
*innerLocation,
|
||||||
|
"This declaration has the same name as another declaration.",
|
||||||
|
homonymousLocations
|
||||||
|
);
|
||||||
|
if (!shadowedLocations.infos.empty())
|
||||||
|
m_errorReporter.warning(
|
||||||
|
2519_error,
|
||||||
|
*innerLocation,
|
||||||
|
"This declaration shadows an existing declaration.",
|
||||||
|
shadowedLocations
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,5 +16,4 @@ contract D is C {
|
|||||||
// ----
|
// ----
|
||||||
// Warning 2519: (s1.sol:65-134): This declaration shadows an existing declaration.
|
// Warning 2519: (s1.sol:65-134): This declaration shadows an existing declaration.
|
||||||
// Warning 2519: (s2.sol:85-155): This declaration shadows an existing declaration.
|
// Warning 2519: (s2.sol:85-155): This declaration shadows an existing declaration.
|
||||||
// Warning 2519: (s2.sol:85-155): This declaration shadows an existing declaration.
|
|
||||||
// DeclarationError 1686: (s2.sol:17-64): Function with same name and parameter types defined twice.
|
// DeclarationError 1686: (s2.sol:17-64): Function with same name and parameter types defined twice.
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
contract test {
|
||||||
|
function e() external { }
|
||||||
|
function f() public pure { uint e; e = 0; }
|
||||||
|
function e(int) external { }
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// Warning 8760: (77-83): This declaration has the same name as another declaration.
|
10
test/libsolidity/syntaxTests/scoping/name_shadowing3.sol
Normal file
10
test/libsolidity/syntaxTests/scoping/name_shadowing3.sol
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
function e() {}
|
||||||
|
contract test {
|
||||||
|
function f() pure public { uint e; uint g; uint h; e = g = h = 0; }
|
||||||
|
function g() pure public {}
|
||||||
|
}
|
||||||
|
function h() {}
|
||||||
|
// ----
|
||||||
|
// Warning 2519: (63-69): This declaration shadows an existing declaration.
|
||||||
|
// Warning 2519: (71-77): This declaration shadows an existing declaration.
|
||||||
|
// Warning 2519: (79-85): This declaration shadows an existing declaration.
|
Loading…
Reference in New Issue
Block a user