parser: check using 'mut' on fn_decl return type (#13610)
parent
0fb1eaef04
commit
bc16c61f6f
|
@ -353,7 +353,9 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||||
same_line := p.tok.line_nr == p.prev_tok.line_nr
|
same_line := p.tok.line_nr == p.prev_tok.line_nr
|
||||||
if (p.tok.kind.is_start_of_type() && (same_line || p.tok.kind != .lsbr))
|
if (p.tok.kind.is_start_of_type() && (same_line || p.tok.kind != .lsbr))
|
||||||
|| (same_line && p.tok.kind == .key_fn) {
|
|| (same_line && p.tok.kind == .key_fn) {
|
||||||
|
p.inside_fn_return = true
|
||||||
return_type = p.parse_type()
|
return_type = p.parse_type()
|
||||||
|
p.inside_fn_return = false
|
||||||
return_type_pos = return_type_pos.extend(p.prev_tok.pos())
|
return_type_pos = return_type_pos.extend(p.prev_tok.pos())
|
||||||
}
|
}
|
||||||
mut type_sym_method_idx := 0
|
mut type_sym_method_idx := 0
|
||||||
|
|
|
@ -376,6 +376,9 @@ pub fn (mut p Parser) parse_type() ast.Type {
|
||||||
p.register_auto_import('sync')
|
p.register_auto_import('sync')
|
||||||
}
|
}
|
||||||
mut nr_muls := 0
|
mut nr_muls := 0
|
||||||
|
if p.inside_fn_return && p.tok.kind == .key_mut {
|
||||||
|
p.error_with_pos('cannot use `mut` on fn return type', p.tok.pos())
|
||||||
|
}
|
||||||
if p.tok.kind == .key_mut || is_shared || is_atomic {
|
if p.tok.kind == .key_mut || is_shared || is_atomic {
|
||||||
nr_muls++
|
nr_muls++
|
||||||
p.next()
|
p.next()
|
||||||
|
|
|
@ -40,6 +40,7 @@ mut:
|
||||||
inside_or_expr bool
|
inside_or_expr bool
|
||||||
inside_for bool
|
inside_for bool
|
||||||
inside_fn bool // true even with implicit main
|
inside_fn bool // true even with implicit main
|
||||||
|
inside_fn_return bool
|
||||||
inside_unsafe_fn bool
|
inside_unsafe_fn bool
|
||||||
inside_str_interp bool
|
inside_str_interp bool
|
||||||
inside_array_lit bool
|
inside_array_lit bool
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
vlib/v/parser/tests/fn_decl_return_type_err_a.vv:3:13: error: cannot use `mut` on fn return type
|
||||||
|
1 | struct Foo{}
|
||||||
|
2 |
|
||||||
|
3 | fn maker() ?mut Foo {
|
||||||
|
| ~~~
|
||||||
|
4 | inner := &Foo{}
|
||||||
|
5 | return *inner
|
|
@ -0,0 +1,9 @@
|
||||||
|
struct Foo{}
|
||||||
|
|
||||||
|
fn maker() ?mut Foo {
|
||||||
|
inner := &Foo{}
|
||||||
|
return *inner
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
vlib/v/parser/tests/fn_decl_return_type_err_b.vv:3:13: error: cannot use `mut` on fn return type
|
||||||
|
1 | struct Foo{}
|
||||||
|
2 |
|
||||||
|
3 | fn maker() (mut Foo) {
|
||||||
|
| ~~~
|
||||||
|
4 | inner := &Foo{}
|
||||||
|
5 | return *inner
|
|
@ -0,0 +1,9 @@
|
||||||
|
struct Foo{}
|
||||||
|
|
||||||
|
fn maker() (mut Foo) {
|
||||||
|
inner := &Foo{}
|
||||||
|
return *inner
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
}
|
Loading…
Reference in New Issue