diff options
author | John Hodge <tpg@mutabah.net> | 2016-10-07 15:36:42 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-10-07 15:36:42 +0800 |
commit | e35d4ba0abc93e3ea8e6f9c6f51d015bc10dac52 (patch) | |
tree | 481e2671f205b3502d19f87deb3db19b9634c190 /src/mir/from_hir_match.cpp | |
parent | 343d70590763fb415495c2973f3cfd272e60a96a (diff) | |
download | mrust-e35d4ba0abc93e3ea8e6f9c6f51d015bc10dac52.tar.gz |
MIR Gen Match - Struct patterns
Diffstat (limited to 'src/mir/from_hir_match.cpp')
-rw-r--r-- | src/mir/from_hir_match.cpp | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/mir/from_hir_match.cpp b/src/mir/from_hir_match.cpp index 1ad207a3..45980f80 100644 --- a/src/mir/from_hir_match.cpp +++ b/src/mir/from_hir_match.cpp @@ -1144,10 +1144,10 @@ void PatternRulesetBuilder::append_from(const Span& sp, const ::HIR::Pattern& pa ( BUG(sp, "Match not allowed, " << ty << " with " << pat); ), (Any, // Nothing. - this->push_rule( PatternRule::make_Any({}) ); + //this->push_rule( PatternRule::make_Any({}) ); ), (Value, - TODO(sp, "Match over struct - Unit + Value"); + //TODO(sp, "Match over struct - Unit + Value"); ) ) ), @@ -1186,7 +1186,26 @@ void PatternRulesetBuilder::append_from(const Span& sp, const ::HIR::Pattern& pa m_field_path.pop_back(); ), (Struct, - TODO(sp, "Match over struct - Named + Struct"); + m_field_path.push_back(0); + // NOTE: Sort field patterns to ensure that patterns are in order between arms + for(const auto& fld : sd) + { + ::HIR::TypeRef tmp; + const auto& sty_mono = (monomorphise_type_needed(fld.second.ent) ? tmp = monomorph(fld.second.ent) : fld.second.ent); + + auto it = ::std::find_if( pe.sub_patterns.begin(), pe.sub_patterns.end(), [&](const auto& x){ return x.first == fld.first; } ); + if( it == pe.sub_patterns.end() ) + { + ::HIR::Pattern any_pat {}; + this->append_from(sp, any_pat, sty_mono); + } + else + { + this->append_from(sp, it->second, sty_mono); + } + m_field_path.back() ++; + } + m_field_path.pop_back(); ) ) ) |