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 symbol visibility checking #23543

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion vlib/builtin/cfns.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn C.sysctl(name &int, namelen u32, oldp voidptr, oldlenp voidptr, newp voidptr,
@[trusted]
fn C._fileno(int) int

type C.intptr_t = voidptr
pub type C.intptr_t = voidptr

fn C._get_osfhandle(fd int) C.intptr_t

Expand Down
2 changes: 1 addition & 1 deletion vlib/builtin/int.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct VContext {
allocator int
}

type byte = u8
pub type byte = u8

// ptr_str returns the address of `ptr` as a `string`.
pub fn ptr_str(ptr voidptr) string {
Expand Down
2 changes: 1 addition & 1 deletion vlib/builtin/js/int.js.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module builtin

type byte = u8
pub type byte = u8

pub const min_i8 = i8(-128)
pub const max_i8 = i8(127)
Expand Down
2 changes: 1 addition & 1 deletion vlib/builtin/wasm/wasi/int.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module builtin

type byte = u8
pub type byte = u8

// type i32 = int

Expand Down
2 changes: 1 addition & 1 deletion vlib/compress/szip/szip.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub:
pub struct C.zip_t {
}

type Zip = C.zip_t
pub type Zip = C.zip_t

pub type Fn_on_extract_entry = fn (&&char, &&char) int

Expand Down
2 changes: 1 addition & 1 deletion vlib/encoding/csv/csv_reader_random_access.v
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ pub fn (mut cr RandomAccessReader) get_cell(cfg GetCellConfig) !string {
return cr.default_cell
}

type CellValue = f32 | int | string
pub type CellValue = f32 | int | string

// get_cellt read a single cell and return a sum type CellValue
pub fn (mut cr RandomAccessReader) get_cellt(cfg GetCellConfig) !CellValue {
Expand Down
23 changes: 19 additions & 4 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -5293,10 +5293,25 @@ 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.kind in [.struct, .alias, .sum_type, .function]! && !sym.is_pub
felipensp marked this conversation as resolved.
Show resolved Hide resolved
&& sym.mod != c.mod && sym.mod != 'main' {
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'
}
felipensp marked this conversation as resolved.
Show resolved Hide resolved
if fn_mod != '' && fn_mod != c.mod && fn_info.func.name != '' && !fn_info.is_anon {
c.error('function type `${fn_info.func.name}` was declared as private to module `${fn_mod}`, so it can not be used inside module `${c.mod}`',
pos)
return false
}
} else {
c.error('${sym.kind} `${sym.name}` was declared as private to module `${sym.mod}`, so it can not be used inside module `${c.mod}`',
pos)
return false
}
}
match sym.kind {
.placeholder {
Expand Down
21 changes: 21 additions & 0 deletions vlib/v/checker/tests/modules/private_symbol.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
vlib/v/checker/tests/modules/private_symbol/main.v:10:11: error: function `priv_sym.priv` is private
8 |
9 | fn main() {
10 | priv_sym.priv()
| ~~~~~~
11 | a := priv_sym.Foo(0)
12 | dump(a)
vlib/v/checker/tests/modules/private_symbol/main.v:11:16: error: alias `priv_sym.Foo` was declared as private to module `priv_sym`, so it can not be used inside module `main`
9 | fn main() {
10 | priv_sym.priv()
11 | a := priv_sym.Foo(0)
| ~~~~~~
12 | dump(a)
13 | b := priv_sym.BarFn(t)
vlib/v/checker/tests/modules/private_symbol/main.v:13:16: error: function type `priv_sym.BarFn` was declared as private to module `priv_sym`, so it can not be used inside module `main`
11 | a := priv_sym.Foo(0)
12 | dump(a)
13 | b := priv_sym.BarFn(t)
| ~~~~~~~~
14 | dump(b)
15 | c := priv_sym.PubFoo(0)
19 changes: 19 additions & 0 deletions vlib/v/checker/tests/modules/private_symbol/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module main

import priv_sym

fn t() int {
return 0
}

fn main() {
priv_sym.priv()
a := priv_sym.Foo(0)
dump(a)
b := priv_sym.BarFn(t)
dump(b)
c := priv_sym.PubFoo(0)
dump(c)
d := priv_sym.PubBarFn(t)
dump(d)
}
12 changes: 12 additions & 0 deletions vlib/v/checker/tests/modules/private_symbol/priv_sym.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// sub module
module priv_sym

type Foo = int
pub type PubFoo = int

type BarFn = fn () int

pub type PubBarFn = fn () int

fn priv() {
}
felipensp marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion vlib/x/templating/dtm/dynamic_template_manager.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import time
import regex

// These are all the types of dynamic values that the DTM allows to be returned in the context of a map
type DtmMultiTypeMap = f32 | f64 | i16 | i64 | i8 | int | string | u16 | u32 | u64 | u8
pub type DtmMultiTypeMap = f32 | f64 | i16 | i64 | i8 | int | string | u16 | u32 | u64 | u8

// type MiddlewareFn = fn (mut Context, string) bool

Expand Down
Loading