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

View File

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

View File

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