From 34d9790f14a5926b0f416d42c3297e98b407570b Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sat, 22 Nov 2025 13:06:34 +0530 Subject: [PATCH] parser: disallow generic function to be exported (fix #25794) (#25806) --- vlib/v/parser/fn.v | 8 ++++++++ vlib/v/parser/tests/generic_fn_export_err.out | 5 +++++ vlib/v/parser/tests/generic_fn_export_err.vv | 4 ++++ 3 files changed, 17 insertions(+) create mode 100644 vlib/v/parser/tests/generic_fn_export_err.out create mode 100644 vlib/v/parser/tests/generic_fn_export_err.vv diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 580531e34..44a7b2817 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -473,6 +473,14 @@ fn (mut p Parser) fn_decl() ast.FnDecl { } } } + if generic_names.len > 0 { + for fna in fn_attrs { + if fna.name == 'export' { + p.error_with_pos('generic functions cannot be exported', fna.pos) + break + } + } + } // Params params_t, are_params_type_only, mut is_variadic, mut is_c_variadic := p.fn_params() if is_c2v_variadic { diff --git a/vlib/v/parser/tests/generic_fn_export_err.out b/vlib/v/parser/tests/generic_fn_export_err.out new file mode 100644 index 000000000..c69b16000 --- /dev/null +++ b/vlib/v/parser/tests/generic_fn_export_err.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/generic_fn_export_err.vv:1:1: error: generic functions cannot be exported + 1 | @[export: "sort_array"] + | ~~~~~~~~~~~~~~~~~~~~~~~ + 2 | pub fn sort_array[T](mut arr []T) { + 3 | arr.sort(|a, b| a < b) diff --git a/vlib/v/parser/tests/generic_fn_export_err.vv b/vlib/v/parser/tests/generic_fn_export_err.vv new file mode 100644 index 000000000..f3b25ace0 --- /dev/null +++ b/vlib/v/parser/tests/generic_fn_export_err.vv @@ -0,0 +1,4 @@ +@[export: "sort_array"] +pub fn sort_array[T](mut arr []T) { + arr.sort(|a, b| a < b) +} -- 2.39.5