summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-06-12 22:19:18 +0800
committerJohn Hodge <tpg@mutabah.net>2016-06-12 22:19:18 +0800
commit7478d2e92eb3fb13cf36033a2a84a9bec6f20de4 (patch)
tree5fd9d1b0f29ad2a46943e134743f324fdc2aeaaa /src
parent4bfcf255fa80cd0635bf6457aa9b5230fa0b3509 (diff)
downloadmrust-7478d2e92eb3fb13cf36033a2a84a9bec6f20de4.tar.gz
HIR Typecheck - Defer `if` inferrence until iteration, fixes mismatched branches
Diffstat (limited to 'src')
-rw-r--r--src/hir_typeck/expr.cpp19
-rw-r--r--src/hir_typeck/expr_context.cpp1
2 files changed, 10 insertions, 10 deletions
diff --git a/src/hir_typeck/expr.cpp b/src/hir_typeck/expr.cpp
index d14424b2..b621b92f 100644
--- a/src/hir_typeck/expr.cpp
+++ b/src/hir_typeck/expr.cpp
@@ -387,8 +387,8 @@ namespace typeck {
{
node.m_cond->m_res_type = ::HIR::TypeRef( ::HIR::CoreType::Bool );
if( node.m_false ) {
- node.m_true->m_res_type = node.m_res_type.clone();
- node.m_false->m_res_type = node.m_res_type.clone();
+ // node.m_true->m_res_type = node.m_res_type.clone();
+ // node.m_false->m_res_type = node.m_res_type.clone();
}
else {
this->context.apply_equality(node.span(), node.m_res_type, ::HIR::TypeRef::new_unit());
@@ -424,7 +424,6 @@ namespace typeck {
::HIR::ExprVisitorDef::visit(node);
- this->context.apply_equality( node.span(), node.m_res_type, ::HIR::TypeRef::new_array(node.m_val->m_res_type.clone(), node.m_size_val) );
this->context.apply_equality( node.span(), node.m_size->m_res_type, ::HIR::TypeRef(::HIR::CoreType::Usize) );
}
void visit(::HIR::ExprNode_Tuple& node) override
@@ -583,10 +582,12 @@ namespace typeck {
void visit(::HIR::ExprNode_If& node) override
{
TRACE_FUNCTION_F("if ...");
+
this->context.apply_equality(node.span(), node.m_res_type, node.m_true->m_res_type, &node.m_true);
if( node.m_false ) {
this->context.apply_equality(node.span(), node.m_res_type, node.m_false->m_res_type, &node.m_false);
}
+
::HIR::ExprVisitorDef::visit(node);
}
// - Match: all branches match
@@ -1886,11 +1887,10 @@ namespace typeck {
// - Array (sized)
void visit(::HIR::ExprNode_ArraySized& node) override
{
- auto& ty = this->context.get_type(node.m_res_type);
- if( !ty.m_data.is_Array() ) {
- this->context.dump();
- BUG(node.span(), "Return type of array literal wasn't an array - " << node.m_res_type << " = " << ty);
+ if( !this->context.get_type(node.m_res_type).m_data.is_Array() ) {
+ this->context.apply_equality(node.span(), node.m_res_type, ::HIR::TypeRef::new_array(node.m_val->m_res_type.clone(), node.m_size_val) );
}
+ auto& ty = this->context.get_type(node.m_res_type);
const auto& val_type = *ty.m_data.as_Array().inner;
::HIR::ExprVisitorDef::visit(node);
this->context.apply_equality(node.span(), val_type, node.m_val->m_res_type, &node.m_val);
@@ -1993,11 +1993,10 @@ void Typecheck_Code(typeck::TypecheckContext context, const ::HIR::TypeRef& resu
// 1. Enumerate inferrence variables and assign indexes to them
{
typeck::ExprVisitor_Enum visitor { context, result_type };
+ context.add_ivars( root_ptr->m_res_type );
+ context.apply_equality(root_ptr->span(), result_type, root_ptr->m_res_type, &root_ptr);
visitor.visit_node_ptr(root_ptr);
}
- // - Apply equality between the node result and the expected type
- DEBUG("- Apply RV");
- context.apply_equality(root_ptr->span(), result_type, root_ptr->m_res_type, &root_ptr);
context.dump();
// 2. Iterate through nodes applying rules until nothing changes
diff --git a/src/hir_typeck/expr_context.cpp b/src/hir_typeck/expr_context.cpp
index 3044379b..14eb869c 100644
--- a/src/hir_typeck/expr_context.cpp
+++ b/src/hir_typeck/expr_context.cpp
@@ -1850,6 +1850,7 @@ unsigned int typeck::TypecheckContext::autoderef_find_method(const Span& sp, con
bool typeck::TypecheckContext::find_method(const Span& sp, const ::HIR::TypeRef& ty, const ::std::string& method_name, /* Out -> */::HIR::Path& fcn_path) const
{
+ TRACE_FUNCTION_F("ty=" << ty << ", name=" << method_name);
// 1. Search generic bounds for a match
const ::HIR::GenericParams* v[2] = { m_item_params, m_impl_params };
for(auto p : v)