checker: add check for using a private const in another module (#8501)

pull/8530/head
zakuro 2021-02-03 17:17:13 +09:00 committed by GitHub
parent 3ef4885094
commit e30e794884
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 10 deletions

View File

@ -7,7 +7,7 @@ const (
)
// TODO: remove these and uae the enum everywhere
const (
pub const (
align_left = HorizontalAlign.left
align_right = HorizontalAlign.right
)

View File

@ -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)
)

View File

@ -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)

View File

@ -5,7 +5,7 @@ module time
#include <time.h>
const (
pub const (
days_string = 'MonTueWedThuFriSatSun'
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
months_string = 'JanFebMarAprMayJunJulAugSepOctNovDec'

View File

@ -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

View File

@ -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 | }

View File

@ -0,0 +1,5 @@
import v.scanner
fn main() {
println(scanner.single_quote)
}

View File

@ -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

View File

@ -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()
)