v.transformer: fix string always escaped by transformer (#12603)

pull/12611/head
ChAoS_UnItY 2021-11-29 05:53:30 +08:00 committed by GitHub
parent 6d6a23a1c9
commit fe37da31a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 4 deletions

View File

@ -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
// }

View File

@ -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