diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 86a2eee25e..e6533da5ab 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -255,6 +255,9 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { struct_init.typ = c.expected_type } type_sym := c.table.get_type_symbol(struct_init.typ) + if type_sym.kind == .interface_ { + c.error('cannot instantiate interface `$type_sym.name`', struct_init.pos) + } if !type_sym.is_public && type_sym.kind != .placeholder && type_sym.mod != c.mod { c.error('type `$type_sym.name` is private', struct_init.pos) } diff --git a/vlib/v/checker/tests/no_interface_instantiation_a.out b/vlib/v/checker/tests/no_interface_instantiation_a.out new file mode 100644 index 0000000000..2efdfb9dc8 --- /dev/null +++ b/vlib/v/checker/tests/no_interface_instantiation_a.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/no_interface_instantiation_a.v:4:10: error: cannot instantiate interface `Speaker` + 2 | + 3 | fn main() { + 4 | _ := Speaker{} + | ~~~~~~~~~ + 5 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/no_interface_instantiation_a.vv b/vlib/v/checker/tests/no_interface_instantiation_a.vv new file mode 100644 index 0000000000..52484b68ec --- /dev/null +++ b/vlib/v/checker/tests/no_interface_instantiation_a.vv @@ -0,0 +1,5 @@ +interface Speaker {} + +fn main() { + _ := Speaker{} +} diff --git a/vlib/v/checker/tests/no_interface_instantiation_b.out b/vlib/v/checker/tests/no_interface_instantiation_b.out new file mode 100644 index 0000000000..2755dc08dc --- /dev/null +++ b/vlib/v/checker/tests/no_interface_instantiation_b.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/no_interface_instantiation_b.v:6:12: error: cannot instantiate interface `Speaker` + 4 | + 5 | fn main() { + 6 | my_fn({}) + | ^ + 7 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/no_interface_instantiation_b.vv b/vlib/v/checker/tests/no_interface_instantiation_b.vv new file mode 100644 index 0000000000..01d47c8d10 --- /dev/null +++ b/vlib/v/checker/tests/no_interface_instantiation_b.vv @@ -0,0 +1,7 @@ +interface Speaker {} + +fn my_fn(s Speaker) {} + +fn main() { + my_fn({}) +} diff --git a/vlib/v/checker/tests/no_interface_instantiation_c.out b/vlib/v/checker/tests/no_interface_instantiation_c.out new file mode 100644 index 0000000000..8518a22880 --- /dev/null +++ b/vlib/v/checker/tests/no_interface_instantiation_c.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/no_interface_instantiation_c.v:9:9: error: cannot instantiate interface `Speaker` + 7 | fn main() { + 8 | my_fn( + 9 | speak: 1 + | ~~~~~~~~ + 10 | ) + 11 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/no_interface_instantiation_c.vv b/vlib/v/checker/tests/no_interface_instantiation_c.vv new file mode 100644 index 0000000000..0eb6eec962 --- /dev/null +++ b/vlib/v/checker/tests/no_interface_instantiation_c.vv @@ -0,0 +1,11 @@ +interface Speaker { + speak() +} + +fn my_fn(s Speaker) {} + +fn main() { + my_fn( + speak: 1 + ) +}