transformer: fix a bug with string literal length (#14757)

master
lemon 2022-06-14 18:42:45 +09:00 committed by GitHub
parent 6d8a0ad15d
commit e4e858b132
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 3 deletions

View File

@ -1,2 +1,4 @@
5 5
7 7
6
6

View File

@ -1,6 +1,10 @@
fn main() { fn main() {
a := 'Vlang'.len a := 'Vlang'.len
b := r'Vlang\n'.len b := r'Vlang\n'.len
c := 'Vlang\n'.len
d := 'Vlang$a'.len
println(a) println(a)
println(b) println(b)
println(c)
println(d)
} }

View File

@ -637,12 +637,14 @@ pub fn (mut t Transformer) expr(mut node ast.Expr) ast.Expr {
ast.SelectorExpr { ast.SelectorExpr {
node.expr = t.expr(mut node.expr) node.expr = t.expr(mut node.expr)
if mut node.expr is ast.StringLiteral && node.field_name == 'len' { if mut node.expr is ast.StringLiteral && node.field_name == 'len' {
if !node.expr.val.contains('\\') || node.expr.is_raw {
return ast.IntegerLiteral{ return ast.IntegerLiteral{
val: node.expr.val.len.str() val: node.expr.val.len.str()
pos: node.pos pos: node.pos
} }
} }
} }
}
ast.SizeOf { ast.SizeOf {
node.expr = t.expr(mut node.expr) node.expr = t.expr(mut node.expr)
} }