diff --git a/vlib/live/common.v b/vlib/live/common.v index 5e01139e10..c661f5a79b 100644 --- a/vlib/live/common.v +++ b/vlib/live/common.v @@ -52,7 +52,7 @@ pub mut: // so that the user can set callbacks, read meta information, etc. pub fn info() &LiveReloadInfo { if C.g_live_info != 0 { - return C.g_live_info + return &LiveReloadInfo(C.g_live_info) } // When the current program is not compiled with -live, simply // return a new empty struct LiveReloadInfo in order to prevent diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8b05c2f973..3c7e31feeb 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2047,6 +2047,12 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) { c.error('fn `$c.cur_fn.name` expects you to return a non reference type `${c.table.type_to_str(exp_type)}`, but you are returning `${c.table.type_to_str(got_typ)}` instead', pos) } + if (exp_type.is_ptr() || exp_type.is_pointer()) && + (!got_typ.is_ptr() && !got_typ.is_pointer()) && got_typ != table.any_int_type { + pos := return_stmt.exprs[i].position() + c.error('fn `$c.cur_fn.name` expects you to return a reference type `${c.table.type_to_str(exp_type)}`, but you are returning `${c.table.type_to_str(got_typ)}` instead', + pos) + } } if exp_is_optional && return_stmt.exprs.len > 0 { expr0 := return_stmt.exprs[0] diff --git a/vlib/v/checker/tests/reference_return.out b/vlib/v/checker/tests/reference_return.out new file mode 100644 index 0000000000..4c9b3c2966 --- /dev/null +++ b/vlib/v/checker/tests/reference_return.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/reference_return.vv:6:9: error: fn `f` expects you to return a reference type `&Aa`, but you are returning `Aa` instead + 4 | + 5 | fn f() &Aa { + 6 | return Aa{ + | ~~~ + 7 | a: 1 + 8 | } diff --git a/vlib/v/checker/tests/reference_return.vv b/vlib/v/checker/tests/reference_return.vv new file mode 100644 index 0000000000..42c1e5ac67 --- /dev/null +++ b/vlib/v/checker/tests/reference_return.vv @@ -0,0 +1,14 @@ +struct Aa { + a int +} + +fn f() &Aa { + return Aa{ + a: 1 + } +} + +fn main() { + x := f() + println(x.a) +} diff --git a/vlib/v/tests/ref_return_test.v b/vlib/v/tests/ref_return_test.v new file mode 100644 index 0000000000..286e85990a --- /dev/null +++ b/vlib/v/tests/ref_return_test.v @@ -0,0 +1,14 @@ +struct Aa { + a int +} + +fn f() &Aa { + return &Aa{ + a: 5 + } +} + +fn test_ref_return() { + x := f() + assert x.a == 5 +}