diff options
author | John Hodge <tpg@mutabah.net> | 2016-08-11 17:30:23 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-08-11 17:30:23 +0800 |
commit | 9936ca6f41867e0e5378db770a1787ccf1c0a9bd (patch) | |
tree | 303c189f8fb4c0a3462e7660205ff6a97f204557 /src | |
parent | 4a588dfb1a5dd5e4b7ec32171583d01858c51f20 (diff) | |
download | mrust-9936ca6f41867e0e5378db770a1787ccf1c0a9bd.tar.gz |
HIR Typecheck - Unsize operates on &-ptrs
Diffstat (limited to 'src')
-rw-r--r-- | src/hir_typeck/expr_check.cpp | 10 | ||||
-rw-r--r-- | src/hir_typeck/expr_cs.cpp | 25 |
2 files changed, 19 insertions, 16 deletions
diff --git a/src/hir_typeck/expr_check.cpp b/src/hir_typeck/expr_check.cpp index e9ce915e..e0476f3e 100644 --- a/src/hir_typeck/expr_check.cpp +++ b/src/hir_typeck/expr_check.cpp @@ -283,8 +283,14 @@ namespace { TRACE_FUNCTION_F(&node << " ... : " << node.m_res_type); const Span& sp = node.span(); - const auto& src_ty = node.m_value->m_res_type; - const auto& dst_ty = node.m_res_type; + if( !node.m_value->m_res_type.m_data.is_Borrow() ) { + ERROR(sp, E0000, "Invalid unsizing operation from non-&-ptr"); + } + const auto& src_ty = *node.m_value->m_res_type.m_data.as_Borrow().inner; + if( !node.m_res_type.m_data.is_Borrow() ) { + ERROR(sp, E0000, "Invalid unsizing operation to non-&-ptr"); + } + const auto& dst_ty = *node.m_res_type.m_data.as_Borrow().inner; // Check unsizability (including trait impls) // NOTE: Unsize applies inside borrows TU_MATCH_DEF(::HIR::TypeRef::Data, (dst_ty.m_data), (e), diff --git a/src/hir_typeck/expr_cs.cpp b/src/hir_typeck/expr_cs.cpp index 7adf8be8..3687a05f 100644 --- a/src/hir_typeck/expr_cs.cpp +++ b/src/hir_typeck/expr_cs.cpp @@ -2987,7 +2987,7 @@ namespace { context.m_ivars.mark_change(); } - bool check_coerce_borrow(Context& context, const ::HIR::TypeRef& inner_l, const ::HIR::TypeRef& inner_r, ::HIR::ExprNodeP& node_ptr) + bool check_coerce_borrow(Context& context, ::HIR::BorrowType bt, const ::HIR::TypeRef& inner_l, const ::HIR::TypeRef& inner_r, ::HIR::ExprNodeP& node_ptr) { const auto& sp = node_ptr->span(); @@ -3157,13 +3157,13 @@ namespace { } } - // Add CoerceUnsized - add_coerce_borrow(context, node_ptr, ty_dst, [&](auto& node_ptr) { + // Add _Unsize operator to the &-ptr + { auto span = node_ptr->span(); - node_ptr = ::HIR::ExprNodeP(new ::HIR::ExprNode_Unsize( mv$(span), mv$(node_ptr), ty_dst.clone() )); + auto ty_dst_ref = ::HIR::TypeRef::new_borrow(bt, ty_dst.clone() ); + node_ptr = ::HIR::ExprNodeP(new ::HIR::ExprNode_Unsize( mv$(span), mv$(node_ptr), mv$(ty_dst_ref) )); DEBUG("- Unsize " << &*node_ptr << " -> " << ty_dst); - node_ptr->m_res_type = ty_dst.clone(); - }); + } return true; ) ) @@ -3194,13 +3194,10 @@ namespace { return cmp == ::HIR::Compare::Equal; }); if( found ) { - add_coerce_borrow(context, node_ptr, ty_dst, [&](auto& node_ptr) { - auto span = node_ptr->span(); - auto ty = mv$(ty_dst.clone()); - node_ptr = ::HIR::ExprNodeP(new ::HIR::ExprNode_Unsize( mv$(span), mv$(node_ptr), ty.clone() )); - DEBUG("- Unsize " << &*node_ptr << " -> " << ty); - node_ptr->m_res_type = mv$(ty); - }); + auto span = node_ptr->span(); + auto ty_dst_ref = ::HIR::TypeRef::new_borrow(bt, ty_dst.clone() ); + node_ptr = ::HIR::ExprNodeP(new ::HIR::ExprNode_Unsize( mv$(span), mv$(node_ptr), mv$(ty_dst_ref) )); + DEBUG("- Unsize " << &*node_ptr << " -> " << ty_dst); return true; } } @@ -3383,7 +3380,7 @@ namespace { } // - Check for coercions - return check_coerce_borrow(context, *l_e.inner, *r_e.inner, node_ptr); + return check_coerce_borrow(context, l_e.type, *l_e.inner, *r_e.inner, node_ptr); ) else TU_IFLET(::HIR::TypeRef::Data, ty_src.m_data, Infer, r_e, // Leave for now |