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

checker: make the type C.X is private re-declaration error more informative

This commit is contained in:
Delyan Angelov 2022-06-27 12:54:08 +03:00
parent 0ef8382824
commit 94d6670e8f
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
8 changed files with 45 additions and 6 deletions

View File

@ -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 {

View File

@ -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{}

View File

@ -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

View File

@ -0,0 +1,8 @@
module module_with_redeclaration
pub const used = 1
struct C.timeval {
tv_sec u64 // Seconds.
tv_usec u64 // Microseconds.
}

View File

@ -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 | }

View File

@ -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)
}

View File

@ -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{}
| ~~~~~~~~~