From 093cab6f5618668bf594a236edab26c6ca01d2a9 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 9 Oct 2021 20:03:37 +0800 Subject: [PATCH] checker: check generics struct init that types mismatch (fix #12115) (#12120) --- vlib/v/checker/checker.v | 11 +++++++---- .../tests/generics_struct_type_mismatch_err.out | 1 + .../tests/generics_struct_type_mismatch_err.vv | 11 +++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 vlib/v/checker/tests/generics_struct_type_mismatch_err.out create mode 100644 vlib/v/checker/tests/generics_struct_type_mismatch_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 67326f7a73..c5d31bfb2c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -856,10 +856,13 @@ pub fn (mut c Checker) generic_insts_to_concrete() { } typ.is_public = true typ.kind = parent.kind - } - parent_sym := c.table.get_type_symbol(parent_info.parent_type) - for method in parent_sym.methods { - c.table.register_fn_concrete_types(method.name, info.concrete_types) + + parent_sym := c.table.get_type_symbol(parent_info.parent_type) + for method in parent_sym.methods { + c.table.register_fn_concrete_types(method.name, info.concrete_types) + } + } else { + util.verror('generic error', 'the number of generic types of struct `$parent.name` is inconsistent with the concrete types') } } ast.Interface { diff --git a/vlib/v/checker/tests/generics_struct_type_mismatch_err.out b/vlib/v/checker/tests/generics_struct_type_mismatch_err.out new file mode 100644 index 0000000000..2c7760d6ea --- /dev/null +++ b/vlib/v/checker/tests/generics_struct_type_mismatch_err.out @@ -0,0 +1 @@ +generic error: the number of generic types of struct `main.Example` is inconsistent with the concrete types diff --git a/vlib/v/checker/tests/generics_struct_type_mismatch_err.vv b/vlib/v/checker/tests/generics_struct_type_mismatch_err.vv new file mode 100644 index 0000000000..67617ae0d3 --- /dev/null +++ b/vlib/v/checker/tests/generics_struct_type_mismatch_err.vv @@ -0,0 +1,11 @@ +struct Example { + key T + value V +} + +fn main() { + example := Example{ + key: 'key' + value: 'value' + } +}