This commit is contained in:
Daniel Kirchner 2023-06-21 02:15:01 +02:00
parent 7a8c997438
commit 14a34ae088
3 changed files with 37 additions and 17 deletions

View File

@ -272,6 +272,12 @@ bool TypeInference::visit(Identifier const& _identifier)
auto const* referencedDeclaration = _identifier.annotation().referencedDeclaration; auto const* referencedDeclaration = _identifier.annotation().referencedDeclaration;
solAssert(referencedDeclaration); solAssert(referencedDeclaration);
if (
!dynamic_cast<FunctionDefinition const*>(referencedDeclaration) &&
!dynamic_cast<VariableDeclaration const*>(referencedDeclaration)
)
m_errorReporter.fatalTypeError(0000_error, referencedDeclaration->location(), "Attempt to type ídentifier referring to unexpected node.");
auto& declarationAnnotation = annotation(*referencedDeclaration); auto& declarationAnnotation = annotation(*referencedDeclaration);
if (!declarationAnnotation.type) if (!declarationAnnotation.type)
referencedDeclaration->accept(*this); referencedDeclaration->accept(*this);
@ -290,6 +296,11 @@ bool TypeInference::visit(IdentifierPath const& _identifier)
auto const* referencedDeclaration = _identifier.annotation().referencedDeclaration; auto const* referencedDeclaration = _identifier.annotation().referencedDeclaration;
solAssert(referencedDeclaration); solAssert(referencedDeclaration);
if (
!dynamic_cast<FunctionDefinition const*>(referencedDeclaration) &&
!dynamic_cast<VariableDeclaration const*>(referencedDeclaration)
)
m_errorReporter.fatalTypeError(0000_error, referencedDeclaration->location(), "Attempt to type ídentifier referring to unexpected node.");
auto& declarationAnnotation = annotation(*referencedDeclaration); auto& declarationAnnotation = annotation(*referencedDeclaration);
if (!declarationAnnotation.type) if (!declarationAnnotation.type)

View File

@ -176,22 +176,31 @@ void TypeSystem::validate(TypeVariable _variable) const
experimental::Type TypeSystem::fresh(Type _type, bool _generalize) experimental::Type TypeSystem::fresh(Type _type, bool _generalize)
{ {
std::unordered_map<uint64_t, Type> mapping;
auto freshImpl = [&](Type _type, bool _generalize, auto _recurse) -> Type {
return std::visit(util::GenericVisitor{ return std::visit(util::GenericVisitor{
[&](TypeExpression const& _type) -> Type { [&](TypeExpression const& _type) -> Type {
return TypeExpression{ return TypeExpression{
_type.constructor, _type.constructor,
_type.arguments | ranges::view::transform([&](Type _argType) { _type.arguments | ranges::view::transform([&](Type _argType) {
return fresh(_argType, _generalize); return _recurse(_argType, _generalize, _recurse);
}) | ranges::to<vector<Type>> }) | ranges::to<vector<Type>>
}; };
}, },
[&](TypeVariable const& _var) { [&](TypeVariable const& _var) -> Type {
validate(_var);
if (_generalize || _var.generic()) if (_generalize || _var.generic())
return freshTypeVariable(true); {
if (mapping.count(_var.index()))
return mapping[_var.index()];
return mapping[_var.index()] = freshTypeVariable(true);
}
else else
return _type; return _type;
}, },
}, resolve(_type)); }, resolve(_type));
};
return freshImpl(_type, _generalize, freshImpl);
} }
experimental::Type TypeSystemHelpers::tupleType(vector<Type> _elements) const experimental::Type TypeSystemHelpers::tupleType(vector<Type> _elements) const

View File

@ -622,7 +622,7 @@ Parser::FunctionHeaderParserResult Parser::parseFunctionHeader(bool _isStateVari
m_scanner->currentToken() == (m_experimentalSolidityEnabledInCurrentSourceUnit ? Token::RightArrow : Token::Returns) m_scanner->currentToken() == (m_experimentalSolidityEnabledInCurrentSourceUnit ? Token::RightArrow : Token::Returns)
) )
{ {
bool const permitEmptyParameterList = false; bool const permitEmptyParameterList = m_experimentalSolidityEnabledInCurrentSourceUnit;
advance(); advance();
result.returnParameters = parseParameterList(options, permitEmptyParameterList); result.returnParameters = parseParameterList(options, permitEmptyParameterList);
} }