Skip to content

Commit

Permalink
parser: add better error for mut variadic fn argument (#21063)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Mar 19, 2024
1 parent 8b7f908 commit 44c78ed
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions vlib/v/parser/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,10 @@ fn (mut p Parser) fn_params() ([]ast.Param, bool, bool) {
}
if is_mut {
if !param_type.has_flag(.generic) {
if is_variadic {
p.error_with_pos('variadic arguments cannot be `mut`, `shared` or `atomic`',
pos)
}
if is_shared {
p.check_fn_shared_arguments(param_type, pos)
} else if is_atomic {
Expand Down Expand Up @@ -1027,6 +1031,10 @@ fn (mut p Parser) fn_params() ([]ast.Param, bool, bool) {
}
if is_mut {
if !typ.has_flag(.generic) {
if is_variadic {
p.error_with_pos('variadic arguments cannot be `mut`, `shared` or `atomic`',
pos)
}
if is_shared {
p.check_fn_shared_arguments(typ, pos)
} else if is_atomic {
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/parser/tests/fn_alias_arg_variadic_mut_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vlib/v/parser/tests/fn_alias_arg_variadic_mut_err.vv:1:22: error: variadic arguments cannot be `mut`, `shared` or `atomic`
1 | type Foo = fn(mut ...string)
| ~~~~~~
1 change: 1 addition & 0 deletions vlib/v/parser/tests/fn_alias_arg_variadic_mut_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Foo = fn(mut ...string)
5 changes: 5 additions & 0 deletions vlib/v/parser/tests/fn_arg_variadic_mut_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vlib/v/parser/tests/fn_arg_variadic_mut_err.vv:1:26: error: variadic arguments cannot be `mut`, `shared` or `atomic`
1 | fn load(mut filepaths ...string) {
| ~~~~~~
2 | println(filepaths[0])
3 | }
3 changes: 3 additions & 0 deletions vlib/v/parser/tests/fn_arg_variadic_mut_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn load(mut filepaths ...string) {
println(filepaths[0])
}

0 comments on commit 44c78ed

Please sign in to comment.