Skip to content

Commit 1b91ac3

Browse files
authored
checker: overwrite stale unresolved fixed-array sym for struct fields (fix #27078) (#27081)
1 parent fc8ef0e commit 1b91ac3

5 files changed

Lines changed: 44 additions & 0 deletions

File tree

vlib/v/checker/struct.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,23 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
121121
sym := c.table.sym(field.typ)
122122
if sym.info is ast.ArrayFixed && c.array_fixed_has_unresolved_size(sym.info) {
123123
mut size_expr := unsafe { sym.info.size_expr }
124+
old_typ := field.typ
124125
field.typ = c.eval_array_fixed_sizes(mut size_expr, 0, sym.info.elem_type)
125126
for mut symfield in struct_sym.info.fields {
126127
if symfield.name == field.name {
127128
symfield.typ = field.typ
128129
}
129130
}
131+
// Overwrite the previously unresolved type symbol so that earlier
132+
// expressions which captured its idx (e.g. IndexExpr.left_type from
133+
// another file checked first) observe the resolved size. See #27078.
134+
if old_typ.idx() != field.typ.idx() {
135+
new_sym := c.table.sym(field.typ)
136+
mut old_sym := c.table.type_symbols[old_typ.idx()]
137+
old_sym.name = new_sym.name
138+
old_sym.cname = new_sym.cname
139+
old_sym.info = new_sym.info
140+
}
130141
}
131142
}
132143

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module main
2+
3+
fn main() {
4+
mut t := TestStruct{}
5+
t.list[TestEnum.one] = 1
6+
assert t.list[TestEnum.one] == 1
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module main
2+
3+
enum TestEnum {
4+
one
5+
two
6+
_max
7+
}
8+
9+
const te_max = int(TestEnum._max)
10+
11+
struct TestStruct {
12+
mut:
13+
list [te_max]int
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Module {
2+
name: 'project_issue_27078'
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import os
2+
3+
const vexe = os.quoted_path(@VEXE)
4+
const issue_27078_project = os.join_path(os.dir(@FILE), 'project_issue_27078')
5+
6+
fn test_fixed_array_with_int_cast_enum_const_in_separate_file_runs() {
7+
res := os.execute('${vexe} run ${os.quoted_path(issue_27078_project)}')
8+
assert res.exit_code == 0, res.output
9+
}

0 commit comments

Comments
 (0)