aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <herron.philip@googlemail.com>2023-05-09 14:47:37 +0100
committerPhilip Herron <philip.herron@embecosm.com>2023-05-09 16:22:28 +0000
commiteb60cd1f6d53136467857c30140c7ef282c39085 (patch)
tree9f3b79174d1ab64943f345e411517ac2f9dda4de
parent6ff31ac29db3bdc208083461e8b7a20f7128dd42 (diff)
downloadgcc-eb60cd1f6d53136467857c30140c7ef282c39085.zip
gcc-eb60cd1f6d53136467857c30140c7ef282c39085.tar.gz
gcc-eb60cd1f6d53136467857c30140c7ef282c39085.tar.bz2
gccrs: add missing coercion site code generation for block tail
Fixes #2179 gcc/rust/ChangeLog: * backend/rust-compile-base.cc (HIRCompileBase::compile_function_body): add missing coercion_site codegen gcc/testsuite/ChangeLog: * rust/execute/torture/issue-2179.rs: New test. Signed-off-by: Philip Herron <herron.philip@googlemail.com>
-rw-r--r--gcc/rust/backend/rust-compile-base.cc13
-rw-r--r--gcc/testsuite/rust/execute/torture/issue-2179.rs29
2 files changed, 42 insertions, 0 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 4a7e04b..9d27e23 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -489,6 +489,19 @@ HIRCompileBase::compile_function_body (tree fndecl,
// we can only return this if non unit value return type
if (!fn_return_ty->is_unit ())
{
+ HirId id = function_body.get_mappings ().get_hirid ();
+ Location lvalue_locus = function_body.get_locus ();
+ Location rvalue_locus = locus;
+
+ TyTy::BaseType *expected = fn_return_ty;
+ TyTy::BaseType *actual = nullptr;
+ bool ok = ctx->get_tyctx ()->lookup_type (
+ function_body.expr->get_mappings ().get_hirid (), &actual);
+ rust_assert (ok);
+
+ return_value = coercion_site (id, return_value, actual, expected,
+ lvalue_locus, rvalue_locus);
+
tree return_stmt
= ctx->get_backend ()->return_statement (fndecl, return_value,
locus);
diff --git a/gcc/testsuite/rust/execute/torture/issue-2179.rs b/gcc/testsuite/rust/execute/torture/issue-2179.rs
new file mode 100644
index 0000000..53bacaa
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/issue-2179.rs
@@ -0,0 +1,29 @@
+// { dg-output "123\n" }
+trait A {
+ fn get_int(&self) -> i32;
+}
+
+impl A for i32 {
+ fn get_int(&self) -> i32 {
+ *self
+ }
+}
+
+fn get_dyn_a(x: &i32) -> &dyn A {
+ x
+}
+
+extern "C" {
+ fn printf(s: *const i8, ...) -> i32;
+}
+
+fn main() -> i32 {
+ let x = 123;
+ let y = get_dyn_a(&x);
+ let value = y.get_int();
+ let fmt_string = "%d\n\0" as *const str as *const i8;
+ unsafe {
+ printf(fmt_string, value);
+ }
+ return 0;
+}