1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

compiler: force custom struct .str() methods to be defined public

This commit is contained in:
joe-conigliaro 2019-12-11 11:24:26 +11:00 committed by Alexander Medvednikov
parent f286387647
commit 3486118ab7
9 changed files with 22 additions and 18 deletions

View File

@ -341,11 +341,11 @@ mut:
b []Test2
}
fn (t Test2) str() string {
pub fn (t Test2) str() string {
return '{$t.one $t.two}'
}
fn (t Test) str() string {
pub fn (t Test) str() string {
return '{$t.a $t.b}'
}

View File

@ -14,7 +14,7 @@ struct CFlag{
value string // eg. /path/to/include
}
fn (c &CFlag) str() string {
pub fn (c &CFlag) str() string {
return 'CFlag{ name: "$c.name" value: "$c.value" mod: "$c.mod" os: "$c.os" }'
}

View File

@ -199,7 +199,7 @@ mut:
last_nl_pos int
}
fn (s ScannerPos) str() string {
pub fn (s ScannerPos) str() string {
return 'ScannerPos{ ${s.pos:5d} , ${s.line_nr:5d} , ${s.last_nl_pos:5d} }'
}

View File

@ -314,7 +314,7 @@ fn (p mut Parser) gen_array_str(typ Type) {
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
}
p.v.vgen_buf.writeln('
fn (a $typ.name) str() string {
pub fn (a $typ.name) str() string {
mut sb := strings.new_builder(a.len * 3)
sb.write("[")
for i, elm in a {
@ -342,7 +342,7 @@ fn (p mut Parser) gen_struct_str(typ Type) {
})
mut sb := strings.new_builder(typ.fields.len * 20)
sb.writeln('fn (a $typ.name) str() string {\nreturn')
sb.writeln('pub fn (a $typ.name) str() string {\nreturn')
sb.writeln("'{")
for field in typ.fields {
sb.writeln('\t$field.name: $' + 'a.${field.name}')
@ -366,7 +366,7 @@ fn (p mut Parser) gen_varg_str(typ Type) {
p.gen_struct_str(elm_type2)
}
p.v.vgen_buf.writeln('
fn (a $typ.name) str() string {
pub fn (a $typ.name) str() string {
mut sb := strings.new_builder(a.len * 3)
sb.write("[")
for i, elm in a {

View File

@ -60,7 +60,7 @@ const (
MainFn = Fn{ name: 'main' }
)
fn (a []TypeInst) str() string {
pub fn (a []TypeInst) str() string {
mut r := []string
for t in a {
mut s := ' | '
@ -254,7 +254,7 @@ fn (p mut Parser) fn_decl() {
}
// Don't allow modifying types from a different module
if !p.first_pass() && !p.builtin_mod && t.mod != p.mod &&
!p.is_vgen // allow .str()
!p.is_vgen // let vgen define methods like .str() on types defined in other modules
{
//println('T.mod=$T.mod')
//println('p.mod=$p.mod')
@ -297,6 +297,10 @@ fn (p mut Parser) fn_decl() {
if f.name == 'init' && !f.is_method && f.is_public && !p.is_vh {
p.error('init function cannot be public')
}
// .str() methods
if f.is_method && f.name == 'str' && !f.is_public {
p.error('.str() methods must be declared as public')
}
// C function header def? (fn C.NSMakeRect(int,int,int,int))
is_c := f.name == 'C' && p.tok == .dot
// Just fn signature? only builtin.v + default build mode
@ -726,7 +730,7 @@ fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type s
if f.is_deprecated {
p.warn('$f.name is deprecated')
}
if !f.is_public && !f.is_c && !p.pref.is_test && !f.is_interface && f.mod != p.mod {
if !f.is_public && !f.is_c && !p.pref.is_test && !f.is_interface && f.mod != p.mod {
if f.name == 'contains' {
println('use `value in numbers` instead of `numbers.contains(value)`')
}

View File

@ -122,7 +122,7 @@ struct TypeNode {
/*
// For debugging types
fn (t Type) str() string {
pub fn (t Type) str() string {
mut s := 'type "$t.name" {'
if t.fields.len > 0 {
// s += '\n $t.fields.len fields:\n'
@ -185,7 +185,7 @@ const (
)
// This is used for debugging only
fn (f Fn) str() string {
pub fn (f Fn) str() string {
t := Table{}
str_args := f.str_args(t)
return '${f.name}($str_args) $f.typ'

View File

@ -1,6 +1,6 @@
struct Foo {
}
fn (f Foo) str() string { return 'Foo{}' }
pub fn (f Foo) str() string { return 'Foo{}' }
fn process_foo(foo &Foo) {
println('>process_foo, called for ${foo} === ${*foo}')

View File

@ -257,7 +257,7 @@ fn is_key(key string) bool {
return int(key_to_token(key)) > 0
}
fn (t TokenKind) str() string {
pub fn (t TokenKind) str() string {
return TokenStr[int(t)]
}
@ -290,7 +290,7 @@ fn (t []TokenKind) contains(val TokenKind) bool {
return false
}
fn (t Token) str() string {
pub fn (t Token) str() string {
if t.tok == .number {
return t.lit

View File

@ -49,15 +49,15 @@ fn mat4(f &f32) Mat4 {
return res
}
fn (v Vec3) str() string {
pub fn (v Vec3) str() string {
return 'Vec3{ $v.x, $v.y, $v.z }'
}
fn (v Vec2) str() string {
pub fn (v Vec2) str() string {
return 'Vec3{ $v.x, $v.y }'
}
fn (m Mat4) str() string {
pub fn (m Mat4) str() string {
mut s := '[ '
for i := 0; i < 4; i++ {
if i != 0 {