Skip to content

Commit

Permalink
check JS fns/structs too
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Oct 15, 2024
1 parent d5cecb3 commit 875376e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
5 changes: 3 additions & 2 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
eprintln('>>> post processing node.name: ${node.name:-30} | ${node.generic_names} <=> ${c.table.cur_concrete_types}')
}
}
if node.language == .c && node.generic_names.len > 0 {
c.error('C functions cannot be declared as generic', node.pos)
if node.language in [.c, .js] && node.generic_names.len > 0 {
lang := if node.language == .c { 'C' } else { 'JS' }
c.error('${lang} functions cannot be declared as generic', node.pos)
}
// record the veb route methods (public non-generic methods):
if node.generic_names.len > 0 && node.is_pub {
Expand Down
5 changes: 3 additions & 2 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
defer {
util.timing_measure_cumulative(@METHOD)
}
if node.language == .c && node.generic_types.len > 0 {
c.error('C structs cannot be declared as generic', node.pos)
if node.language in [.c, .js] && node.generic_types.len > 0 {
lang := if node.language == .c { 'C' } else { 'JS' }
c.error('${lang} structs cannot be declared as generic', node.pos)
}
node_name := if node.scoped_name != '' { node.scoped_name } else { node.name }
mut struct_sym, struct_typ_idx := c.table.find_sym_and_type_idx(node_name)
Expand Down
26 changes: 26 additions & 0 deletions vlib/v/checker/tests/c_js_struct_and_fn_as_generics_error.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
vlib/v/checker/tests/c_js_struct_and_fn_as_generics_error.vv:1:1: error: C structs cannot be declared as generic
1 | struct C.Ptr[T] {
| ~~~~~~~~~~~~
2 | ptr &T
3 | }
vlib/v/checker/tests/c_js_struct_and_fn_as_generics_error.vv:5:1: error: C functions cannot be declared as generic
3 | }
4 |
5 | fn C.alloc[T]() &T
| ~~~~~~~~~~~~~~~~~~
6 |
7 | fn JS.encode[T](data string) T
vlib/v/checker/tests/c_js_struct_and_fn_as_generics_error.vv:7:1: error: JS functions cannot be declared as generic
5 | fn C.alloc[T]() &T
6 |
7 | fn JS.encode[T](data string) T
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 |
9 | struct JS.Some[T] {
vlib/v/checker/tests/c_js_struct_and_fn_as_generics_error.vv:9:1: error: JS structs cannot be declared as generic
7 | fn JS.encode[T](data string) T
8 |
9 | struct JS.Some[T] {
| ~~~~~~~~~~~~~~
10 | i T
11 | }
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ struct C.Ptr[T] {

fn C.alloc[T]() &T

fn JS.encode[T](data string) T

struct JS.Some[T] {
i T
}

fn main() {}
12 changes: 0 additions & 12 deletions vlib/v/checker/tests/c_struct_and_fn_as_generics_error.out

This file was deleted.

0 comments on commit 875376e

Please sign in to comment.