From e30e794884eda31a250824c71a7d361f49e53963 Mon Sep 17 00:00:00 2001 From: zakuro Date: Wed, 3 Feb 2021 17:17:13 +0900 Subject: [PATCH] checker: add check for using a private const in another module (#8501) --- vlib/gx/text.v | 8 ++++---- vlib/net/common.v | 4 ++-- vlib/net/errors.v | 5 ++++- vlib/time/time.v | 2 +- vlib/v/checker/checker.v | 3 +++ .../checker/tests/import_symbol_const_private_err.out | 6 ++++++ .../v/checker/tests/import_symbol_const_private_err.vv | 5 +++++ vlib/v/parser/parser.v | 1 + vlib/v/token/token.v | 10 ++++++++-- 9 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 vlib/v/checker/tests/import_symbol_const_private_err.out create mode 100644 vlib/v/checker/tests/import_symbol_const_private_err.vv diff --git a/vlib/gx/text.v b/vlib/gx/text.v index 72ddfa988f..53c4bd4cd3 100644 --- a/vlib/gx/text.v +++ b/vlib/gx/text.v @@ -7,8 +7,8 @@ const ( ) // TODO: remove these and uae the enum everywhere -const ( - align_left = HorizontalAlign.left +pub const ( + align_left = HorizontalAlign.left align_right = HorizontalAlign.right ) @@ -28,9 +28,9 @@ pub enum VerticalAlign { pub struct TextCfg { pub: color Color = black - size int = 16 + size int = 16 align HorizontalAlign = .left - vertical_align VerticalAlign = .top + vertical_align VerticalAlign = .top max_width int family string bold bool diff --git a/vlib/net/common.v b/vlib/net/common.v index 0b86a7cce4..3d25ec41ca 100644 --- a/vlib/net/common.v +++ b/vlib/net/common.v @@ -111,12 +111,12 @@ const ( // no_timeout should be given to functions when no timeout is wanted (i.e. all functions // return instantly) -const ( +pub const ( no_timeout = time.Duration(0) ) // infinite_timeout should be given to functions when an infinite_timeout is wanted (i.e. functions // only ever return with data) -const ( +pub const ( infinite_timeout = time.Duration(-1) ) diff --git a/vlib/net/errors.v b/vlib/net/errors.v index ec5f4069dd..6187abe6bd 100644 --- a/vlib/net/errors.v +++ b/vlib/net/errors.v @@ -1,8 +1,11 @@ module net -// Well defined errors that are returned from socket functions const ( errors_base = 0 +) + +// Well defined errors that are returned from socket functions +pub const ( err_new_socket_failed = error_with_code('net: new_socket failed to create socket', errors_base+1) err_option_not_settable = error_with_code('net: set_option_xxx option not settable', errors_base+2) err_option_wrong_type = error_with_code('net: set_option_xxx option wrong type', errors_base+3) diff --git a/vlib/time/time.v b/vlib/time/time.v index 248fe9e940..c881c83641 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -5,7 +5,7 @@ module time #include -const ( +pub const ( days_string = 'MonTueWedThuFriSatSun' month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] months_string = 'JanFebMarAprMayJunJulAugSepOctNovDec' diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 237d991271..e52fd036d1 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4037,6 +4037,9 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type { if obj := c.file.global_scope.find(name) { match mut obj { ast.ConstField { + if !(obj.is_pub || obj.mod == c.mod || c.pref.is_test) { + c.error('constant `$obj.name` is private', ident.pos) + } mut typ := obj.typ if typ == 0 { c.inside_const = true diff --git a/vlib/v/checker/tests/import_symbol_const_private_err.out b/vlib/v/checker/tests/import_symbol_const_private_err.out new file mode 100644 index 0000000000..42c70dee95 --- /dev/null +++ b/vlib/v/checker/tests/import_symbol_const_private_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/import_symbol_const_private_err.vv:4:19: error: constant `v.scanner.single_quote` is private + 2 | + 3 | fn main() { + 4 | println(scanner.single_quote) + | ~~~~~~~~~~~~ + 5 | } diff --git a/vlib/v/checker/tests/import_symbol_const_private_err.vv b/vlib/v/checker/tests/import_symbol_const_private_err.vv new file mode 100644 index 0000000000..df1f872759 --- /dev/null +++ b/vlib/v/checker/tests/import_symbol_const_private_err.vv @@ -0,0 +1,5 @@ +import v.scanner + +fn main() { + println(scanner.single_quote) +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index efcccc1767..e6dc90c73d 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2002,6 +2002,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl { field := ast.ConstField{ name: full_name mod: p.mod + is_pub: is_pub expr: expr pos: pos comments: comments diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index d2bdf9ef95..1142d42562 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -126,9 +126,12 @@ pub enum Kind { _end_ } -const ( +pub const ( assign_tokens = [Kind.assign, .plus_assign, .minus_assign, .mult_assign, .div_assign, .xor_assign, .mod_assign, .or_assign, .and_assign, .right_shift_assign, .left_shift_assign] +) + +const ( nr_tokens = int(Kind._end_) ) @@ -160,7 +163,7 @@ pub enum AtKind { vmod_file } -const ( +pub const ( valid_at_tokens = ['@FN', '@MOD', '@STRUCT', '@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VMOD_FILE', ] @@ -291,6 +294,9 @@ fn build_token_str() []string { const ( token_str = build_token_str() +) + +pub const ( keywords = build_keys() )