diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 9fb0fb24e7..599ef708b4 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 { diff --git a/vlib/v/checker/tests/no_interface_str.out b/vlib/v/checker/tests/no_interface_str.out new file mode 100644 index 0000000000..871c9a8c7d --- /dev/null +++ b/vlib/v/checker/tests/no_interface_str.out @@ -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 | } diff --git a/vlib/v/checker/tests/no_interface_str.vv b/vlib/v/checker/tests/no_interface_str.vv new file mode 100644 index 0000000000..b0da84aa33 --- /dev/null +++ b/vlib/v/checker/tests/no_interface_str.vv @@ -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()) +}