checker: add error when interface i, without a .str() method, have i.str() called (#5788)

pull/5795/head^2
pancake 2020-07-10 21:47:29 +02:00 committed by GitHub
parent 2fb5c91f4d
commit 7d6ba2d07d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View File

@ -936,6 +936,10 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
}
// TODO: str methods
if method_name == 'str' {
if left_type_sym.kind == .interface_ {
iname := left_type_sym.name
c.error('interface `$iname` does not have a .str() method. Use typeof() instead', call_expr.pos)
}
call_expr.receiver_type = left_type
call_expr.return_type = table.string_type
if call_expr.args.len > 0 {

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/no_interface_str.v:18:12: error: interface `Animal` does not have a .str() method. Use typeof() instead
16 | fn moin() {
17 | a := get_animal()
18 | println(a.str())
| ~~~~~
19 | }

View File

@ -0,0 +1,19 @@
interface Animal {
speak()
}
struct Cow {
}
fn (c Cow)speak() {
println('moo')
}
fn get_animal() Animal {
return Cow{}
}
fn moin() {
a := get_animal()
println(a.str())
}