Skip to content

Commit 9f740ba

Browse files
committed
checker: handle alias container str uses
1 parent b2e4080 commit 9f740ba

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

vlib/v/checker/used_features.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,15 @@ fn (c &Checker) alias_parent_concrete_types(info ast.Alias) []ast.Type {
472472
ast.GenericInst {
473473
return parent_sym.info.concrete_types.clone()
474474
}
475+
ast.Array {
476+
return [parent_sym.info.elem_type]
477+
}
478+
ast.ArrayFixed {
479+
return [parent_sym.info.elem_type]
480+
}
481+
ast.Map {
482+
return [parent_sym.info.key_type, parent_sym.info.value_type]
483+
}
475484
ast.Alias {
476485
return c.alias_parent_concrete_types(parent_sym.info)
477486
}

vlib/v/gen/c/auto_str_methods.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,15 @@ fn (mut g Gen) alias_parent_concrete_types(info ast.Alias) []ast.Type {
718718
ast.GenericInst {
719719
return parent_sym.info.concrete_types.clone()
720720
}
721+
ast.Array {
722+
return [parent_sym.info.elem_type]
723+
}
724+
ast.ArrayFixed {
725+
return [parent_sym.info.elem_type]
726+
}
727+
ast.Map {
728+
return [parent_sym.info.key_type, parent_sym.info.value_type]
729+
}
721730
ast.Alias {
722731
return g.alias_parent_concrete_types(parent_sym.info)
723732
}

vlib/v/markused/walker.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,13 +962,15 @@ fn (mut w Walker) expr(node_ ast.Expr) {
962962
w.mark_by_type(w.table.find_or_register_array(sym.info.key_type))
963963
}
964964
} else if !node.is_method && node.args.len == 1
965-
&& node.name in ['println', 'print', 'eprint', 'eprintln'] {
965+
&& node.name in ['println', 'print', 'eprint', 'eprintln', 'panic'] {
966966
if f := w.table.find_fn(node.name) {
967967
if f.mod == 'builtin' {
968968
if node.args[0].typ != ast.string_type {
969969
w.uses_str[node.args[0].typ] = true
970+
w.mark_generic_str_method_for_type(node.args[0].typ)
970971
}
971-
if w.pref.gc_mode == .boehm_leak && (node.args[0].typ != ast.string_type
972+
if node.name != 'panic' && w.pref.gc_mode == .boehm_leak
973+
&& (node.args[0].typ != ast.string_type
972974
|| node.args[0].expr !in [ast.Ident, ast.StringLiteral, ast.SelectorExpr, ast.ComptimeSelector]) {
973975
w.uses_free[ast.string_type] = true
974976
}

vlib/v/tests/generics/generic_auto_str_nested_generic_field_test.v

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ struct InterpBox[T] {
1818

1919
type InterpIntBox = InterpBox[int]
2020

21+
type Ints = []int
22+
2123
struct StructuredContainer[T] {
2224
box Box[[]T]
2325
}
2426

27+
struct IntsContainer {
28+
values Ints
29+
}
30+
2531
struct AssertNode[T] {
2632
val T
2733
}
@@ -50,6 +56,10 @@ fn (box InterpIntBox) str[T]() string {
5056
return 'interp alias ${box.val}'
5157
}
5258

59+
fn (values Ints) str[T]() string {
60+
return 'ints ${values[0]}'
61+
}
62+
5363
fn (list AssertList[T]) str() string {
5464
return 'assert list'
5565
}
@@ -58,6 +68,12 @@ fn (bad SkippedBad[[]T]) str() string {
5868
return bad.val[0].len.str()
5969
}
6070

71+
fn maybe_panic_auto_str_container(should_panic bool) {
72+
if should_panic {
73+
panic(MyContainer[string]{})
74+
}
75+
}
76+
6177
fn test_auto_str_registers_nested_generic_field_str_method() {
6278
c := MyContainer[string]{}
6379
assert '${c}' == 'MyContainer[string]{
@@ -81,6 +97,18 @@ fn test_interpolation_registers_exact_generic_alias_str_method() {
8197
assert '${box}' == 'interp alias 987'
8298
}
8399

100+
fn test_auto_str_registers_container_alias_parent_str_method() {
101+
c := IntsContainer{
102+
values: Ints([654])
103+
}
104+
assert '${c}'.contains('values: ints 654')
105+
}
106+
107+
fn test_interpolation_registers_container_alias_parent_str_method() {
108+
values := Ints([321])
109+
assert '${values}' == 'ints 321'
110+
}
111+
84112
fn test_dump_registers_exact_generic_alias_str_method() {
85113
box := InterpIntBox{
86114
val: 654
@@ -89,6 +117,11 @@ fn test_dump_registers_exact_generic_alias_str_method() {
89117
assert dumped.val == 654
90118
}
91119

120+
fn test_panic_marks_auto_str_dependencies_under_skip_unused() {
121+
maybe_panic_auto_str_container(false)
122+
assert true
123+
}
124+
92125
fn test_assert_auto_str_registers_nested_generic_field_str_method() {
93126
left := AssertContainer[string]{
94127
list: AssertList[AssertNode[string]]{

0 commit comments

Comments
 (0)