ctx->add_statement (ret_var_stmt);
- ctx->push_fn (fndecl, return_address);
+ ctx->push_fn (fndecl, return_address, tyret);
compile_function_body (fndecl, *function_body, tyret);
tree bind_tree = ctx->pop_block ();
&ret_var_stmt);
ctx->add_statement (ret_var_stmt);
- ctx->push_fn (fndecl, return_address);
+ ctx->push_fn (fndecl, return_address, resolved_type);
if (is_block_expr)
{
{
tree fndecl;
::Bvariable *ret_addr;
+ TyTy::BaseType *retty;
};
class Context
return true;
}
- void push_fn (tree fn, ::Bvariable *ret_addr)
+ void push_fn (tree fn, ::Bvariable *ret_addr, TyTy::BaseType *retty)
{
- fn_stack.push_back (fncontext{fn, ret_addr});
+ fn_stack.push_back (fncontext{fn, ret_addr, retty});
}
void pop_fn () { fn_stack.pop_back (); }
tree return_value = expr.has_return_expr ()
? CompileExpr::Compile (expr.return_expr.get (), ctx)
: unit_expression (ctx, expr.get_locus ());
+
+ if (expr.has_return_expr ())
+ {
+ HirId id = expr.get_mappings ().get_hirid ();
+ Location rvalue_locus = expr.return_expr->get_locus ();
+
+ TyTy::BaseType *expected = fncontext.retty;
+ Location lvalue_locus
+ = ctx->get_mappings ()->lookup_location (expected->get_ref ());
+
+ TyTy::BaseType *actual = nullptr;
+ bool ok = ctx->get_tyctx ()->lookup_type (
+ expr.return_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 (fncontext.fndecl, return_value,
expr.get_locus ());
ctx->add_statement (ret_var_stmt);
- ctx->push_fn (fndecl, return_address);
+ ctx->push_fn (fndecl, return_address, tyret);
if (is_block_expr)
{
? TypeCheckExpr::Resolve (expr.get_expr ())
: TyTy::TupleType::get_unit_type (expr.get_mappings ().get_hirid ());
- infered = unify_site (expr.get_mappings ().get_hirid (),
- TyTy::TyWithLocation (fn_return_tyty),
- TyTy::TyWithLocation (expr_ty, expr_locus),
- expr.get_locus ());
+ coercion_site (expr.get_mappings ().get_hirid (),
+ TyTy::TyWithLocation (fn_return_tyty),
+ TyTy::TyWithLocation (expr_ty, expr_locus), expr.get_locus ());
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ());
}
--- /dev/null
+// { 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 {
+ return x;
+}
+
+fn clobber_stack() {
+ let _z: [usize; 8] = [1, 2, 3, 4, 5, 6, 7, 8];
+}
+
+extern "C" {
+ fn printf(s: *const i8, ...) -> i32;
+}
+
+fn main() -> i32 {
+ let x = 123;
+ let y = get_dyn_a(&x);
+ clobber_stack();
+ let value = y.get_int();
+ let fmt_string = "%d\n\0" as *const str as *const i8;
+ unsafe {
+ printf(fmt_string, value);
+ }
+
+ 0
+}