summaryrefslogtreecommitdiff
path: root/src/mir/from_hir.cpp
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-08-13 17:13:15 +0800
committerJohn Hodge <tpg@mutabah.net>2016-08-13 17:13:15 +0800
commit49e9d496b700937542ec405a0c47cbdec06826d0 (patch)
treeb490734a08695342f01bac45ba632dcb12a15489 /src/mir/from_hir.cpp
parentca38d96c7979342bdf8b033cbef62d4664b13511 (diff)
downloadmrust-49e9d496b700937542ec405a0c47cbdec06826d0.tar.gz
MIR Gen - Handle unsizing to slices (with hack for libcore's FixedSizeArray impl)
Diffstat (limited to 'src/mir/from_hir.cpp')
-rw-r--r--src/mir/from_hir.cpp26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/mir/from_hir.cpp b/src/mir/from_hir.cpp
index 8cbb55d2..6727ab4b 100644
--- a/src/mir/from_hir.cpp
+++ b/src/mir/from_hir.cpp
@@ -621,7 +621,31 @@ namespace {
TU_MATCH_DEF( ::HIR::TypeRef::Data, (ty_out.m_data), (e),
(
- TODO(node.span(), "MIR _Unsize to " << ty_out);
+ TODO(node.span(), "MIR _Unsize to &[mut] " << ty_out);
+ ),
+ // TODO: Unsize custom types containing a ?Size generic - See the Unsize trait
+ //(Path,
+ // ),
+ //(Generic,
+ // ),
+ (Slice,
+ if( ty_in.m_data.is_Array() )
+ {
+ const auto& in_array = ty_in.m_data.as_Array();
+ auto size_lval = m_builder.lvalue_or_temp( ::HIR::TypeRef(::HIR::CoreType::Usize), ::MIR::Constant( static_cast<uint64_t>(in_array.size_val) ) );
+ m_builder.set_result( node.span(), ::MIR::RValue::make_MakeDst({ mv$(ptr_lval), mv$(size_lval) }) );
+ }
+ else if( ty_in.m_data.is_Generic() )
+ {
+ // HACK: FixedSizeArray uses `A: Unsize<[T]>` which will lead to the above code not working (as the size isn't known).
+ // - Maybe _Meta on the `&A` would work as a stopgap (since A: Sized, it won't collide with &[T] or similar)
+ auto size_lval = m_builder.lvalue_or_temp( ::HIR::TypeRef(::HIR::CoreType::Usize), ::MIR::RValue::make_DstMeta({ ptr_lval.clone() }) );
+ m_builder.set_result( node.span(), ::MIR::RValue::make_MakeDst({ mv$(ptr_lval), mv$(size_lval) }) );
+ }
+ else
+ {
+ ASSERT_BUG(node.span(), ty_in.m_data.is_Array(), "Unsize to slice from non-array - " << ty_in);
+ }
),
(TraitObject,
// TODO: Obtain the vtable if the destination is a trait object