diff --git a/vlib/v/tests/raw_string_test.v b/vlib/v/tests/raw_string_test.v index 762d532ff2..5dad86a7b6 100644 --- a/vlib/v/tests/raw_string_test.v +++ b/vlib/v/tests/raw_string_test.v @@ -1,3 +1,39 @@ +const ca = r'x\n' + +const cb = 'x\n' + +const cc = ca + cb + +const cd = cc + cc + +const ce = cd + cd + fn test_raw_string_backslash() { assert r'\' == r'\' } + +fn test_raw_string_not_escaped_by_transformer() { + assert r'a\nb' + r'a\nb' == r'a\nba\nb' + assert 'a\nb' + r'a\nb' == 'a\nba\\nb' +} + +// this test will cause test failure (see #12604) +// fn test_many_pluses() { +// a := r'x\n' +// assert a == ca +// b := 'x\n' +// assert b == cb +// c := a + b +// assert c == cc // this fails +// d := c + c +// assert d == cd +// e := d + d +// assert e == ce +// println(e) +// result := r'x\nx +// x\nx +// x\nx +// x\nx +// ' +// assert e == result +// } diff --git a/vlib/v/transformer/transformer.v b/vlib/v/transformer/transformer.v index 6072fe16f5..06c5fa3fbb 100644 --- a/vlib/v/transformer/transformer.v +++ b/vlib/v/transformer/transformer.v @@ -2,6 +2,7 @@ module transformer import v.pref import v.ast +import v.util pub struct Transformer { pref &pref.Preferences @@ -379,10 +380,10 @@ pub fn (t Transformer) infix_expr(original ast.InfixExpr) ast.Expr { } } .plus { - return ast.StringLiteral{ - val: left_node.val + right_node.val - pos: pos - } + return if t.pref.backend == .c { ast.Expr(ast.StringLiteral{ + val: util.smart_quote(left_node.val, left_node.is_raw) + util.smart_quote(right_node.val, right_node.is_raw) + pos: pos + }) } else { ast.Expr(node) } } else { return node