From 94d6670e8f78cfc0bb9ab51b8aa3e18df74c0686 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 27 Jun 2022 12:54:08 +0300 Subject: [PATCH] checker: make the `type C.X is private` re-declaration error more informative --- vlib/v/checker/checker.v | 5 +++-- vlib/v/checker/tests/import_symbol_private_err.out | 2 +- .../tests/modules/module_with_redeclaration.out | 5 +++++ .../redeclare_time_structs.v | 8 ++++++++ .../tests/modules/module_with_redeclaration/v.mod | 0 .../tests/private_redeclaration_of_C_timeval.out | 14 ++++++++++++++ .../tests/private_redeclaration_of_C_timeval.vv | 11 +++++++++++ .../v/checker/tests/struct_type_is_private_err.out | 6 +++--- 8 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 vlib/v/checker/tests/modules/module_with_redeclaration.out create mode 100644 vlib/v/checker/tests/modules/module_with_redeclaration/redeclare_time_structs.v create mode 100644 vlib/v/checker/tests/modules/module_with_redeclaration/v.mod create mode 100644 vlib/v/checker/tests/private_redeclaration_of_C_timeval.out create mode 100644 vlib/v/checker/tests/private_redeclaration_of_C_timeval.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a9b9377e52..c3a015ddbb 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 { diff --git a/vlib/v/checker/tests/import_symbol_private_err.out b/vlib/v/checker/tests/import_symbol_private_err.out index e9ba70da09..61b570c6d1 100644 --- a/vlib/v/checker/tests/import_symbol_private_err.out +++ b/vlib/v/checker/tests/import_symbol_private_err.out @@ -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{} diff --git a/vlib/v/checker/tests/modules/module_with_redeclaration.out b/vlib/v/checker/tests/modules/module_with_redeclaration.out new file mode 100644 index 0000000000..b44170bda8 --- /dev/null +++ b/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 diff --git a/vlib/v/checker/tests/modules/module_with_redeclaration/redeclare_time_structs.v b/vlib/v/checker/tests/modules/module_with_redeclaration/redeclare_time_structs.v new file mode 100644 index 0000000000..7545677a5d --- /dev/null +++ b/vlib/v/checker/tests/modules/module_with_redeclaration/redeclare_time_structs.v @@ -0,0 +1,8 @@ +module module_with_redeclaration + +pub const used = 1 + +struct C.timeval { + tv_sec u64 // Seconds. + tv_usec u64 // Microseconds. +} diff --git a/vlib/v/checker/tests/modules/module_with_redeclaration/v.mod b/vlib/v/checker/tests/modules/module_with_redeclaration/v.mod new file mode 100644 index 0000000000..e69de29bb2 diff --git a/vlib/v/checker/tests/private_redeclaration_of_C_timeval.out b/vlib/v/checker/tests/private_redeclaration_of_C_timeval.out new file mode 100644 index 0000000000..cce34e2e47 --- /dev/null +++ b/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 | } diff --git a/vlib/v/checker/tests/private_redeclaration_of_C_timeval.vv b/vlib/v/checker/tests/private_redeclaration_of_C_timeval.vv new file mode 100644 index 0000000000..50d2c056cc --- /dev/null +++ b/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) +} diff --git a/vlib/v/checker/tests/struct_type_is_private_err.out b/vlib/v/checker/tests/struct_type_is_private_err.out index eef2b03485..6b9f631401 100644 --- a/vlib/v/checker/tests/struct_type_is_private_err.out +++ b/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{} | ~~~~~~~~~