From 32a7bd3a48de6dd4232515ac002c931bba8443bb Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 4 May 2020 21:56:41 +0200 Subject: [PATCH] parser/checker: do not allow using private types from other modules --- vlib/net/ftp/ftp.v | 14 +++++++------- vlib/os/os.v | 5 +++++ vlib/v/cflag/cflags.v | 2 +- vlib/v/checker/checker.v | 3 +++ vlib/v/parser/parser.v | 4 +++- vlib/v/parser/struct.v | 1 + vlib/v/table/atypes.v | 3 ++- 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/vlib/net/ftp/ftp.v b/vlib/net/ftp/ftp.v index 42c7fdba05..c7a8a639f2 100644 --- a/vlib/net/ftp/ftp.v +++ b/vlib/net/ftp/ftp.v @@ -29,9 +29,9 @@ const ( OpenDataConnection = 150 CloseDataConnection = 226 CommandOk = 200 - Denied = 550 + denied = 550 PassiveMode = 227 - Complete = 226 + complete = 226 ) struct DTP { @@ -173,13 +173,13 @@ pub fn (ftp FTP) pwd() string { pub fn (ftp FTP) cd(dir string) { ftp.write('CWD $dir') or { return } mut code, mut data := ftp.read() - match code { - Denied { + match int(code) { + denied { $if debug { println('CD $dir denied!') } } - Complete { + complete { code, data = ftp.read() } else {} @@ -233,7 +233,7 @@ pub fn (ftp FTP) dir() ?[]string { ftp.write('LIST') or {} code, _ := ftp.read() - if code == Denied { + if code == denied { return error('LIST denied') } if code != OpenDataConnection { @@ -267,7 +267,7 @@ pub fn (ftp FTP) get(file string) ?[]byte { ftp.write('RETR $file') or {} code, _ := ftp.read() - if code == Denied { + if code == denied { return error('Permission denied') } diff --git a/vlib/os/os.v b/vlib/os/os.v index 357b0a182d..40433669fe 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -393,6 +393,11 @@ fn vpclose(f voidptr) int { } } +struct Foo2 { + x int + +} + pub struct Result { pub: exit_code int diff --git a/vlib/v/cflag/cflags.v b/vlib/v/cflag/cflags.v index 31b78a95f7..e51a8e87e9 100644 --- a/vlib/v/cflag/cflags.v +++ b/vlib/v/cflag/cflags.v @@ -6,7 +6,7 @@ module cflag import os // parsed cflag -struct CFlag { +pub struct CFlag { mod string // the module in which the flag was given os string // eg. windows | darwin | linux name string // eg. -I diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 65a8158b44..316544e1a8 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -260,6 +260,9 @@ pub fn (mut c Checker) struct_init(struct_init mut ast.StructInit) table.Type { struct_init.typ = c.expected_type } type_sym := c.table.get_type_symbol(struct_init.typ) + if !type_sym.is_public && type_sym.mod != c.mod { + c.warn('type `$type_sym.name` is private', struct_init.pos) + } // println('check struct $typ_sym.name') match type_sym.kind { .placeholder { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index f68d388583..bf0af37977 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -12,7 +12,7 @@ import v.util import term import os -struct Parser { +pub struct Parser { scanner &scanner.Scanner file_name string // "/home/user/hello.v" file_name_dir string // "/home/user" @@ -1202,6 +1202,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl { info: table.SumType{ variants: sum_variants } + is_public: is_pub }) return ast.SumTypeDecl{ name: name @@ -1220,6 +1221,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl { info: table.Alias{ foo: '' } + is_public: is_pub }) return ast.AliasTypeDecl{ name: name diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 00a372fb6b..1b3e097cb1 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -161,6 +161,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { is_union: is_union } mod: p.mod + is_public: is_pub } mut ret := 0 if p.builtin_mod && t.name in table.builtin_type_names { diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index 225a3cd1f5..90c135a009 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -26,6 +26,7 @@ mut: name string methods []Fn mod string + is_public bool } pub enum TypeFlag { @@ -546,7 +547,7 @@ pub: // NB: FExpr here is a actually an ast.Expr . // It should always be used by casting to ast.Expr, using ast.fe2ex()/ast.ex2fe() // That hack is needed to break an import cycle between v.ast and v.table . -type FExpr = byteptr | voidptr +pub type FExpr = byteptr | voidptr pub struct Field { pub: