checker: check reference return to be really reference (#7739)
parent
a9ab79d301
commit
74ea5ac99f
|
@ -52,7 +52,7 @@ pub mut:
|
||||||
// so that the user can set callbacks, read meta information, etc.
|
// so that the user can set callbacks, read meta information, etc.
|
||||||
pub fn info() &LiveReloadInfo {
|
pub fn info() &LiveReloadInfo {
|
||||||
if C.g_live_info != 0 {
|
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
|
// When the current program is not compiled with -live, simply
|
||||||
// return a new empty struct LiveReloadInfo in order to prevent
|
// return a new empty struct LiveReloadInfo in order to prevent
|
||||||
|
|
|
@ -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',
|
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)
|
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 {
|
if exp_is_optional && return_stmt.exprs.len > 0 {
|
||||||
expr0 := return_stmt.exprs[0]
|
expr0 := return_stmt.exprs[0]
|
||||||
|
|
|
@ -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 | }
|
|
@ -0,0 +1,14 @@
|
||||||
|
struct Aa {
|
||||||
|
a int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn f() &Aa {
|
||||||
|
return Aa{
|
||||||
|
a: 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
x := f()
|
||||||
|
println(x.a)
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue