Skip to content

Commit b556f13

Browse files
authored
parser: fix parse error in the type of a ref array when the element type is a structure of another mod(fix #19033) (#19039)
1 parent fe9bdd4 commit b556f13

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

vlib/v/parser/expr.v

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,28 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr {
182182
if p.expecting_type {
183183
// parse json.decode type (`json.decode([]User, s)`)
184184
node = p.name_expr()
185-
} else if p.is_amp && p.peek_tok.kind == .rsbr && p.peek_token(3).kind != .lcbr {
186-
pos := p.tok.pos()
187-
typ := p.parse_type()
188-
typname := p.table.sym(typ).name
189-
p.check(.lpar)
190-
expr := p.expr(0)
191-
p.check(.rpar)
192-
node = ast.CastExpr{
193-
typ: typ
194-
typname: typname
195-
expr: expr
196-
pos: pos
185+
} else if p.is_amp && p.peek_tok.kind == .rsbr {
186+
mut n := 2
187+
mut peek_n_tok := p.peek_token(n)
188+
for peek_n_tok.kind in [.name, .dot] {
189+
n++
190+
peek_n_tok = p.peek_token(n)
191+
}
192+
if peek_n_tok.kind != .lcbr {
193+
pos := p.tok.pos()
194+
typ := p.parse_type()
195+
typname := p.table.sym(typ).name
196+
p.check(.lpar)
197+
expr := p.expr(0)
198+
p.check(.rpar)
199+
node = ast.CastExpr{
200+
typ: typ
201+
typname: typname
202+
expr: expr
203+
pos: pos
204+
}
205+
} else {
206+
node = p.array_init(false)
197207
}
198208
} else {
199209
node = p.array_init(false)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import another_module
2+
3+
struct SomeStruct {}
4+
5+
fn type_from_another_mod() {
6+
_ = &[]another_module.SomeStruct{}
7+
}
8+
9+
fn type_from_current_mod() {
10+
_ = &[]SomeStruct{}
11+
}
12+
13+
fn test_parse_type_of_ref_array() {
14+
type_from_another_mod()
15+
type_from_current_mod()
16+
assert true
17+
}

0 commit comments

Comments
 (0)