checker: add error when interface i, without a .str() method, have i.str() called (#5788)
parent
2fb5c91f4d
commit
7d6ba2d07d
|
@ -936,6 +936,10 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
|
||||||
}
|
}
|
||||||
// TODO: str methods
|
// TODO: str methods
|
||||||
if method_name == 'str' {
|
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.receiver_type = left_type
|
||||||
call_expr.return_type = table.string_type
|
call_expr.return_type = table.string_type
|
||||||
if call_expr.args.len > 0 {
|
if call_expr.args.len > 0 {
|
||||||
|
|
|
@ -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 | }
|
|
@ -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())
|
||||||
|
}
|
Loading…
Reference in New Issue