Skip to content

Commit c909513

Browse files
authored
cgen: fix markused for option generic param (fix #25501) (#25525)
1 parent e8cff1c commit c909513

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

vlib/v/markused/walker.v

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,16 +1100,23 @@ pub fn (mut w Walker) call_expr(mut node ast.CallExpr) {
11001100
} else if node.return_type.has_flag(.result) {
11011101
w.used_result++
11021102
}
1103-
if stmt.params.len > 1 && stmt.generic_names.len > 0 {
1103+
if ((node.is_method && stmt.params.len > 1) || !node.is_method)
1104+
&& stmt.generic_names.len > 0 {
11041105
// mark concrete []T param as used
1105-
for concrete_type_list in w.table.fn_generic_types[node.fkey()] {
1106+
max_param_len := if node.is_method { stmt.params.len - 1 } else { stmt.params.len }
1107+
param_i := if node.is_method { 1 } else { 0 }
1108+
for concrete_type_list in w.table.fn_generic_types[fn_name] {
11061109
for k, concrete_type in concrete_type_list {
1107-
if k >= stmt.params.len - 1 {
1110+
if k >= max_param_len {
11081111
break
11091112
}
1110-
param_typ := stmt.params[k + 1].typ
1111-
if param_typ.has_flag(.generic) && w.table.type_kind(param_typ) == .array {
1112-
w.mark_by_type(w.table.find_or_register_array(concrete_type))
1113+
param_typ := stmt.params[k + param_i].typ
1114+
if param_typ.has_flag(.generic) {
1115+
if w.table.type_kind(param_typ) == .array {
1116+
w.mark_by_type(w.table.find_or_register_array(concrete_type))
1117+
} else if param_typ.has_flag(.option) {
1118+
w.used_option++
1119+
}
11131120
}
11141121
}
11151122
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn test_main() {
2+
mut arr := []int{}
3+
mut bbb := unwrap(mut arr)
4+
bbb << 1
5+
assert bbb.len == 1
6+
assert bbb == [1]
7+
}
8+
9+
fn unwrap[T](mut t ?&T) T {
10+
return t or { panic('unexpected `none`') }
11+
}

0 commit comments

Comments
 (0)