Skip to content

Commit 48dc721

Browse files
authored
transformer: disable generic str_intp opt (fix #25896) (#25897)
1 parent 66b75c2 commit 48dc721

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module main
2+
3+
import strings
4+
5+
pub struct MyStruct[T] {
6+
pub mut:
7+
result T
8+
}
9+
10+
pub fn (it MyStruct[T]) indent_str[T]() string {
11+
mut res := strings.new_builder(32)
12+
res.write_string('${it.result}')
13+
return res.str()
14+
}
15+
16+
fn test_generics_str_intp() {
17+
x := MyStruct[int]{
18+
result: 100
19+
}
20+
21+
y := MyStruct[string]{
22+
result: 'hello'
23+
}
24+
25+
assert x.indent_str() == '100'
26+
assert y.indent_str() == 'hello'
27+
}

vlib/v/transformer/transformer.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,11 @@ pub fn (mut t Transformer) simplify_nested_interpolation_in_sb(mut onode ast.Stm
12681268
return false
12691269
}
12701270
original := nexpr.args[0].expr as ast.StringInterLiteral
1271+
if original.exprs.len != original.expr_types.len {
1272+
// This should be a generic type, e.g., `${it}` where `it` is type of T
1273+
// first time, `T` maybe `int`, but second time, `T` maybe `string`
1274+
return false
1275+
}
12711276
// only very simple string interpolations, without any formatting, like the following examples
12721277
// can be optimised to a list of simpler string builder calls, instead of using str_intp:
12731278
// >> sb.write_string('abc ${num}')

0 commit comments

Comments
 (0)