Skip to content

Commit 7c780ed

Browse files
authored
cgen, markused, checker: fix iteration over mutable option (fix #24860) (#25199)
1 parent 9fb8aae commit 7c780ed

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎vlib/v/checker/for.v‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
254254
}
255255
if node.val_is_mut {
256256
value_type = value_type.ref()
257+
if value_type.has_flag(.option) {
258+
value_type = value_type.set_flag(.option_mut_param_t)
259+
}
257260
match mut node.cond {
258261
ast.Ident {
259262
if mut node.cond.obj is ast.Var {

‎vlib/v/gen/c/assign.v‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,9 +866,19 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
866866
g.write('*')
867867
}
868868
if node_.op == .assign && var_type.has_flag(.option_mut_param_t) {
869+
if val is ast.CastExpr {
870+
g.expr(left)
871+
g.write('->state = ')
872+
g.expr(val)
873+
g.writeln('.state;')
874+
}
869875
g.write('memcpy(&')
870876
g.expr(left)
871-
g.write('->data, *(${g.styp(val_type)}**)&')
877+
if val is ast.CastExpr {
878+
g.write('->data, ')
879+
} else {
880+
g.write('->data, *(${g.styp(val_type)}**)&')
881+
}
872882
} else if var_type.has_flag(.option_mut_param_t) {
873883
g.expr(left)
874884
g.write(' = ')

‎vlib/v/markused/walker.v‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,10 @@ fn (mut w Walker) mark_resource_dependencies() {
13001300
w.fn_by_name(builderptr_idx + '.write_string')
13011301
w.fn_by_name('strings.new_builder')
13021302
w.uses_free[ast.string_type] = true
1303+
1304+
if w.table.dumps.keys().any(ast.Type(u32(it)).has_flag(.option)) {
1305+
w.fn_by_name('str_intp')
1306+
}
13031307
}
13041308
if w.features.auto_str_ptr {
13051309
w.fn_by_name('isnil')
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module main
2+
3+
fn test_main() {
4+
mut data := [3]?int{}
5+
6+
for mut d in data {
7+
d = ?int(1)
8+
assert '${d}' == 'Option(1)'
9+
}
10+
11+
for i in 0 .. data.len {
12+
data[i] = ?int(3)
13+
}
14+
assert '${data}' == '[Option(3), Option(3), Option(3)]'
15+
}

0 commit comments

Comments
 (0)