1
0
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:
Enzo Baldisserri 2020-05-13 19:20:15 +02:00 committed by GitHub
parent 3270545953
commit 1ca04e6113
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 0 deletions

View File

@ -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)
}

View 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 | }

View File

@ -0,0 +1,5 @@
interface Speaker {}
fn main() {
_ := Speaker{}
}

View 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 | }

View File

@ -0,0 +1,7 @@
interface Speaker {}
fn my_fn(s Speaker) {}
fn main() {
my_fn({})
}

View 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 | }

View File

@ -0,0 +1,11 @@
interface Speaker {
speak()
}
fn my_fn(s Speaker) {}
fn main() {
my_fn(
speak: 1
)
}