checker: fix interpolation recursive str (fix #1905) (#7141)

pull/7152/head
yuyi 2020-12-06 04:41:54 +08:00 committed by GitHub
parent 005676fe60
commit 29857cb9d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View File

@ -390,6 +390,10 @@ pub fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) table.T
}
node.need_fmts[i] = fmt != c.get_default_fmt(ftyp, typ)
}
// check recursive str
if c.cur_fn.is_method && c.cur_fn.name == 'str' && c.cur_fn.receiver.name == expr.str() {
c.error('cannot call `str()` method recursively', expr.position())
}
}
return table.string_type
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/interpolation_recursive_str_err.vv:8:8: error: cannot call `str()` method recursively
6 |
7 | fn (t Test) str() string {
8 | _ = '$t'
| ^
9 | return 'test'
10 | }

View File

@ -0,0 +1,15 @@
module main
struct Test {
a int
}
fn (t Test) str() string {
_ = '$t'
return 'test'
}
fn main() {
a := Test{}
println(a)
}