Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix private type #23544

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions vlib/builtin/cfns_wrapper.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ type FnSortCB = fn (const_a voidptr, const_b voidptr) int
fn vqsort(base voidptr, nmemb usize, size usize, sort_cb FnSortCB) {
C.qsort(base, nmemb, size, voidptr(sort_cb))
}

type MyPrivateType = u32
27 changes: 23 additions & 4 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -5293,10 +5293,29 @@ fn (mut c Checker) ensure_type_exists(typ ast.Type, pos token.Pos) bool {
return false
}
sym := c.table.sym(typ)
if !c.is_builtin_mod && sym.kind == .struct && !sym.is_pub && sym.mod != c.mod {
c.error('struct `${sym.name}` was declared as private to module `${sym.mod}`, so it can not be used inside module `${c.mod}`',
pos)
return false
if !c.is_builtin_mod && !sym.is_pub && sym.mod != c.mod {
if sym.kind == .struct {
c.error('struct `${sym.name}` was declared as private to module `${sym.mod}`, so it can not be used inside module `${c.mod}`',
pos)
return false
}
if sym.kind == .alias || sym.kind == .sum_type {
c.error('module `${sym.mod}` type `${sym.name}` is private', pos)
return false
}
if sym.kind == .function {
fn_info := sym.info as ast.FnType
// hack: recover fn mod from func name
mut fn_mod := fn_info.func.name.all_before_last('.')
if fn_mod == fn_info.func.name {
fn_mod = 'builtin'
}
if fn_mod != '' && fn_mod != c.mod && fn_info.func.name != '' {
c.error('module `${fn_mod}` function type `${fn_info.func.name}` is private',
pos)
return false
}
}
}
match sym.kind {
.placeholder {
Expand Down
15 changes: 15 additions & 0 deletions vlib/v/checker/tests/type_alias_decl_private_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type_alias_decl_private_err.vv:7:15: error: module `builtin` function type `FnSortCB` is private
5 | }
6 |
7 | fn hello(func FnSortCB) {
| ~~~~~~~~
8 | }
9 |
type_alias_decl_private_err.vv:11:11: error: module `cli` type `cli.Defaults` is private
9 |
10 | fn main() {
11 | _ := cli.Defaults(true)
| ~~~~~~~~~~~~~~
12 | hello(my_sort)
13 | }

13 changes: 13 additions & 0 deletions vlib/v/checker/tests/type_alias_decl_private_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import cli

fn my_sort(a voidptr, b voidptr) int {
return 1
}

fn hello(func FnSortCB) {
}

fn main() {
_ := cli.Defaults(true)
hello(my_sort)
}
Loading