Skip to content

Commit 3486118

Browse files
joe-conigliaromedvednikov
authored andcommitted
compiler: force custom struct .str() methods to be defined public
1 parent f286387 commit 3486118

9 files changed

Lines changed: 22 additions & 18 deletions

File tree

vlib/builtin/array_test.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,11 @@ mut:
341341
b []Test2
342342
}
343343

344-
fn (t Test2) str() string {
344+
pub fn (t Test2) str() string {
345345
return '{$t.one $t.two}'
346346
}
347347

348-
fn (t Test) str() string {
348+
pub fn (t Test) str() string {
349349
return '{$t.a $t.b}'
350350
}
351351

vlib/compiler/cflags.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct CFlag{
1414
value string // eg. /path/to/include
1515
}
1616

17-
fn (c &CFlag) str() string {
17+
pub fn (c &CFlag) str() string {
1818
return 'CFlag{ name: "$c.name" value: "$c.value" mod: "$c.mod" os: "$c.os" }'
1919
}
2020

vlib/compiler/compile_errors.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ mut:
199199
last_nl_pos int
200200
}
201201

202-
fn (s ScannerPos) str() string {
202+
pub fn (s ScannerPos) str() string {
203203
return 'ScannerPos{ ${s.pos:5d} , ${s.line_nr:5d} , ${s.last_nl_pos:5d} }'
204204
}
205205

vlib/compiler/comptime.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ fn (p mut Parser) gen_array_str(typ Type) {
314314
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
315315
}
316316
p.v.vgen_buf.writeln('
317-
fn (a $typ.name) str() string {
317+
pub fn (a $typ.name) str() string {
318318
mut sb := strings.new_builder(a.len * 3)
319319
sb.write("[")
320320
for i, elm in a {
@@ -342,7 +342,7 @@ fn (p mut Parser) gen_struct_str(typ Type) {
342342
})
343343

344344
mut sb := strings.new_builder(typ.fields.len * 20)
345-
sb.writeln('fn (a $typ.name) str() string {\nreturn')
345+
sb.writeln('pub fn (a $typ.name) str() string {\nreturn')
346346
sb.writeln("'{")
347347
for field in typ.fields {
348348
sb.writeln('\t$field.name: $' + 'a.${field.name}')
@@ -366,7 +366,7 @@ fn (p mut Parser) gen_varg_str(typ Type) {
366366
p.gen_struct_str(elm_type2)
367367
}
368368
p.v.vgen_buf.writeln('
369-
fn (a $typ.name) str() string {
369+
pub fn (a $typ.name) str() string {
370370
mut sb := strings.new_builder(a.len * 3)
371371
sb.write("[")
372372
for i, elm in a {

vlib/compiler/fn.v

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const (
6060
MainFn = Fn{ name: 'main' }
6161
)
6262

63-
fn (a []TypeInst) str() string {
63+
pub fn (a []TypeInst) str() string {
6464
mut r := []string
6565
for t in a {
6666
mut s := ' | '
@@ -254,7 +254,7 @@ fn (p mut Parser) fn_decl() {
254254
}
255255
// Don't allow modifying types from a different module
256256
if !p.first_pass() && !p.builtin_mod && t.mod != p.mod &&
257-
!p.is_vgen // allow .str()
257+
!p.is_vgen // let vgen define methods like .str() on types defined in other modules
258258
{
259259
//println('T.mod=$T.mod')
260260
//println('p.mod=$p.mod')
@@ -297,6 +297,10 @@ fn (p mut Parser) fn_decl() {
297297
if f.name == 'init' && !f.is_method && f.is_public && !p.is_vh {
298298
p.error('init function cannot be public')
299299
}
300+
// .str() methods
301+
if f.is_method && f.name == 'str' && !f.is_public {
302+
p.error('.str() methods must be declared as public')
303+
}
300304
// C function header def? (fn C.NSMakeRect(int,int,int,int))
301305
is_c := f.name == 'C' && p.tok == .dot
302306
// 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
726730
if f.is_deprecated {
727731
p.warn('$f.name is deprecated')
728732
}
729-
if !f.is_public && !f.is_c && !p.pref.is_test && !f.is_interface && f.mod != p.mod {
733+
if !f.is_public && !f.is_c && !p.pref.is_test && !f.is_interface && f.mod != p.mod {
730734
if f.name == 'contains' {
731735
println('use `value in numbers` instead of `numbers.contains(value)`')
732736
}

vlib/compiler/table.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct TypeNode {
122122

123123
/*
124124
// For debugging types
125-
fn (t Type) str() string {
125+
pub fn (t Type) str() string {
126126
mut s := 'type "$t.name" {'
127127
if t.fields.len > 0 {
128128
// s += '\n $t.fields.len fields:\n'
@@ -185,7 +185,7 @@ const (
185185
)
186186

187187
// This is used for debugging only
188-
fn (f Fn) str() string {
188+
pub fn (f Fn) str() string {
189189
t := Table{}
190190
str_args := f.str_args(t)
191191
return '${f.name}($str_args) $f.typ'

vlib/compiler/tests/fn_expecting_ref_but_returning_struct_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct Foo {
22
}
3-
fn (f Foo) str() string { return 'Foo{}' }
3+
pub fn (f Foo) str() string { return 'Foo{}' }
44

55
fn process_foo(foo &Foo) {
66
println('>process_foo, called for ${foo} === ${*foo}')

vlib/compiler/token.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ fn is_key(key string) bool {
257257
return int(key_to_token(key)) > 0
258258
}
259259

260-
fn (t TokenKind) str() string {
260+
pub fn (t TokenKind) str() string {
261261
return TokenStr[int(t)]
262262
}
263263

@@ -290,7 +290,7 @@ fn (t []TokenKind) contains(val TokenKind) bool {
290290
return false
291291
}
292292

293-
fn (t Token) str() string {
293+
pub fn (t Token) str() string {
294294
if t.tok == .number {
295295
return t.lit
296296

vlib/glm/glm.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ fn mat4(f &f32) Mat4 {
4949
return res
5050
}
5151

52-
fn (v Vec3) str() string {
52+
pub fn (v Vec3) str() string {
5353
return 'Vec3{ $v.x, $v.y, $v.z }'
5454
}
5555

56-
fn (v Vec2) str() string {
56+
pub fn (v Vec2) str() string {
5757
return 'Vec3{ $v.x, $v.y }'
5858
}
5959

60-
fn (m Mat4) str() string {
60+
pub fn (m Mat4) str() string {
6161
mut s := '[ '
6262
for i := 0; i < 4; i++ {
6363
if i != 0 {

0 commit comments

Comments
 (0)