1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

gen: fix mut arr [3]int as a fn argument (#8085)

This commit is contained in:
Ruofan XU 2021-01-13 21:52:46 +08:00 committed by GitHub
parent cf93be918f
commit 245ed9160a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 5 deletions

View File

@ -240,7 +240,13 @@ fn (mut g Gen) fn_args(args []table.Param, is_variadic bool) ([]string, []string
g.definitions.write(')')
}
} else {
s := arg_type_name + ' ' + caname
// TODO: combine two operations into one once ternary in expression is fixed
mut s := if arg_type_sym.kind == .array_fixed {
arg_type_name.trim('*')
} else {
arg_type_name
}
s += ' ' + caname
g.write(s)
g.definitions.write(s)
fargs << caname

View File

@ -686,7 +686,8 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) {
fn (mut p Parser) check_fn_mutable_arguments(typ table.Type, pos token.Position) {
sym := p.table.get_type_symbol(typ)
if sym.kind !in [.array, .struct_, .map, .placeholder, .sum_type] && !typ.is_ptr() && !typ.is_pointer() {
if sym.kind !in [.array, .array_fixed, .struct_, .map, .placeholder, .sum_type] &&
!typ.is_ptr() && !typ.is_pointer() {
p.error_with_pos('mutable arguments are only allowed for arrays, maps, structs and pointers\n' +
'return values instead: `fn foo(mut n $sym.name) {` => `fn foo(n $sym.name) $sym.name {`',
pos)

View File

@ -2,7 +2,7 @@ fn test_fixed_array_can_be_assigned() {
x := 2.32
mut v := [8]f64{}
assert v[1] == 0
v = [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]!!
v = [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]!
assert v[1] == x
v[1] = 2.0
for i, e in v {
@ -20,7 +20,7 @@ fn test_fixed_array_can_be_assigned() {
fn test_fixed_array_can_be_used_in_declaration() {
x := 2.32
v := [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]!!
v := [1.0, x, 3.0,4.0,5.0,6.0,7.0,8.0]!
assert v.len == 8
assert v[1] == x
}
@ -35,7 +35,7 @@ fn test_fixed_array_can_be_assigned_to_a_struct_field() {
mut ctx := Context{}
assert ctx.vb.len == 8
x := 2.32
ctx.vb = [1.1, x, 3.3, 4.4, 5.0, 6.0, 7.0, 8.9]!!
ctx.vb = [1.1, x, 3.3, 4.4, 5.0, 6.0, 7.0, 8.9]!
assert ctx.vb[1] == x
assert ctx.vb[7] == 8.9
for i, e in ctx.vb {
@ -49,3 +49,22 @@ fn test_fixed_array_can_be_assigned_to_a_struct_field() {
println( ctx.vb[3] )
*/
}
fn multiply_by_two(mut arr [3]int) {
for i in 0..arr.len {
arr[i] *= 2
}
}
fn change_first_element(mut arr [3][3]int) {
arr[0][0] = 0
}
fn test_fixed_array_can_be_passed_as_mut_arg() {
mut arr := [1,2,3]!
multiply_by_two(mut arr)
assert arr == [2,4,6]!
mut arr2 := [[1,2,3]!, [4,5,6]!, [7,8,9]!]!
change_first_element(mut arr2)
assert arr2 == [[0,2,3]!, [4,5,6]!, [7,8,9]!]!
}