checker: prevent interface instantiation
parent
3270545953
commit
1ca04e6113
|
@ -255,6 +255,9 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type {
|
||||||
struct_init.typ = c.expected_type
|
struct_init.typ = c.expected_type
|
||||||
}
|
}
|
||||||
type_sym := c.table.get_type_symbol(struct_init.typ)
|
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 {
|
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)
|
c.error('type `$type_sym.name` is private', struct_init.pos)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 | }
|
|
@ -0,0 +1,5 @@
|
||||||
|
interface Speaker {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
_ := Speaker{}
|
||||||
|
}
|
|
@ -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 | }
|
|
@ -0,0 +1,7 @@
|
||||||
|
interface Speaker {}
|
||||||
|
|
||||||
|
fn my_fn(s Speaker) {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
my_fn({})
|
||||||
|
}
|
|
@ -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 | }
|
|
@ -0,0 +1,11 @@
|
||||||
|
interface Speaker {
|
||||||
|
speak()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn my_fn(s Speaker) {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
my_fn(
|
||||||
|
speak: 1
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue