diff options
| author | John Hodge <tpg@mutabah.net> | 2016-10-30 22:49:17 +0800 | 
|---|---|---|
| committer | John Hodge <tpg@mutabah.net> | 2016-10-30 22:49:17 +0800 | 
| commit | 8a2a5689c5de1dc995232e81e560a0610864ee02 (patch) | |
| tree | 7e9de1d46c6f518fa9250579004515adce391345 | |
| parent | 86d573e56120c275e0e0a71a39d5c0dc80cbfeac (diff) | |
| download | mrust-8a2a5689c5de1dc995232e81e560a0610864ee02.tar.gz | |
MIR Gen - Handle single-variant enums
| -rw-r--r-- | src/mir/from_hir.cpp | 31 | 
1 files changed, 21 insertions, 10 deletions
| 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, | 
