Skip to content

Commit fb338d2

Browse files
authored
markused: fix []T param as used (fix #25383) (#25454)
1 parent b16c73b commit fb338d2

12 files changed

Lines changed: 124 additions & 0 deletions

‎vlib/v/markused/walker.v‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,20 @@ 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 {
1104+
// mark concrete []T param as used
1105+
for concrete_type_list in w.table.fn_generic_types[node.fkey()] {
1106+
for k, concrete_type in concrete_type_list {
1107+
if k >= stmt.params.len - 1 {
1108+
break
1109+
}
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+
}
1114+
}
1115+
}
1116+
}
11031117
}
11041118
}
11051119

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module main
2+
3+
fn test_main() {
4+
mut fake := Faker{}
5+
6+
code := fake.currency_code()
7+
codes := Currency.cases().map(it.to_code())
8+
assert codes.contains(code)
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module main
2+
3+
pub enum Country {
4+
afghanistan
5+
albania
6+
algeria
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module main
2+
3+
pub fn Country.cases() []Country {
4+
mut cases := []Country{}
5+
6+
$for country in Country.values {
7+
case := Country.from(country.name) or { panic(err) }
8+
cases << case
9+
}
10+
return cases
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module main
2+
3+
pub enum Currency {
4+
afghani
5+
argentina_peso
6+
aruba_guilder
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module main
2+
3+
pub fn (currency Currency) to_code() string {
4+
return match currency {
5+
.afghani { 'AFN' }
6+
.argentina_peso { 'ARS' }
7+
.aruba_guilder { 'AWG' }
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module main
2+
3+
pub fn Currency.cases() []Currency {
4+
mut cases := []Currency{}
5+
6+
$for currency in Currency.values {
7+
case := Currency.from(currency.name) or { panic(err) }
8+
cases << case
9+
}
10+
return cases
11+
}

‎vlib/v/tests/generic_calls/faker.v‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module main
2+
3+
import rand.pcg32 { PCG32RNG }
4+
5+
enum Lang {
6+
en
7+
}
8+
9+
pub struct Faker {
10+
pub mut:
11+
lang Lang = .en
12+
randomizer PCG32RNG
13+
seeded bool
14+
}
15+
16+
pub fn (mut this Faker) random_element[T](elements []T) T {
17+
number_of_elements := u64(elements.len)
18+
random_index := this.randomizer.u64() % number_of_elements
19+
20+
return elements[random_index] or {
21+
panic('Failed to get random element at index ${random_index} with length ${number_of_elements}.')
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module main
2+
3+
pub fn (mut this Faker) country_name() string {
4+
return this.random_element(Country.cases()).str()
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module main
2+
3+
pub fn (mut this Faker) currency_code() string {
4+
return this.random_element(Currency.cases()).to_code()
5+
}

0 commit comments

Comments
 (0)