parser: do not allow array references + explain why

pull/6298/head
Alexander Medvednikov 2020-10-06 06:53:48 +02:00
parent 63c9b88e7f
commit ed15b40529
2 changed files with 8 additions and 5 deletions

View File

@ -159,6 +159,7 @@ pub fn (mut p Parser) parse_type() table.Type {
}
language := p.parse_language()
mut typ := table.void_type
is_array := p.tok.kind == .lsbr
if p.tok.kind != .lcbr {
pos := p.tok.position()
typ = p.parse_any_type(language, nr_muls > 0, true)
@ -177,6 +178,11 @@ pub fn (mut p Parser) parse_type() table.Type {
}
if nr_muls > 0 {
typ = typ.set_nr_muls(nr_muls)
if is_array {
p.error('V arrays are already references behind the scenes,
there is no need to use a reference to an array (e.g. use `[]string` instead of `&[]string`).
If you need to modify an array in a function, use a mutable argument instead: `fn foo(mut s []string) {}`.')
}
}
return typ
}

View File

@ -55,7 +55,7 @@ mut:
expecting_type bool // `is Type`, expecting type
errors []errors.Error
warnings []errors.Warning
vet_errors &[]string
vet_errors []string
cur_fn_name string
}
@ -72,7 +72,6 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
start_pos: 0
parent: 0
}
vet_errors: 0
}
p.init_parse_fns()
p.read_first_token()
@ -89,7 +88,6 @@ pub fn parse_text(text string, b_table &table.Table, pref &pref.Preferences, sco
errors: []errors.Error{}
warnings: []errors.Warning{}
global_scope: global_scope
vet_errors: 0
}
return p.parse()
}
@ -117,12 +115,11 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme
errors: []errors.Error{}
warnings: []errors.Warning{}
global_scope: global_scope
vet_errors: 0
}
return p.parse()
}
pub fn parse_vet_file(path string, table_ &table.Table, pref &pref.Preferences, vet_errors &[]string) ast.File {
pub fn parse_vet_file(path string, table_ &table.Table, pref &pref.Preferences, vet_errors []string) ast.File {
global_scope := &ast.Scope{
parent: 0
}