Skip to content

Commit fe37da3

Browse files
authored
v.transformer: fix string always escaped by transformer (#12603)
1 parent 6d6a23a commit fe37da3

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

vlib/v/tests/raw_string_test.v

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
const ca = r'x\n'
2+
3+
const cb = 'x\n'
4+
5+
const cc = ca + cb
6+
7+
const cd = cc + cc
8+
9+
const ce = cd + cd
10+
111
fn test_raw_string_backslash() {
212
assert r'\' == r'\'
313
}
14+
15+
fn test_raw_string_not_escaped_by_transformer() {
16+
assert r'a\nb' + r'a\nb' == r'a\nba\nb'
17+
assert 'a\nb' + r'a\nb' == 'a\nba\\nb'
18+
}
19+
20+
// this test will cause test failure (see #12604)
21+
// fn test_many_pluses() {
22+
// a := r'x\n'
23+
// assert a == ca
24+
// b := 'x\n'
25+
// assert b == cb
26+
// c := a + b
27+
// assert c == cc // this fails
28+
// d := c + c
29+
// assert d == cd
30+
// e := d + d
31+
// assert e == ce
32+
// println(e)
33+
// result := r'x\nx
34+
// x\nx
35+
// x\nx
36+
// x\nx
37+
// '
38+
// assert e == result
39+
// }

vlib/v/transformer/transformer.v

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module transformer
22

33
import v.pref
44
import v.ast
5+
import v.util
56

67
pub struct Transformer {
78
pref &pref.Preferences
@@ -379,10 +380,10 @@ pub fn (t Transformer) infix_expr(original ast.InfixExpr) ast.Expr {
379380
}
380381
}
381382
.plus {
382-
return ast.StringLiteral{
383-
val: left_node.val + right_node.val
384-
pos: pos
385-
}
383+
return if t.pref.backend == .c { ast.Expr(ast.StringLiteral{
384+
val: util.smart_quote(left_node.val, left_node.is_raw) + util.smart_quote(right_node.val, right_node.is_raw)
385+
pos: pos
386+
}) } else { ast.Expr(node) }
386387
}
387388
else {
388389
return node

0 commit comments

Comments
 (0)