parser/checker: do not allow using private types from other modules

pull/4716/head
Alexander Medvednikov 2020-05-04 21:56:41 +02:00
parent 9aa1a65489
commit 32a7bd3a48
7 changed files with 22 additions and 10 deletions

View File

@ -29,9 +29,9 @@ const (
OpenDataConnection = 150 OpenDataConnection = 150
CloseDataConnection = 226 CloseDataConnection = 226
CommandOk = 200 CommandOk = 200
Denied = 550 denied = 550
PassiveMode = 227 PassiveMode = 227
Complete = 226 complete = 226
) )
struct DTP { struct DTP {
@ -173,13 +173,13 @@ pub fn (ftp FTP) pwd() string {
pub fn (ftp FTP) cd(dir string) { pub fn (ftp FTP) cd(dir string) {
ftp.write('CWD $dir') or { return } ftp.write('CWD $dir') or { return }
mut code, mut data := ftp.read() mut code, mut data := ftp.read()
match code { match int(code) {
Denied { denied {
$if debug { $if debug {
println('CD $dir denied!') println('CD $dir denied!')
} }
} }
Complete { complete {
code, data = ftp.read() code, data = ftp.read()
} }
else {} else {}
@ -233,7 +233,7 @@ pub fn (ftp FTP) dir() ?[]string {
ftp.write('LIST') or {} ftp.write('LIST') or {}
code, _ := ftp.read() code, _ := ftp.read()
if code == Denied { if code == denied {
return error('LIST denied') return error('LIST denied')
} }
if code != OpenDataConnection { if code != OpenDataConnection {
@ -267,7 +267,7 @@ pub fn (ftp FTP) get(file string) ?[]byte {
ftp.write('RETR $file') or {} ftp.write('RETR $file') or {}
code, _ := ftp.read() code, _ := ftp.read()
if code == Denied { if code == denied {
return error('Permission denied') return error('Permission denied')
} }

View File

@ -393,6 +393,11 @@ fn vpclose(f voidptr) int {
} }
} }
struct Foo2 {
x int
}
pub struct Result { pub struct Result {
pub: pub:
exit_code int exit_code int

View File

@ -6,7 +6,7 @@ module cflag
import os import os
// parsed cflag // parsed cflag
struct CFlag { pub struct CFlag {
mod string // the module in which the flag was given mod string // the module in which the flag was given
os string // eg. windows | darwin | linux os string // eg. windows | darwin | linux
name string // eg. -I name string // eg. -I

View File

@ -260,6 +260,9 @@ pub fn (mut c Checker) struct_init(struct_init mut ast.StructInit) table.Type {
struct_init.typ = c.expected_type struct_init.typ = c.expected_type
} }
type_sym := c.table.get_type_symbol(struct_init.typ) 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') // println('check struct $typ_sym.name')
match type_sym.kind { match type_sym.kind {
.placeholder { .placeholder {

View File

@ -12,7 +12,7 @@ import v.util
import term import term
import os import os
struct Parser { pub struct Parser {
scanner &scanner.Scanner scanner &scanner.Scanner
file_name string // "/home/user/hello.v" file_name string // "/home/user/hello.v"
file_name_dir string // "/home/user" file_name_dir string // "/home/user"
@ -1202,6 +1202,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
info: table.SumType{ info: table.SumType{
variants: sum_variants variants: sum_variants
} }
is_public: is_pub
}) })
return ast.SumTypeDecl{ return ast.SumTypeDecl{
name: name name: name
@ -1220,6 +1221,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
info: table.Alias{ info: table.Alias{
foo: '' foo: ''
} }
is_public: is_pub
}) })
return ast.AliasTypeDecl{ return ast.AliasTypeDecl{
name: name name: name

View File

@ -161,6 +161,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
is_union: is_union is_union: is_union
} }
mod: p.mod mod: p.mod
is_public: is_pub
} }
mut ret := 0 mut ret := 0
if p.builtin_mod && t.name in table.builtin_type_names { if p.builtin_mod && t.name in table.builtin_type_names {

View File

@ -26,6 +26,7 @@ mut:
name string name string
methods []Fn methods []Fn
mod string mod string
is_public bool
} }
pub enum TypeFlag { pub enum TypeFlag {
@ -546,7 +547,7 @@ pub:
// NB: FExpr here is a actually an ast.Expr . // NB: FExpr here is a actually an ast.Expr .
// It should always be used by casting to ast.Expr, using ast.fe2ex()/ast.ex2fe() // 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 . // 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 struct Field {
pub: pub: