Skip to content

Commit

Permalink
compiler: force custom struct .str() methods to be defined public
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-conigliaro authored and medvednikov committed Dec 11, 2019
1 parent f286387 commit 3486118
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions vlib/builtin/array_test.v
Expand Up @@ -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}'
}

Expand Down
2 changes: 1 addition & 1 deletion vlib/compiler/cflags.v
Expand Up @@ -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" }'
}

Expand Down
2 changes: 1 addition & 1 deletion vlib/compiler/compile_errors.v
Expand Up @@ -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} }'
}

Expand Down
6 changes: 3 additions & 3 deletions vlib/compiler/comptime.v
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}')
Expand All @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions vlib/compiler/fn.v
Expand Up @@ -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 := ' | '
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)`')
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/compiler/table.v
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
@@ -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}')
Expand Down
4 changes: 2 additions & 2 deletions vlib/compiler/token.v
Expand Up @@ -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)]
}

Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions vlib/glm/glm.v
Expand Up @@ -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 {
Expand Down

2 comments on commit 3486118

@changrui
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test to public? Do you use it?

@joe-conigliaro
Copy link
Member Author

@joe-conigliaro joe-conigliaro commented on 3486118 Dec 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@changrui, this made all struct .str() methods need to be public, so even in test it must be.
Other parts of code access the str method

Please sign in to comment.