mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: prevent interface instantiation
This commit is contained in:
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
6
vlib/v/checker/tests/no_interface_instantiation_a.out
Normal file
6
vlib/v/checker/tests/no_interface_instantiation_a.out
Normal file
@ -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 | }
|
5
vlib/v/checker/tests/no_interface_instantiation_a.vv
Normal file
5
vlib/v/checker/tests/no_interface_instantiation_a.vv
Normal file
@ -0,0 +1,5 @@
|
||||
interface Speaker {}
|
||||
|
||||
fn main() {
|
||||
_ := Speaker{}
|
||||
}
|
6
vlib/v/checker/tests/no_interface_instantiation_b.out
Normal file
6
vlib/v/checker/tests/no_interface_instantiation_b.out
Normal file
@ -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 | }
|
7
vlib/v/checker/tests/no_interface_instantiation_b.vv
Normal file
7
vlib/v/checker/tests/no_interface_instantiation_b.vv
Normal file
@ -0,0 +1,7 @@
|
||||
interface Speaker {}
|
||||
|
||||
fn my_fn(s Speaker) {}
|
||||
|
||||
fn main() {
|
||||
my_fn({})
|
||||
}
|
7
vlib/v/checker/tests/no_interface_instantiation_c.out
Normal file
7
vlib/v/checker/tests/no_interface_instantiation_c.out
Normal file
@ -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 | }
|
11
vlib/v/checker/tests/no_interface_instantiation_c.vv
Normal file
11
vlib/v/checker/tests/no_interface_instantiation_c.vv
Normal file
@ -0,0 +1,11 @@
|
||||
interface Speaker {
|
||||
speak()
|
||||
}
|
||||
|
||||
fn my_fn(s Speaker) {}
|
||||
|
||||
fn main() {
|
||||
my_fn(
|
||||
speak: 1
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue
Block a user