From 8a2a5689c5de1dc995232e81e560a0610864ee02 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 30 Oct 2016 22:49:17 +0800 Subject: MIR Gen - Handle single-variant enums --- src/mir/from_hir.cpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/mir/from_hir.cpp b/src/mir/from_hir.cpp index a2faeeb5..9b9ddf5c 100644 --- a/src/mir/from_hir.cpp +++ b/src/mir/from_hir.cpp @@ -208,25 +208,36 @@ namespace { ASSERT_BUG(sp, allow_refutable, "Refutable pattern not expected - " << pat); ), (EnumValue, - ASSERT_BUG(sp, allow_refutable, "Refutable pattern not expected - " << pat); + const auto& enm = *e.binding_ptr; + if( enm.m_variants.size() > 1 ) + { + ASSERT_BUG(sp, allow_refutable, "Refutable pattern not expected - " << pat); + } ), (EnumTuple, - ASSERT_BUG(sp, allow_refutable, "Refutable pattern not expected - " << pat); - auto lval_var = ::MIR::LValue::make_Downcast({ box$(mv$(lval)), e.binding_idx }); - for(unsigned int i = 0; i < e.sub_patterns.size(); i ++ ) + const auto& enm = *e.binding_ptr; + if( enm.m_variants.size() > 1 ) { - destructure_from_ex(sp, e.sub_patterns[i], ::MIR::LValue::make_Field({ box$( lval_var.clone() ), i}), allow_refutable); + ASSERT_BUG(sp, allow_refutable, "Refutable pattern not expected - " << pat); + auto lval_var = ::MIR::LValue::make_Downcast({ box$(mv$(lval)), e.binding_idx }); + for(unsigned int i = 0; i < e.sub_patterns.size(); i ++ ) + { + destructure_from_ex(sp, e.sub_patterns[i], ::MIR::LValue::make_Field({ box$( lval_var.clone() ), i}), allow_refutable); + } } ), (EnumStruct, ASSERT_BUG(sp, allow_refutable, "Refutable pattern not expected - " << pat); const auto& enm = *e.binding_ptr; - const auto& fields = enm.m_variants[e.binding_idx].second.as_Struct(); - auto lval_var = ::MIR::LValue::make_Downcast({ box$(mv$(lval)), e.binding_idx }); - for(const auto& fld_pat : e.sub_patterns) + if( enm.m_variants.size() > 1 ) { - unsigned idx = ::std::find_if( fields.begin(), fields.end(), [&](const auto&x){ return x.first == fld_pat.first; } ) - fields.begin(); - destructure_from_ex(sp, fld_pat.second, ::MIR::LValue::make_Field({ box$( lval_var.clone() ), idx}), allow_refutable); + const auto& fields = enm.m_variants[e.binding_idx].second.as_Struct(); + auto lval_var = ::MIR::LValue::make_Downcast({ box$(mv$(lval)), e.binding_idx }); + for(const auto& fld_pat : e.sub_patterns) + { + unsigned idx = ::std::find_if( fields.begin(), fields.end(), [&](const auto&x){ return x.first == fld_pat.first; } ) - fields.begin(); + destructure_from_ex(sp, fld_pat.second, ::MIR::LValue::make_Field({ box$( lval_var.clone() ), idx}), allow_refutable); + } } ), (Slice, -- cgit v1.2.3