1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check private built-in methods are not accessible (#9062)

This commit is contained in:
Nick Treleaven 2021-03-03 09:12:50 +00:00 committed by GitHub
parent bd6693efb8
commit 412c17ccda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 5 deletions

View File

@ -1470,8 +1470,7 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
}
}
if has_method {
if !method.is_pub && !c.is_builtin_mod && !c.pref.is_test && left_type_sym.mod != c.mod
&& left_type_sym.mod != '' { // method.mod != c.mod {
if !method.is_pub && !c.pref.is_test && method.mod != c.mod {
// If a private method is called outside of the module
// its receiver type is defined in, show an error.
// println('warn $method_name lef.mod=$left_type_sym.mod c.mod=$c.mod')
@ -1823,8 +1822,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
}
}
if !f.is_pub && f.language == .v && f.name.len > 0 && f.mod.len > 0 && f.mod != c.mod {
c.error('function `$f.name` is private, so you can not import it in module `$c.mod`',
call_expr.pos)
c.error('function `$f.name` is private', call_expr.pos)
}
if !c.cur_fn.is_deprecated && f.is_deprecated {
mut deprecation_message := 'function `$f.name` has been deprecated'

View File

@ -3,9 +3,16 @@ vlib/v/checker/tests/import_symbol_fn_private_err.vv:1:20: error: module `time`
| ~~~~~
2 | fn main() {
3 | since(now())
vlib/v/checker/tests/import_symbol_fn_private_err.vv:3:3: error: function `time.since` is private, so you can not import it in module `main`
vlib/v/checker/tests/import_symbol_fn_private_err.vv:3:3: error: function `time.since` is private
1 | import time { now, since }
2 | fn main() {
3 | since(now())
| ~~~~~~~~~~~~
4 | }
5 |
vlib/v/checker/tests/import_symbol_fn_private_err.vv:7:20: error: method `map[string]int.exists_1` is private
5 |
6 | fn method() {
7 | _ = map{'h':2}.exists_1('h')
| ~~~~~~~~~~~~~
8 | }

View File

@ -2,3 +2,7 @@ import time { now, since }
fn main() {
since(now())
}
fn method() {
_ = map{'h':2}.exists_1('h')
}