From f5e03deaa80270cb86fe053265173e0752c4ee26 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 23 Mar 2025 13:40:27 -0300 Subject: [PATCH] checker: fix chan element type validation with inexistent type (fix #23978) (#24008) --- vlib/v/ast/table.v | 10 +++++++--- vlib/v/checker/checker.v | 10 ++++++++++ vlib/v/checker/tests/chan_unknown_err.out | 7 +++++++ vlib/v/checker/tests/chan_unknown_err.vv | 8 ++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 vlib/v/checker/tests/chan_unknown_err.out create mode 100644 vlib/v/checker/tests/chan_unknown_err.vv diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index f453c8b03..ce9fa1cfb 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -1499,9 +1499,13 @@ pub fn (t &Table) known_type_names() []string { for _, idx in t.type_idxs { typ := idx_to_type(idx) // Skip `int_literal_type_idx` and `float_literal_type_idx` because they shouldn't be visible to the User. - if idx !in [0, int_literal_type_idx, float_literal_type_idx] && t.known_type_idx(typ) - && t.sym(typ).kind != .function { - res << t.type_to_str(typ) + if idx !in [0, int_literal_type_idx, float_literal_type_idx] && t.known_type_idx(typ) { + tsym := t.sym(typ) + if tsym.kind !in [.function, .chan] { + res << t.type_to_str(typ) + } else if tsym.info is Chan && t.sym(tsym.info.elem_type).kind != .placeholder { + res << t.type_to_str(tsym.info.elem_type) + } } } return res diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1a6a26ae0..488821c7f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -5487,6 +5487,16 @@ fn (mut c Checker) ensure_type_exists(typ ast.Type, pos token.Pos) bool { } } } + .chan { + if sym.info is ast.Chan { + if !c.ensure_type_exists((sym.info as ast.Chan).elem_type, token.Pos{ + ...pos + col: pos.col + 5 + }) { + return false + } + } + } else {} } return true diff --git a/vlib/v/checker/tests/chan_unknown_err.out b/vlib/v/checker/tests/chan_unknown_err.out new file mode 100644 index 000000000..e9bbdd337 --- /dev/null +++ b/vlib/v/checker/tests/chan_unknown_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/chan_unknown_err.vv:7:11: error: unknown type `DoesNotExist`. +Did you mean `DoesNotExists`? + 5 | struct ChanBug { + 6 | pub mut: + 7 | bug chan DoesNotExist + | ~~~~~~~~~~~~ + 8 | } diff --git a/vlib/v/checker/tests/chan_unknown_err.vv b/vlib/v/checker/tests/chan_unknown_err.vv new file mode 100644 index 000000000..60d45325c --- /dev/null +++ b/vlib/v/checker/tests/chan_unknown_err.vv @@ -0,0 +1,8 @@ +module main + +struct DoesNotExists {} + +struct ChanBug { +pub mut: + bug chan DoesNotExist +} -- 2.39.5