From 412c17ccda2b21bf14e4e34154c7d78f64852323 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Wed, 3 Mar 2021 09:12:50 +0000 Subject: [PATCH] checker: check private built-in methods are not accessible (#9062) --- vlib/v/checker/checker.v | 6 ++---- vlib/v/checker/tests/import_symbol_fn_private_err.out | 9 ++++++++- vlib/v/checker/tests/import_symbol_fn_private_err.vv | 4 ++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 4cddda3091..67949eb25c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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' diff --git a/vlib/v/checker/tests/import_symbol_fn_private_err.out b/vlib/v/checker/tests/import_symbol_fn_private_err.out index c12f27ee2c..b034706220 100644 --- a/vlib/v/checker/tests/import_symbol_fn_private_err.out +++ b/vlib/v/checker/tests/import_symbol_fn_private_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/import_symbol_fn_private_err.vv b/vlib/v/checker/tests/import_symbol_fn_private_err.vv index 9c959e89b2..f1425b8ce0 100644 --- a/vlib/v/checker/tests/import_symbol_fn_private_err.vv +++ b/vlib/v/checker/tests/import_symbol_fn_private_err.vv @@ -2,3 +2,7 @@ import time { now, since } fn main() { since(now()) } + +fn method() { + _ = map{'h':2}.exists_1('h') +}