aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-04-23 09:13:59 +0000
committerGitHub <noreply@github.com>2021-04-23 09:13:59 +0000
commitde9af784e79926d01e8c030e999becc4b8bfad0f (patch)
treeeec0947af5d1e9bcbddb29c3b1935136d1948e98 /gcc/rust/backend
parentdd3c58b2ce55ce6585916607927c3bca087610c6 (diff)
parent28895c1265e19a3e854040610d8478cfb8768d0a (diff)
downloadgcc-de9af784e79926d01e8c030e999becc4b8bfad0f.zip
gcc-de9af784e79926d01e8c030e999becc4b8bfad0f.tar.gz
gcc-de9af784e79926d01e8c030e999becc4b8bfad0f.tar.bz2
Merge #380
380: Fix issues about block expression evaluation r=philberty a=lrh2000 I've tried my best to split https://github.com/Rust-GCC/gccrs/pull/364. Its core part is here. All unrelated fixes should have been removed and I will make them separate PRs, but this PR still has to contain 2 commits and introduce limited support for the never type. https://github.com/Rust-GCC/gccrs/blob/935aff52a66a70e1e96f0a6aa7e0ad37eda3f93c/gcc/testsuite/rust.test/compile/deadcode1.rs#L7-L15 https://github.com/Rust-GCC/gccrs/blob/935aff52a66a70e1e96f0a6aa7e0ad37eda3f93c/gcc/testsuite/rust.test/compile/implicit_returns1.rs#L44-L50 The reason is that the above two tests failed to pass simultaneously after the first commit. The first test requires the `if/else` expression resolves to `()` but the second test requires the `if/else` expression resolves to `<integer>`. They are conflicted. I wonder whether or not this PR is OK, and anyway thanks for your review. Co-authored-by: lrh2000 <lrh2000@pku.edu.cn>
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-context.h5
-rw-r--r--gcc/rust/backend/rust-compile-tyty.h5
-rw-r--r--gcc/rust/backend/rust-compile.cc9
3 files changed, 14 insertions, 5 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h
index 226cec6..11e791c 100644
--- a/gcc/rust/backend/rust-compile-context.h
+++ b/gcc/rust/backend/rust-compile-context.h
@@ -501,6 +501,11 @@ public:
translated = compiled_type;
}
+ void visit (TyTy::NeverType &) override
+ {
+ translated = ctx->get_backend ()->void_type ();
+ }
+
private:
TyTyResolveCompile (Context *ctx) : ctx (ctx), translated (nullptr) {}
diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h
index 28db289..ba98ac0 100644
--- a/gcc/rust/backend/rust-compile-tyty.h
+++ b/gcc/rust/backend/rust-compile-tyty.h
@@ -222,6 +222,11 @@ public:
= backend->named_type ("str", raw_str, Linemap::predeclared_location ());
}
+ void visit (TyTy::NeverType &) override
+ {
+ translated = backend->void_type ();
+ }
+
private:
TyTyCompile (::Backend *backend)
: backend (backend), translated (nullptr),
diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc
index a2f5247..9375dd0 100644
--- a/gcc/rust/backend/rust-compile.cc
+++ b/gcc/rust/backend/rust-compile.cc
@@ -256,7 +256,7 @@ CompileBlock::visit (HIR::BlockExpr &expr)
}
}
- if (expr.has_expr () && expr.tail_expr_reachable ())
+ if (expr.has_expr ())
{
// the previous passes will ensure this is a valid return
// dead code elimination should remove any bad trailing expressions
@@ -410,15 +410,14 @@ HIRCompileBase::compile_function_body (
}
}
- if (function_body->has_expr () && function_body->tail_expr_reachable ())
+ if (function_body->has_expr ())
{
// the previous passes will ensure this is a valid return
// dead code elimination should remove any bad trailing expressions
Bexpression *compiled_expr
= CompileExpr::Compile (function_body->expr.get (), ctx);
- rust_assert (compiled_expr != nullptr);
- if (has_return_type)
+ if (has_return_type && compiled_expr)
{
std::vector<Bexpression *> retstmts;
retstmts.push_back (compiled_expr);
@@ -428,7 +427,7 @@ HIRCompileBase::compile_function_body (
function_body->get_final_expr ()->get_locus_slow ());
ctx->add_statement (ret);
}
- else
+ else if (compiled_expr)
{
Bstatement *final_stmt
= ctx->get_backend ()->expression_statement (fndecl, compiled_expr);