Skip to content

Commit

Permalink
checker: make the type C.X is private re-declaration error more inf…
Browse files Browse the repository at this point in the history
…ormative
  • Loading branch information
spytheman committed Jun 27, 2022
1 parent 0ef8382 commit 94d6670
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 6 deletions.
5 changes: 3 additions & 2 deletions vlib/v/checker/checker.v
Expand Up @@ -3741,8 +3741,9 @@ fn (mut c Checker) ensure_type_exists(typ ast.Type, pos token.Pos) ? {
return
}
sym := c.table.sym(typ)
if !c.is_builtin_mod && sym.kind == .struct_ && sym.mod != c.mod && !sym.is_pub {
c.error('type `$sym.name` is private', pos)
if !c.is_builtin_mod && sym.kind == .struct_ && !sym.is_pub && sym.mod != c.mod {
c.error('struct `$sym.name` was declared as private to module `$sym.mod`, so it can not be used inside module `$c.mod`',
pos)
return
}
match sym.kind {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/import_symbol_private_err.out
Expand Up @@ -52,7 +52,7 @@ vlib/v/checker/tests/import_symbol_private_err.vv:12:4: error: method `map[strin
| ~~~~~~~~~~~
13 | _ = ReaderWriterImpl{}
14 | }
vlib/v/checker/tests/import_symbol_private_err.vv:13:6: error: type `io.ReaderWriterImpl` is private
vlib/v/checker/tests/import_symbol_private_err.vv:13:6: error: struct `io.ReaderWriterImpl` was declared as private to module `io`, so it can not be used inside module `main`
11 | 'h': 2
12 | }.exists('h')
13 | _ = ReaderWriterImpl{}
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/modules/module_with_redeclaration.out
@@ -0,0 +1,5 @@
vlib/v/checker/tests/modules/module_with_redeclaration/redeclare_time_structs.v:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`)
1 | module module_with_redeclaration
| ^
2 |
3 | pub const used = 1
@@ -0,0 +1,8 @@
module module_with_redeclaration

pub const used = 1

struct C.timeval {
tv_sec u64 // Seconds.
tv_usec u64 // Microseconds.
}
Empty file.
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/private_redeclaration_of_C_timeval.out
@@ -0,0 +1,14 @@
vlib/time/time.c.v:90:11: error: struct `C.timeval` was declared as private to module `module_with_redeclaration`, so it can not be used inside module `time`
88 | return C.GetTickCount()
89 | } $else {
90 | ts := C.timeval{}
| ~~~~~~~~~
91 | C.gettimeofday(&ts, 0)
92 | return i64(ts.tv_sec * u64(1000) + (ts.tv_usec / u64(1000)))
vlib/v/checker/tests/private_redeclaration_of_C_timeval.vv:9:10: error: struct `C.timeval` was declared as private to module `module_with_redeclaration`, so it can not be used inside module `main`
7 | dump(time.now()) // ensure `time` is used
8 | //
9 | ts := C.timeval{}
| ~~~~~~~~~
10 | dump(ts)
11 | }
11 changes: 11 additions & 0 deletions vlib/v/checker/tests/private_redeclaration_of_C_timeval.vv
@@ -0,0 +1,11 @@
import module_with_redeclaration as mr
import time

const used = mr.used

fn main() {
dump(time.now()) // ensure `time` is used
//
ts := C.timeval{}
dump(ts)
}
6 changes: 3 additions & 3 deletions vlib/v/checker/tests/struct_type_is_private_err.out
@@ -1,10 +1,10 @@
vlib/v/checker/tests/struct_type_is_private_err.vv:1:8: warning: module 'sqlite' is imported but never used
1 | import sqlite
| ~~~~~~
2 |
2 |
3 | fn main(){
vlib/v/checker/tests/struct_type_is_private_err.vv:4:10: error: type `C.sqlite3` is private
2 |
vlib/v/checker/tests/struct_type_is_private_err.vv:4:10: error: struct `C.sqlite3` was declared as private to module `sqlite`, so it can not be used inside module `main`
2 |
3 | fn main(){
4 | _ := &C.sqlite3{}
| ~~~~~~~~~
Expand Down

0 comments on commit 94d6670

Please sign in to comment.