Skip to content

Commit

Permalink
parser: fix parse error in the type of a ref array when the element t…
Browse files Browse the repository at this point in the history
…ype is a structure of another mod(fix #19033) (#19039)
  • Loading branch information
shove70 committed Aug 3, 2023
1 parent fe9bdd4 commit b556f13
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
34 changes: 22 additions & 12 deletions vlib/v/parser/expr.v
Expand Up @@ -182,18 +182,28 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr {
if p.expecting_type {
// parse json.decode type (`json.decode([]User, s)`)
node = p.name_expr()
} else if p.is_amp && p.peek_tok.kind == .rsbr && p.peek_token(3).kind != .lcbr {
pos := p.tok.pos()
typ := p.parse_type()
typname := p.table.sym(typ).name
p.check(.lpar)
expr := p.expr(0)
p.check(.rpar)
node = ast.CastExpr{
typ: typ
typname: typname
expr: expr
pos: pos
} else if p.is_amp && p.peek_tok.kind == .rsbr {
mut n := 2
mut peek_n_tok := p.peek_token(n)
for peek_n_tok.kind in [.name, .dot] {
n++
peek_n_tok = p.peek_token(n)
}
if peek_n_tok.kind != .lcbr {
pos := p.tok.pos()
typ := p.parse_type()
typname := p.table.sym(typ).name
p.check(.lpar)
expr := p.expr(0)
p.check(.rpar)
node = ast.CastExpr{
typ: typ
typname: typname
expr: expr
pos: pos
}
} else {
node = p.array_init(false)
}
} else {
node = p.array_init(false)
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/tests/parse_type_of_ref_array_from_another_mod_test.v
@@ -0,0 +1,17 @@
import another_module

struct SomeStruct {}

fn type_from_another_mod() {
_ = &[]another_module.SomeStruct{}
}

fn type_from_current_mod() {
_ = &[]SomeStruct{}
}

fn test_parse_type_of_ref_array() {
type_from_another_mod()
type_from_current_mod()
assert true
}

0 comments on commit b556f13

Please sign in to comment.