summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-12-24 12:07:55 +1100
committerJohn Hodge <tpg@mutabah.net>2016-12-24 12:07:55 +1100
commit3c557d2413f1c6f7c1229ea351cce44014d3078b (patch)
treed6acb60f67573b5ed552ad34cafd34a33c4eaf9e
parent07e150e9d7d638977ba3a1561d2687950a2a4668 (diff)
downloadmrust-3c557d2413f1c6f7c1229ea351cce44014d3078b.tar.gz
MIR Gen - Better scoping for single-arm matches
-rw-r--r--src/mir/from_hir.cpp24
1 files changed, 19 insertions, 5 deletions
diff --git a/src/mir/from_hir.cpp b/src/mir/from_hir.cpp
index 655591a5..7218563b 100644
--- a/src/mir/from_hir.cpp
+++ b/src/mir/from_hir.cpp
@@ -529,15 +529,29 @@ namespace {
}
else if( node.m_arms.size() == 1 && node.m_arms[0].m_patterns.size() == 1 && ! node.m_arms[0].m_cond ) {
// - Shortcut: Single-arm match
- auto scope = m_builder.new_scope_var( node.span() );
- this->define_vars_from(node.span(), node.m_arms[0].m_patterns[0]);
- this->destructure_from(node.span(), node.m_arms[0].m_patterns[0], mv$(match_val));
- this->visit_node_ptr(node.m_arms[0].m_code);
+ auto& arm = node.m_arms[0];
+ const auto& pat = arm.m_patterns[0];
+
+ auto scope = m_builder.new_scope_var(arm.m_code->span());
+ auto tmp_scope = m_builder.new_scope_temp(arm.m_code->span());
+ this->define_vars_from(node.span(), pat);
+ // TODO: Do the same shortcut as _Let?
+ this->destructure_from(node.span(), pat, mv$(match_val));
+
+ // Temp scope.
+ this->visit_node_ptr(arm.m_code);
+
if( m_builder.block_active() ) {
+ auto res = m_builder.get_result(arm.m_code->span());
+ m_builder.raise_variables( arm.m_code->span(), res );
+ m_builder.set_result(arm.m_code->span(), mv$(res));
+
+ m_builder.terminate_scope( node.span(), mv$(tmp_scope) );
m_builder.terminate_scope( node.span(), mv$(scope) );
}
else {
- auto _ = mv$(scope);
+ { auto _ = mv$(tmp_scope); }
+ { auto _ = mv$(scope); }
}
}
else {